ResponseEntityExceptionHandler在Spring Boot中由于验证错误而禁用格式化的响应正文

时间:2019-02-21 14:12:26

标签: java rest validation spring-boot

在我的Spring Boot HTTP Rest GET服务中,我在体内拥有此属性:

@NotNull
@Size(min = 2)
private List<Dto> dtos = new ArrayList<Dto>();

当列表只有一个条目时,会出现此错误:

{
  "timestamp": "2019-02-21T14:05:41.968+0000",
  "status": 400,
  "error": "Bad Request",
  "errors": [
    {
      "codes": [
        "Size.baseDto.dtos",
        "Size.dtos",
        "Size.java.util.List",
        "Size"
      ],
      "arguments": [
        {
          "codes": [
            "baseDto.dtos",
            "dtos"
          ],
          "arguments": null,
          "defaultMessage": "dtos",
          "code": "dtos"
        },
        2147483647,
        2
      ],
      "defaultMessage": "has to be between 2 and 2147483647",
      "objectName": "baseDto",
      "field": "dtos",
      "rejectedValue": [
        {
          "coordinates": {
            "latitude": 0.0,
            "longitude": 0.0
          }
        }
      ],
      "bindingFailure": false,
      "code": "Size"
    }
  ],
  "message": "Validation failed for object='baseDto'. Error count: 1",
  "path": "/api/v1/transports"
}

但是我添加

@ControllerAdvice
public class ExceptionMapper extends ResponseEntityExceptionHandler {

该错误正文不再返回。即使ExceptionMapper没有实现,也不会返回任何正文。

即使使用自定义ResponseEntityExceptionHandler,如何保持验证?

0 个答案:

没有答案