我在路径“/test”中有这个无用的端点:
@PUT
public Response doSomething() {
return Response.status(409).build();
}
我这样测试:
@Test
public void uselessTest() {
put("/test").then().assertThat().statusCode(409);
}
但是我得到一个断言错误:
Expected status code <409> doesn't match actual status code <404>.
这发生在更多代码中:除了200,还有400、500...。
我正在使用Spring Boot。如果在运行测试时在端点方法中设置了断点,则执行将在此处停止,因此测试中的请求已正确完成。如果我将状态代码(也在资源和测试中)更改为200,则测试通过。
怎么了?
最佳答案
当存在错误状态(4xx,5xx)时,Jersey的默认行为是使用servlet的Response.sendError
,这会导致重定向到错误页面。由于未设置错误页面,因此结果为404。
我们可以通过设置Jersey属性来更改此行为
您可以在
ResourceConfig
子类中执行此操作public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
}
}
或者(使用Spring Boot)可以将其添加到
application.properties
文件中。spring.jersey.init.jersey.config.server.response.setStatusOverSendError=true
关于spring-boot - Jersey返回404并带有任何错误状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597274/