首先,我所做的是进行全局搜索server.error.include-binding-errors
在图书馆,这引导我到spring-configuration-metadata.json
{
"name": "server.error.include-binding-errors",
"type": "org.springframework.boot.autoconfigure.web.ErrorProperties$IncludeAttribute",
"description": "When to include \"errors\" attribute.",
"sourceType": "org.springframework.boot.autoconfigure.web.ErrorProperties",
"defaultValue": "never"
},
我们看到相关的属性是errors
这里,值类是IncludeAttribute
。通过检查文档IncludeAttribute#ON_PARAM https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/web/ErrorProperties.IncludeAttribute.html#ON_PARAM
公共静态最终ErrorProperties.includeAttribute ON_PARAM
当适当的请求参数不为“false”时添加错误属性。
我们知道errors
当请求参数不为“false”时,将添加该属性。
如果您想要具体的东西,让我们考虑以下示例:
控制器
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
public class TestController {
@PostMapping("/dummy")
public String test(@Valid @RequestBody DummyDTO dummyDTO) {
return "";
}
}
DTO
import javax.validation.constraints.NotNull;
public class DummyDTO {
@NotNull(message = "mandatoryText can not be null")
private String mandatoryText;
private String canBeNullText;
public String getMandatoryText() {
return mandatoryText;
}
public void setMandatoryText(String mandatoryText) {
this.mandatoryText = mandatoryText;
}
public String getCanBeNullText() {
return canBeNullText;
}
public void setCanBeNullText(String canBeNullText) {
this.canBeNullText = canBeNullText;
}
}
假设我们设置
server.error.include-binding-errors=on-param
当我们带参数运行时错误=假
curl -H "Content-Type: application/json" --data '{"canBeNullText":""}' -X POST http://localhost:8080/dummy?errors=false
结果将不包括errors
{
"timestamp": "2021-06-11T13:38:29.868+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/dummy"
}
当我们带参数运行时错误=真
curl -H "Content-Type: application/json" --data '{"canBeNullText":""}' -X POST http://localhost:8080/dummy?errors=true
结果将包括errors
as
{
"timestamp": "2021-06-11T13:51:00.649+00:00",
"status": 400,
"error": "Bad Request",
"errors": [{
"codes": ["NotNull.dummyDTO.mandatoryText", "NotNull.mandatoryText", "NotNull.java.lang.String", "NotNull"],
"arguments": [{
"codes": ["dummyDTO.mandatoryText", "mandatoryText"],
"arguments": null,
"defaultMessage": "mandatoryText",
"code": "mandatoryText"
}
],
"defaultMessage": "mandatoryText can not be null",
"objectName": "dummyDTO",
"field": "mandatoryText",
"rejectedValue": null,
"bindingFailure": false,
"code": "NotNull"
}
],
"path": "/dummy"
}
参考:
Web 响应式的实现
DefaultErrorWebExceptionHandler#isIncludeBindingErrors https://github.com/spring-projects/spring-boot/blob/v2.5.0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandler.java#L224
AbstractErrorWebExceptionHandler#isBindingErrorsEnabled https://github.com/spring-projects/spring-boot/blob/v2.5.0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java#L214
Web servlet 的实现
BaseErrorController#isIncludeBindingErrors https://github.com/spring-projects/spring-boot/blob/v2.5.0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/BasicErrorController.java#L167
AbstractErrorController#isBindingErrorsEnabled https://github.com/spring-projects/spring-boot/blob/v2.5.0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/error/AbstractErrorController.java#L85