Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS service throwing a 404 HTTPException but client receiving a HTTP 500 code

Tags:

java

jax-rs

I have a RESTful resource, which calls a EJB to make a query. If there is no result from the query, the EJB throws a EntityNotFoundException. In the catch block, it will be thrown a javax.xml.ws.http.HTTPException with code 404.

@Stateless
@Path("naturezas")
public class NaturezasResource {

    @GET
    @Path("list/{code}")
    @Produces(MediaType.APPLICATION_JSON)
    public String listByLista(
            @PathParam("code") codeListaNaturezasEnum code) {
        try {
            List<NaturezaORM> naturezas = this.naturezaSB
                    .listByListaNaturezas(code);
            ObjectMapper mapper = new ObjectMapper();
            return mapper.writeValueAsString(naturezas);
        } catch (EntityNotFoundException e) { // No data found
            logger.error("there is no Natures with the code " + code);
            throw new HTTPException(404);
        } catch (Exception e) { // Other exceptions
            e.printStackTrace();
            throw new HTTPException(500);
        }
    }

}

When I call the Rest Service with a code for which there are no results, the log message inside the EntityNotFoundException catch block is printed. However, my client receives a HTTP code 500 instead a 404. Why am I not receiving a 404 code?

Thanks,

Rafael Afonso

like image 995
Rafael Afonso Avatar asked Dec 24 '22 20:12

Rafael Afonso


1 Answers

javax.xml.ws.http.HTTPException is for JAX-WS. JAX-RS by default doesnt know how to handle it unless you write an ExceptionMapper for it. So the exception bubbles up to the container level, which just sends a generic internal server error response.

Instead use WebApplicationException or one of its subclasses. Here a list of the exceptions included in the hierarchy, and what they map to (Note: this is only in JAX-RS 2)

Exception                      Status code    Description
-------------------------------------------------------------------------------
BadRequestException            400            Malformed message
NotAuthorizedException         401            Authentication failure
ForbiddenException             403            Not permitted to access
NotFoundException              404            Couldn’t find resource
NotAllowedException            405            HTTP method not supported
NotAcceptableException         406            Client media type requested 
                                                            not supported
NotSupportedException          415            Client posted media type 
                                                            not supported
InternalServerErrorException   500            General server error
ServiceUnavailableException    503            Server is temporarily unavailable 
                                                            or busy

You can find them also in the WebApplicationException link above. They will fall under one of the direct subclasses ClientErrorException, RedirectionException, or ServerErrorException.

With JAX-RS 1.x, this hierarchy doesn't exist, so you would need to do something like @RafaelAlfonso showed in a comment

throw new WebApplicationException(Response.Status.NOT_FOUND);

There are a lot of other possible constructors. Just look at the API link above

like image 97
Paul Samsotha Avatar answered May 06 '23 02:05

Paul Samsotha