【问题标题】:What does server.error.include-binding-errors=on-param in Spring do?Spring 中的 server.error.include-binding-errors=on-param 有什么作用?
【发布时间】:2021-08-27 12:53:41
【问题描述】:

Spring 应用程序中application.properties 中的以下设置有什么作用?

server.error.include-binding-errors=on-param

我在文档中找不到。

alwaysnever 的值是不言自明的,但我不明白 on-param

【问题讨论】:

    标签: spring spring-boot spring-autoconfiguration


    【解决方案1】:

    首先,我所做的是在库中对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中的文档

    public static final ErrorProperties.IncludeAttribute ON_PARAM
    当相应的请求参数不是“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"
    }
    
    

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

    Web servlet 的实现
    BaseErrorController#isIncludeBindingErrors
    AbstractErrorController#isBindingErrorsEnabled

    【讨论】:

    • 感谢调查!我很难解释这一切意味着什么。你能从用户的角度解释ON_PARAM 的工作原理吗?例如:我的应用程序可以在ServerRequest 中设置"errors" 参数吗?怎么做?或者我的应用程序收到的 Web 请求是否包含 "errors" 参数?
    • @Lii,感谢您的评论,请查看我的更新是否让您清楚。
    • 那是一个非常彻底的调查,谢谢!编辑确实使这一切都非常清楚。 :)
    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2016-04-15
    • 2014-01-27
    • 2011-03-10
    相关资源
    最近更新 更多