lhahne
lhahne

Reputation: 6029

Jersey & Spring Boot turns non-ok response statuses to 404

I am using Jersey with Spring Boot 1.2.5. I have a Jersey controller (annotated with @Produces(MediaType.APPLICATION_JSON)) which works fine and returns JSON as long as I return an ok response such as

return Response.ok(dto).build();

but whenever I try to return a custom error status such as

return Response.status(Status.CONFLICT).build();

it gets turned into a 404 such as

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Error 404 Not Found</title>
</head>
<body>
    <h2>HTTP ERROR 404</h2>
    <p>Problem accessing /error. Reason:

        <pre>    Not Found</pre>
    </p>
    <hr>
        <i>
            <small>Powered by Jetty://</small>
        </i>
        <hr/>
    </body>
</html>

Any ideas what's going on here?

Upvotes: 1

Views: 561

Answers (1)

lhahne
lhahne

Reputation: 6029

Jersey expects the response to contain an entity. If I return an empty map together with my error code, the error gets passed onto the browser with an empty JSON object.

return Response.status(Status.CONFLICT).entity(new HashMap<>()).build();

If you have any better solutions, please feel free to comment.

Upvotes: 1

Related Questions