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();
}
我是这样测试的:
@Test
public void uselessTest() {
put("/test").then().assertThat().statusCode(409);
}
但我收到断言错误:
预期状态代码 <409> 与实际状态代码 <404> 不匹配.
Expected status code <409> doesn't match actual status code <404>.
这发生在更多代码中:400、500...除了 200.
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
ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR
您可以在 ResourceConfig
子类中执行此操作
You can do this in your ResourceConfig
subclass
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屋!