Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

spring boot - Jersey returns 404 with any error status code?

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:

Expected status code <409> doesn't match actual status code <404>.

This happens in more codes: 400, 500... apart from 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.

What is happening?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

We can change this behavior by setting the Jersey property

ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR

You can do this in your ResourceConfig subclass

public JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
    }
}

Or (with Spring Boot) you can add it in your application.properties file.

spring.jersey.init.jersey.config.server.response.setStatusOverSendError=true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...