Spring 中的 server.error.include-binding-errors=on-param 是做什么的?
What does server.error.include-binding-errors=on-param in Spring do?
Spring 应用程序中 application.properties
中的以下设置有什么作用?
server.error.include-binding-errors=on-param
我在文档中找不到它。
always
和 never
值是不言自明的,但我不明白 on-param
。
首先,我在图书馆中对 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
,值class是IncludeAttribute
。通过查看 IncludeAttribute#ON_PARAM
中的文档
public static final ErrorProperties.IncludeAttribute ON_PARAM
Add error attribute when the appropriate request parameter is not "false".
我们知道,当有一个请求参数不是"false"时,就会添加errors
属性。
如果你想要具体的东西,让我们考虑下面的例子:
控制器
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
当我们运行带参数errors=false
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"
}
当我们运行带参数errors=true
curl -H "Content-Type: application/json" --data '{"canBeNullText":""}' -X POST http://localhost:8080/dummy?errors=true
结果将包括 errors
和
{
"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"
}
参考文献:
网络反应的实现
DefaultErrorWebExceptionHandler#isIncludeBindingErrors
AbstractErrorWebExceptionHandler#isBindingErrorsEnabled
web servlet 的实现
BaseErrorController#isIncludeBindingErrors
AbstractErrorController#isBindingErrorsEnabled
Spring 应用程序中 application.properties
中的以下设置有什么作用?
server.error.include-binding-errors=on-param
我在文档中找不到它。
always
和 never
值是不言自明的,但我不明白 on-param
。
首先,我在图书馆中对 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
,值class是IncludeAttribute
。通过查看 IncludeAttribute#ON_PARAM
public static final ErrorProperties.IncludeAttribute ON_PARAM
Add error attribute when the appropriate request parameter is not "false".
我们知道,当有一个请求参数不是"false"时,就会添加errors
属性。
如果你想要具体的东西,让我们考虑下面的例子:
控制器
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
当我们运行带参数errors=false
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"
}
当我们运行带参数errors=true
curl -H "Content-Type: application/json" --data '{"canBeNullText":""}' -X POST http://localhost:8080/dummy?errors=true
结果将包括 errors
和
{
"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"
}
参考文献:
网络反应的实现
DefaultErrorWebExceptionHandler#isIncludeBindingErrors
AbstractErrorWebExceptionHandler#isBindingErrorsEnabled
web servlet 的实现
BaseErrorController#isIncludeBindingErrors
AbstractErrorController#isBindingErrorsEnabled