春季验证:根据条件@RequestParam自定义验证@RequestBody

时间:2019-05-28 16:42:22

标签: spring validation

我想使用@Valid注释验证请求正文。 该方法中有一个@RequestParam值。 如果此@RequestParam值为“ min”,则我希望requestbody中的字段为必填字段。可能有注释吗?

@RequestMapping(method = RequestMethod.POST, value = ProductionResponse.URL, produces = JWSMessageConverter.JWS_MEDIA_TYPE_VALUE)
@ResponseBody
public ResponseEntity<ProductionResponse> richiestaProduzione(@RequestParam("issuerType") String issuerType,
        @RequestParam("issuerCode") String issuerCode, @RequestParam("procedureId") String procedureId,
        @Valid @RequestBody ProductionRequestResource requestBody) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("POST " + ProductionResponse.URL);
  @Valid ProductionRequestResource fff = requestBody;
    requestBody.setProcedureResource(issuerType, issuerCode, procedureId);

    try {
        ProductionResponse response = produzioneService.richiestaProduzione(requestBody);
        return ResponseEntity.ok(response);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body(ProductionResponse.ko(requestBody, e.getMessage(), requestBody.getNun()));
    }
}

如果issuerType等于“ MIN”,我希望ProductionRequestResource中的字段issuerMunicipalityCode是强制性的。

1 个答案:

答案 0 :(得分:0)

要继续进行下去,这是实现自定义验证的方法。

添加一个验证,以在issueType为MIN并且市政代码为空/空时引发异常。

@RequestMapping(method = RequestMethod.POST, value = ProductionResponse.URL, produces = JWSMessageConverter.JWS_MEDIA_TYPE_VALUE)
@ResponseBody
public ResponseEntity<ProductionResponse> richiestaProduzione(@RequestParam("issuerType") String issuerType,
        @RequestParam("issuerCode") String issuerCode, @RequestParam("procedureId") String procedureId,
        @RequestBody ProductionRequestResource requestBody) {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("POST " + ProductionResponse.URL);

    if("MIN".equals(issuerType) && StringUtils.isEmpty(requestBody.getIssuerMunicipalityCode())) {
        throw new IllegalArgumentException("IssuerMuncipalityCode can't be null when IssuerType is MIN");
    }

    try {
        ProductionResponse response = produzioneService.richiestaProduzione(requestBody);
        return ResponseEntity.ok(response);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return ResponseEntity.badRequest().body(ProductionResponse.ko(requestBody, e.getMessage(), requestBody.getNun()));
    }
}

实施异常处理程序以捕获异常并根据异常类型构建响应主体。上面抛出的InvalidArgumentSuppliedException将被下面的异常处理程序捕获。异常处理程序是在单独的组件中处理异常的最佳实践。

@ControllerAdvice
public class ScnSchedulerExceptionHandler {

    @ExceptionHandler(InvalidArgumentSuppliedException.class)
    public final ResponseEntity<Object> handleInvalidArgException(Exception ex) {
        ErrorResponseDTO errorDTO = new ErrorResponseDTO();
        errorDTO.setMessage(ex.getMessage());
        return new ResponseEntity<>(errorDTO, HttpStatus.NOT_ACCEPTABLE);
    }

    @ExceptionHandler(Exception.class)
    public final ResponseEntity<Object> handleAllExceptions(Exception ex) {
        ErrorResponseDTO errorDTO = new ErrorResponseDTO();
        errorDTO.setMessage("Internal server error occurred.");
        return new ResponseEntity<>(errorDTO, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

在异常处理程序中,将错误消息包装在pojo对象中。

public class ErrorResponseDTO {

    private String errorMessage;

}