我在路径"/ test"中有这个无用的端点:
@PUT
public Response doSomething() {
return Response.status(409).build();
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式测试它:
@Test
public void uselessTest() {
put("/test").then().assertThat().statusCode(409);
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个断言错误:
预期状态代码<409>与实际状态代码<404>不匹配.
这种情况发生在更多代码中:400,500 ...除了200.
我正在使用Spring Boot.如果我在运行测试时在端点方法中放置断点,则执行停止,因此测试中的请求正确完成.如果我将状态代码(在资源和测试中)也更改为200,则测试通过.
怎么了?
我正在使用@ ControllerAdvice,@ ErrorHandler和@ResponseStatus批注返回一些错误信息。我确定已执行处理程序方法(已在调试器中检查了它。)但是Tomcat HTML错误页面覆盖了我的ErrorInfo对象。
@ExceptionHandler(value = ServiceExecutionException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Internal Server Error")
ErrorInfo handleServiceError(HttpServletRequest request, HttpServletResponse response, Exception e) {
return new ErrorInfo(request.getRequestURL().toString(), e.getLocalizedMessage());
}
Run Code Online (Sandbox Code Playgroud)
这是一个类似的问题,但是它没有包含正确的答案,因为我试图避免使我的代码复杂化。 禁用Tomcat中的所有默认HTTP错误响应内容
我们有一个使用Spring Boot构建的Web应用程序,并且正在使用Spring Security来管理身份验证。身份验证现在正在使用基本模式进行。
我们正在使用UserDetailsService处理身份验证。当我们使用嵌入式tomcat直接从STS运行应用程序时,如果未启用用户,则会收到状态代码为401的以下错误。
{"timestamp":1485512173312,"status":401,"error":"Unauthorized","message":"User is disabled","path":"/trex/user"}
Run Code Online (Sandbox Code Playgroud)
但是,当我将应用程序打包打包并部署到Tomcat时,我们不再收到json错误。而是,我们收到类似于以下内容的html错误。
<html><head><title>Apache Tomcat/7.0.75 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 401 - User is disabled</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>User is disabled</u></p><p><b>description</b> <u>This request requires HTTP authentication.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.75</h3></body></html>
Run Code Online (Sandbox Code Playgroud)
是否可以停止处理标准错误代码,并让json数据按原样传递回去?
编辑: 春季安全配置
/**
* Security configuration. Defines configuration methods and security
* constraints. Autowired bean definitions can be found in …Run Code Online (Sandbox Code Playgroud) error-handling spring-mvc spring-security tomcat7 spring-boot
spring-boot ×2
spring-mvc ×2
java ×1
jersey ×1
jersey-2.0 ×1
rest ×1
rest-assured ×1
tomcat ×1
tomcat7 ×1
tomcat8 ×1