java 即使使用@Valid [已关闭],@NotNull在 Spring Boot 中也不起作用

jpfvwuh4  于 2023-03-06  发布在  Java
关注(0)|答案(2)|浏览(153)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我浏览了所有@NotNull不起作用的帖子。大多数都建议缺少@Valid注解。
在我的代码中,我已经应用了它,但它仍然不起作用。
静止控制器:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.validation.constraints.Size;

@RestController
@RequestMapping("/api")
public class UserController {

@PostMapping(value = "/{user_id}", produces = "application/json")
public ResponseEntity<CreateUserResponse> createUser (@RequestHeader(value = "correleation_id") String correlationId,
                                                              @PathVariable(name = "user_id")
                                                              @Size(max = 100) String userId,
                                                              @RequestBody @Valid CreateUserRequest createUserRequest) {

      CreateUserResponse response = myServiceCall(correlationId, userId, createUserRequest);
      return new ResponseEntity<>(response, HttpStatus.OK);
  }
}

CreateUserRequest.java

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Data
@AllArgsConstructor
@NoArgsConstructor
class CreateUserRequest {

  @NotNull(message = "name must not be empty")
  @JsonProperty("name")
  private String name;

  @NotNull(message = "user_detail must not be empty")
  @Valid
  @JsonProperty("user_detail")
  private UserDetail userDetail;
}

在请求中,如果我错过了name属性,调用仍然会转到服务方法。

fkaflof6

fkaflof61#

在Pom.xml中添加以下提到的依赖项。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>3.0.3</version>
</dependency>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

在控制器中:

@RestController
@RequestMapping("/api")
public class UserController {

@PostMapping(value = "/{user_id}", produces = "application/json")
public ResponseEntity<CreateUserResponse> createUser (@RequestHeader(value = "correleation_id") String correlationId,
                                                              @PathVariable(name = "user_id")
                                                              @Size(max = 100) String userId,
                                                              @Valid @RequestBody CreateUserRequest createUserRequest) { // See what is the change

      CreateUserResponse response = myServiceCall(correlationId, userId, createUserRequest);
      return new ResponseEntity<>(response, HttpStatus.OK);
  }
}

更新您的项目,然后运行您的项目。它将工作正常。

bcs8qyzn

bcs8qyzn2#

change one
 @RequestBody @Validated CreateUserRequest createUserRequest

 change two 
 @NotBlank(message = "name must not be empty")
 @JsonProperty("name")
  private String name;

相关问题