简体   繁体   English

Spring 不返回默认验证错误响应

[英]Spring not returning default validation error responses

I'm trying to get the default Spring 400 constrain violation errors working.我正在尝试使默认的 Spring 400 约束冲突错误正常工作。 I'm expecting this:我期待这个:

{
    ....
    "status": 400,
    "error": "Bad Request",
    "errors": [
        {
            ....
            "defaultMessage": "must not be null",
            ....
        }
    ],
    "message": "Validation failed for object='notNullRequest'. Error count: 1",
    ....
}

But I get:但我得到:

{
    "timestamp": "2020-07-31T08:30:06.992+00:00",
    "status": 400,
    "error": "Bad Request",
    "message": "",
    "path": "/v0.1/checkouts/1"
}

My POM:我的 POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>hidden</groupId>
    <artifactId>hidden</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hidden</name>
    <description>hidden</description>

    <properties>
        <java.version>11</java.version>
        <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.12</org.projectlombok.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <repositories>
        <repository>
            <id>hidden</id>
            <name>hidden</name>
            <url>hidden</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${org.projectlombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

REST Resource I'm trying to validate:我正在尝试验证的 REST 资源:

@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CheckoutDto> createCheckout(@PathVariable Long id,
                                                  @Valid @RequestBody CheckoutProcessRequest request) {

The DTO has validations like: DTO 具有以下验证:

@NotNull
@JsonProperty("payment_type")
private PaymentType paymentType;

And I get a proper message in the log:我在日志中收到一条正确的消息:

2020-07-31 11:30:06.983  WARN 93126 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [1] in public org.springframework.http.ResponseEntity<com.hidden.payments.rest.model.CheckoutDto> com.hidden.payments.rest.CheckoutsResource.createCheckout(java.lang.Long,com.hidden.payments.rest.model.CheckoutProcessRequest): [Field error in object 'checkoutProcessRequest' on field 'paymentType': rejected value [null]; codes [NotNull.checkoutProcessRequest.paymentType,NotNull.paymentType,NotNull.com.hidden.payments.rest.model.PaymentType,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [checkoutProcessRequest.paymentType,paymentType]; arguments []; default message [paymentType]]; default message [must not be null]] ]

So any ideas what could interfering with the response?那么有什么想法会干扰响应吗? I have no recollection of ever having to do anything specific to get the proper constraint violation responses in latest Spring versions.我不记得在最新的 Spring 版本中必须做任何特定的事情来获得正确的约束违规响应。

Thanks!谢谢!

Spring Boot 2.3 omits the errors and message fields in error responses by default to reduce the chance that sensitive information is included in a response. Spring Boot 2.3 默认省略错误响应中的errorsmessage字段,以减少响应中包含敏感信息的机会。 You can restore the previous behavior by setting the following properties:您可以通过设置以下属性来恢复以前的行为:

server.error.include-message=always
server.error.include-binding-errors=always

See the 2.3 release notes and documentation for more information.有关详细信息,请参阅 2.3 发行说明文档

Sprint Boot 2.3 has issues with validation messages, or behaviour changed. Sprint Boot 2.3 存在验证消息问题或行为改变。 Duplicated question under Size Annotation in Spring Boot is returning 400 Bad Request Spring Boot 中大小注释下的重复问题返回 400 Bad Request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM