Jersey返回404并带有任何错误状态代码? [英] Jersey returns 404 with any error status code?
问题描述
我在路径"/test"中有这个无用的端点:
I have this useless endpoint in path "/test":
@PUT
public Response doSomething() {
return Response.status(409).build();
}
我以这种方式对其进行测试:
and I test it this way:
@Test
public void uselessTest() {
put("/test").then().assertThat().statusCode(409);
}
但是我得到一个断言错误:
But I get an assertion error:
期望的状态码< 409>与实际的状态码< 404>不匹配.
Expected status code <409> doesn't match actual status code <404>.
这种情况发生在更多代码中:除了200之外还有400、500 ...
This happens in more codes: 400, 500... apart from 200.
我正在使用Spring Boot.如果在运行测试时在端点方法中设置了断点,则执行将在此处停止,因此测试中的请求已正确完成.如果我将状态代码(在资源和测试中)也更改为200,则测试通过.
I'm using Spring Boot. If I put a breakpoint in my endpoint method when I run the test, execution stops there, so the request in the test is done properly. If I change the status code (in resource and in the test, too) to 200, test passes.
发生了什么事?
推荐答案
当出现错误状态(4xx,5xx)时,Jersey的默认行为是使用Servlet的Response.sendError
,这会导致重定向到错误页面.由于未设置错误页面,因此结果为404.
The default behavior with Jersey, when there is an error status (4xx, 5xx), is to use the servlet's Response.sendError
, which results in a redirect to an error page. Since there is no error page set up, it results in a 404.
我们可以通过设置Jersey属性来更改此行为
We can change this behavior by setting the Jersey property
您可以在您的ResourceConfig
子类中
public JerseyConfig extends ResourceConfig {
public JerseyConfig() {
property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
}
}
或者(使用Spring Boot),您可以将其添加到application.properties
文件中.
Or (with Spring Boot) you can add it in your application.properties
file.
spring.jersey.init.jersey.config.server.response.setStatusOverSendError=true
这篇关于Jersey返回404并带有任何错误状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!