为什么 `@NotBlank` 没有显示为导入?

huangapple go评论111阅读模式
英文:

why isn't @NotBlank not showing up as an import

问题

  1. import com.fasterxml.jackson.annotation.JsonProperty;
  2. import org.springframework.lang.NonNull;
  3. import javax.validation.constraints.NotBlank;
  4. import java.util.UUID;
  5. public class Students {
  6. private final UUID studentId;
  7. @NotBlank
  8. private final String firstName;
  9. @NotBlank
  10. private final String lastName;
  11. @NotBlank
  12. private final String email;
  13. @NonNull
  14. private final Gender gender;
  15. public Students(@JsonProperty("studentId") UUID studentId,
  16. @JsonProperty("firstName") String firstName,
  17. @JsonProperty("lastName") String lastName,
  18. @JsonProperty("email") String email,
  19. @JsonProperty("gender") Gender gender) {
  20. this.studentId = studentId;
  21. this.firstName = firstName;
  22. this.lastName = lastName;
  23. this.email = email;
  24. this.gender = gender;
  25. }
  26. public UUID getStudentId() {
  27. return studentId;
  28. }
  29. public String getFirstName() {
  30. return firstName;
  31. }
  32. public String getLastName() {
  33. return lastName;
  34. }
  35. public String getEmail() {
  36. return email;
  37. }
  38. public Gender getGender() {
  39. return gender;
  40. }
  41. @Override
  42. public String toString() {
  43. return "Students{" +
  44. "studentId=" + studentId +
  45. ", firstName='" + firstName + '\'' +
  46. ", lastName='" + lastName + '\'' +
  47. ", email='" + email + '\'' +
  48. ", gender=" + gender +
  49. '}';
  50. }
  51. enum Gender {
  52. MALE, FEMALE
  53. }
  54. }
  1. <!-- pom.xml -->
  2. <dependency>
  3. <groupId>javax.validation</groupId>
  4. <artifactId>validation-api</artifactId>
  5. <version>2.0.1.Final</version>
  6. </dependency>

Please note that the images and non-code content from the original text have been omitted as per your request. If you need further assistance, feel free to ask.

英文:

Having an issue with my code

