Spring中的server.error.include-binding-errors=on-param有什么作用?

Lii*_*Lii 16 spring spring-boot spring-autoconfiguration

application.propertiesSpring 应用程序中的以下设置有何作用?

server.error.include-binding-errors=on-param
Run Code Online (Sandbox Code Playgroud)

我在文档中找不到它。

alwaysnever非常不言自明,但我不明白on-param

sam*_*cde 20

首先,我所做的是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"
    },
Run Code Online (Sandbox Code Playgroud)

我们看到相关的属性在errors这里,值类是IncludeAttribute通过检查IncludeAttribute#ON_PARAM中的文档

public static final ErrorProperties.IncludeAttribute ON_PARAM
当适当的请求参数不为“false”时添加错误属性。

我们知道,errors当有请求参数不为“false”时,就会添加该属性。


如果您想要具体的东西,让我们考虑以下示例:

控制器

    {
      "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"
    },
Run Code Online (Sandbox Code Playgroud)

数据传输组织

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 "";
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我们设置
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
Run Code Online (Sandbox Code Playgroud)

结果将不包括errors

{
    "timestamp": "2021-06-11T13:38:29.868+00:00",
    "status": 400,
    "error": "Bad Request",
    "message": "",
    "path": "/dummy"
} 
Run Code Online (Sandbox Code Playgroud)

当我们使用参数errors=true运行时

curl -H "Content-Type: application/json" --data '{"canBeNullText":""}' -X POST http://localhost:8080/dummy?errors=true
Run Code Online (Sandbox Code Playgroud)

结果将包括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"
}

Run Code Online (Sandbox Code Playgroud)

参考文献:
Web响应式的实现
DefaultErrorWebExceptionHandler#isIncludeBindingErrors
AbstractErrorWebExceptionHandler#isBindingErrorsEnabled

Web Servlet 的实现
BaseErrorController#isIncludeBindingErrors
AbstractErrorController#isBindingErrorsEnabled

  • 您对“server.error.include-message:on_param”和?message = true有相同的行为。我认为使用它比错误更好。它不会返回所有信息。正是您想要的消息。例如,对于 404 错误,您可以使用 ` throw new ResponseStatusException(HttpStatus.NOT_FOUND, "No resource");` 如果端点不存在,则 spring 返回“message: “No message available”。这样您就可以轻松区分2例。 (2认同)