当使用MethodValidationPostProcessor时,JavaSpring返回404
我的测试Spring启动应用程序有问题。它工作得很好,但是当我通过添加依赖项等并添加@Configuration
来启用Spring验证时:
@Configuration
public class TestConfiguration {
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
我的测试端点得到了404
{
"timestamp": 1601507037178,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/test"
}
我已经应用了一些类似问题的解决方案/建议(例如here或here),但没有成功
这是我的密码: https://github.com/zolv/error-handling-test
API接口:
@Validated
public interface TestApi {
@PostMapping(
value = "/test",
produces = {"application/json"},
consumes = {"application/json"})
@ResponseBody
ResponseEntity<TestEntity> getTest(@Valid @RequestBody(required = false) TestEntity request);
}
TestEntity
只是想发送一些东西:
@Data
public class TestEntity {
@JsonProperty("test")
@NotNull
private String test;
}
控制器实现:
@RestController
@RequiredArgsConstructor
@Validated
public class TestController implements TestApi {
@Override
@ResponseBody
public ResponseEntity<TestEntity> getTest(@Valid @RequestBody TestEntity request) {
return ResponseEntity.ok(request);
}
}
我的忠告是:
@ControllerAdvice
public class DefaultErrorHandlerAdvice extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = {ConstraintViolationException.class})
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ResponseBody
public ResponseEntity<String> handleValidationFailure(ConstraintViolationException ex) {
StringBuilder messages = new StringBuilder();
for (ConstraintViolation<?> violation : ex.getConstraintViolations()) {
messages.append(violation.getMessage());
}
return ResponseEntity.badRequest().body(messages.toString());
}
@Override
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.body("problem");
}
}
应用程序:
@SpringBootApplication
@EnableWebMvc
@ComponentScan(basePackages = "com.test")
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
我使用的测试,但使用Postman时也失败:
@SpringJUnitConfig
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestControllerTest {
@Autowired protected TestRestTemplate restTemplate;
@Test
void registrationHappyPath() throws Exception {
/*
* Given
*/
final TestEntity request = new TestEntity();
/*
* When/
*/
final ResponseEntity<String> response =
restTemplate.postForEntity("/test", request, String.class);
/*
* Then
*/
Assertions.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
final String body = response.getBody();
Assertions.assertNotNull(body);
}
}
如果我注释掉一个TestConfiguration
,那么一切都正常。
提前感谢您的帮助
# 1 楼答案
您应该设置
MethodValidationPostProcessor#setProxyTargetClass(true)
,因为默认情况下MethodValidationPostProcessor
使用JDK代理,这会导致在Spring上下文中丢失控制器当{cd5}被调用时,{cd5}将返回{cd4^>