为什么 `@NotBlank` 没有显示为导入?
为什么 `@NotBlank` 没有显示为导入?
package com.example.demoSpring.student;

  1. import com.fasterxml.jackson.annotation.JsonProperty;
  2. import org.springframework.lang.NonNull;
  3. **import javax.validation.constraints.NotBlank;**
  4. import java.util.UUID;
  5. public class Students {
  6. private final UUID studentId;
  7. **@NotBlank**
  8. private final String firstName;
  9. **@NotBlank**
  10. private final String lastName;
  11. **@NotBlank**
  12. private final String email;
  13. @NonNull
  14. private final Gender gender;
  15. public Students(@JsonProperty(&quot;studentId&quot;) UUID studentId,
  16. @JsonProperty(&quot;firstName&quot;) String firstName,
  17. @JsonProperty(&quot;lastName&quot;) String lastName,
  18. @JsonProperty(&quot;email&quot;) String email,
  19. @JsonProperty(&quot;gender&quot;) Gender gender) {
  20. this.studentId = studentId;
  21. this.firstName = firstName;
  22. this.lastName = lastName;
  23. this.email = email;
  24. this.gender = gender;
  25. }
  26. public UUID getStudentId() {
  27. return studentId;
  28. }
  29. public String getFirstName() {
  30. return firstName;
  31. }
  32. public String getLastName() {
  33. return lastName;
  34. }
  35. public String getEmail() {
  36. return email;
  37. }
  38. public Gender getGender() {
  39. return gender;
  40. }
  41. @Override
  42. public String toString() {
  43. return &quot;Students{&quot; +
  44. &quot;studentId=&quot; + studentId +
  45. &quot;, firstName=&#39;&quot; + firstName + &#39;\&#39;&#39; +
  46. &quot;, lastName=&#39;&quot; + lastName + &#39;\&#39;&#39; +
  47. &quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
  48. &quot;, gender=&quot; + gender +
  49. &#39;}&#39;;
  50. }
  51. enum Gender{
  52. MALE,FEMALE
  53. }
  54. }

It's telling me that import javaz.validation.contraints.NotBlank doesn't exist, How do I install these on my IntelliJ

Im working on a project and I been having issues with this any import would be helpful, I'm using postman.
it just every time I check import javax.validation.constraints.NotBlank; is red I check options add dependency I get no result maven when I click find jar on web cant find it. How can I import this into my IntelliJ program?

Also here is my pom.xml code

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  4. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  5. &lt;parent&gt;
  6. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  7. &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  8. &lt;version&gt;2.3.0.RELEASE&lt;/version&gt;
  9. &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  10. &lt;/parent&gt;
  11. &lt;groupId&gt;com.example&lt;/groupId&gt;
  12. &lt;artifactId&gt;demoSpring&lt;/artifactId&gt;
  13. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  14. &lt;name&gt;demoSpring&lt;/name&gt;
  15. &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  16. &lt;properties&gt;
  17. &lt;java.version&gt;11&lt;/java.version&gt;
  18. &lt;/properties&gt;
  19. &lt;dependencies&gt;
  20. &lt;dependency&gt;
  21. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  22. &lt;artifactId&gt;spring-boot-starter-data-jdbc&lt;/artifactId&gt;
  23. &lt;/dependency&gt;
  24. &lt;dependency&gt;
  25. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  26. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  27. &lt;/dependency&gt;
  28. &lt;dependency&gt;
  29. &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
  30. &lt;artifactId&gt;flyway-core&lt;/artifactId&gt;
  31. &lt;/dependency&gt;
  32. &lt;dependency&gt;
  33. &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
  34. &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
  35. &lt;scope&gt;runtime&lt;/scope&gt;
  36. &lt;/dependency&gt;
  37. &lt;dependency&gt;
  38. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  39. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  40. &lt;scope&gt;test&lt;/scope&gt;
  41. &lt;exclusions&gt;
  42. &lt;exclusion&gt;
  43. &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
  44. &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
  45. &lt;/exclusion&gt;
  46. &lt;/exclusions&gt;
  47. &lt;/dependency&gt;
  48. &lt;!-- https://mvnrepository.com/artifact/org.assertj/assertj-core --&gt;
  49. &lt;dependency&gt;
  50. &lt;groupId&gt;org.assertj&lt;/groupId&gt;
  51. &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
  52. &lt;version&gt;3.16.1&lt;/version&gt;
  53. &lt;scope&gt;test&lt;/scope&gt;
  54. &lt;/dependency&gt;
  55. &lt;dependency&gt;
  56. &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
  57. &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
  58. &lt;version&gt;3.4.5&lt;/version&gt;
  59. &lt;/dependency&gt;
  60. &lt;dependency&gt;
  61. &lt;groupId&gt;com.example&lt;/groupId&gt;
  62. &lt;artifactId&gt;demoSpring&lt;/artifactId&gt;
  63. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  64. &lt;/dependency&gt;
  65. &lt;dependency&gt;
  66. &lt;groupId&gt;commons-validator&lt;/groupId&gt;
  67. &lt;artifactId&gt;commons-validator&lt;/artifactId&gt;
  68. &lt;version&gt;1.3.1&lt;/version&gt;
  69. &lt;/dependency&gt;
  70. &lt;/dependencies&gt;
  71. &lt;build&gt;
  72. &lt;plugins&gt;
  73. &lt;plugin&gt;
  74. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  75. &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
  76. &lt;/plugin&gt;
  77. &lt;/plugins&gt;
  78. &lt;/build&gt;
  79. &lt;/project&gt;

I added this

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;**javax.validation**&lt;/groupId&gt;
  3. &lt;artifactId&gt;**validation-api**&lt;/artifactId&gt;
  4. &lt;version&gt;**2.0.1.Final**&lt;/version&gt;
  5. &lt;/dependency&gt;

but the bold area is red dependency isn't found for these
为什么 `@NotBlank` 没有显示为导入?

Im still getting an error when I try to input dependency

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;javax.validation&lt;/groupId&gt;
  3. &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
  4. &lt;version&gt;2.0.1.Final&lt;/version&gt;
  5. &lt;/dependency&gt;

答案1

得分: 6

关于 Spring Boot 项目,我添加了 "validation-api" 但 Postman 没有抛出错误,它允许存储空白字段(@NotBlank 不起作用)。然后我使用了以下内容 -

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-validation</artifactId>
  4. <version>2.4.1</version>
  5. </dependency>

它起效了。

英文:

For Spring Boot project, I added "validation-api" but postman was not throwing error and it allowed to store Blank fields (@NotBlank was not working).
Then I used this -

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  3. &lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
  4. &lt;version&gt;2.4.1&lt;/version&gt;
  5. &lt;/dependency&gt;

and it worked.

答案2

得分: 3

需要添加validation-api依赖

  1. <dependency>
  2. <groupId>javax.validation</groupId>
  3. <artifactId>validation-api</artifactId>
  4. <version>2.0.1.Final</version>
  5. </dependency>
英文:

You need to add validation-api dependency

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;javax.validation&lt;/groupId&gt;
  3. &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
  4. &lt;version&gt;2.0.1.Final&lt;/version&gt;
  5. &lt;/dependency&gt;

答案3

得分: 1

你只需要添加这个依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-validation</artifactId>
  4. </dependency>
英文:

you just need to add this dependency:

&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年6月29日 11:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定