gpt4 book ai didi

java - @Valid @RequestBody 无法验证输入错误的传入 Json

转载 作者:行者123 更新时间:2023-12-01 17:44:39 27 4
gpt4 key购买 nike

我想验证传入的 RequestBody。我的 Controller API 是,

@RequestMapping(value = {
POSRequest.REQUEST_SEPARATOR + Request.UPDATE_URL,
method = RequestMethod.PUT,
produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Update .”,
response = SuccessResponseDTO.class, ,
produces = "application/json", httpMethod = "PUT")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK",response = SuccessResponseDTO.class),
@ApiResponse(code = 401, message = "Unauthorized",response =ErrorResponseDTO.class),
@ApiResponse(code = 403, message = "Forbidden",response = ErrorResponseDTO.class),
@ApiResponse(code = 404, message = "Not Found",response = ErrorResponseDTO.class)
})
public @ResponseBody
SuccessResponseDTO update(HttpServletRequest request,
@ApiParam(required = true, type = "String", value = " ") @RequestParam(value = “id”, required = true) String id,
@ApiParam(required = true, type = "String", value = " ") @RequestParam(value = "documentNumber", required = true) String documentNumber,
@ApiParam(required = true, type = "String", value = " ") @RequestParam(value = “Sid”, required = true) String sid,
@ApiParam(required = true, type = "UpdateDTO", value = "Update payload json ") @Valid @RequestBody UpdateDTO pdatePayload) throws IOException {
authorizeRequest(request, anId);
UpdateDTO posDTO = UpdatePayload;
UpdateAction<UpdateDTO> action = new UpdateAction(Constants.ACTION_UPDATE, DTO, principal);
action.addAdditionalParams(Constants.KEY_INPUT_DOC_NUMBER, documentNumber);
action.addAdditionalParams(Constants.KEY_INPUT_SUPPLIER_AN_ID, sid);
Gson gson = new Gson();
action.addAdditionalParams(Constants.CONTENT, gson.toJson(DTO));
return updateService.update(action);
}

我将注释@Valid@RequestBody一起使用,期望它能够验证输入JSON。用户发送非映射属性的无效输入 JSON 正在传递,API 只是忽略非映射字段,没有任何错误。

例如UpdateDTO 包含 String name、String status 字段的 settergetter

用户请求包含无效字段,

{
"name":"my update",
"invalid_field":"abc"
}

转换为UpdateDTO并且非映射字段将被忽略。我预计它会因输入无效而出错。

你能给我推荐一下吗?我怎样才能让它发挥作用?如果您需要任何意见,请告诉我

我已经包含了以下 Maven 依赖项,

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

最佳答案

您误解了@Valid的用法。 @Valid 注释用于验证带有 @NotNull@Size@Min 等约束的 bean根据 JSR 380 规范。例如,如果您想对 UpdateDTO 类的 name 字段进行验证,以使 name 永远不为空,您可以使用在 name 字段上使用 @NotNull,然后在 Controller 方法中的 UpdateDTO 对象上使用 @Valid。这将确保每当在未传递 name 字段的情况下发出请求时,验证都会失败并相应地抛出异常。

对于您希望在请求中发送未知字段时抛出异常的用例,由于您使用的是 spring-boot,它使用 Jackson 进行序列化/反序列化,因此您只需要提供一个配置来启用遇到未知属性时反序列化失败。这可以通过以下属性启用它来完成:

spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=true

或者通过定义一个@Bean如下:

@Bean
public ObjectMapper objectMapper() {
return Jackson2ObjectMapperBuilder
.json()
.featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
}

关于java - @Valid @RequestBody 无法验证输入错误的传入 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57139025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com