gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:16:55 26 4
gpt4 key购买 nike

我正在尝试使默认的 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:

<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 [paymentType]]; default message [must not be null]] ]

那么有什么想法会干扰响应吗?我不记得曾经需要做任何特定的事情来在最新的 Spring 版本中获得正确的约束违规响应。

谢谢!

最佳答案

Spring Boot 2.3 默认省略错误响应中的 errorsmessage 字段,以减少响应中包含敏感信息的可能性。您可以通过设置以下属性来恢复以前的行为:

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

参见 2.3 release notesdocumentation获取更多信息。

关于java - Spring 不返回默认验证错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63188531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com