英文:
Spring not returning default validation error responses
问题
我试图让默认的Spring 400约束违规错误起作用。
我期望得到以下结果:
{
....
"status": 400,
"error": "Bad Request",
"errors": [
{
....
"defaultMessage": "must not be null",
....
}
],
"message": "Validation failed for object='notNullRequest'. Error count: 1",
....
}
但我得到了:
{
"timestamp": "2020-07-31T08:30:06.992+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/v0.1/checkouts/1"
}
我的POM:
<?xml version="1.0" encoding="UTF-8"?>
<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资源:
@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CheckoutDto> createCheckout(@PathVariable Long id,
@Valid @RequestBody CheckoutProcessRequest request) {
DTO有类似的验证:
@NotNull
@JsonProperty("payment_type")
private PaymentType paymentType;
而且我在日志中得到了正确的消息:
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
<details>
<summary>英文:</summary>
I'm trying to get the default Spring 400 constrain violation errors working.
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:
```<?xml version="1.0" encoding="UTF-8"?>
<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:
@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<CheckoutDto> createCheckout(@PathVariable Long id,
@Valid @RequestBody CheckoutProcessRequest request) {
The DTO has validations like:
@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.
Thanks!
答案1
得分: 17
Spring Boot 2.3默认情况下在错误响应中省略了errors
和message
字段,以降低包含敏感信息的响应的机会。您可以通过设置以下属性来恢复先前的行为:
server.error.include-message=always
server.error.include-binding-errors=always
英文:
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. 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
得分: 1
Sprint Boot 2.3存在验证消息问题,或者行为发生了变化。
在https://stackoverflow.com/questions/63110587/size-annotation-in-spring-boot-is-returning-400-bad-request/63187267#63187267下重复提出的问题。
英文:
Sprint Boot 2.3 has issues with validation messages, or behaviour changed.
Duplicated question under
https://stackoverflow.com/questions/63110587/size-annotation-in-spring-boot-is-returning-400-bad-request/63187267#63187267
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论