ovi2ut
ovi2ut

Reputation: 197

HTTP status code 500 for NotFoundException

I'm developing an application with RESTEasy and JBOSS 5.1.

For specific situations, I have to return 404 error (not found).

In the sources, I'm using

import org.jboss.resteasy.spi.NotFoundException;
throw new NotFoundException(...);

The problem is that, in the header response, I have Status Code: 500 internal server error

even if in the body the exception is:

org.jboss.resteasy.spi.UnhandledException: org.jboss.resteasy.spi.NotFoundException

This is a normal behavior? It's not possible to return Status Code: 404?

Upvotes: 2

Views: 4984

Answers (4)

jiafu
jiafu

Reputation: 6546

I encounter some problem. I found the root cause. The built-in exception handle is only occur in resteasy newest version build 2.3.1 GA. If you upgrade to this version.You can get the expected result.

Upvotes: 2

eiden
eiden

Reputation: 2349

It does seem a bit strange that RestEASY does not handle the NotFoundException out of the box. It should, according to the docs:

Resteasy has a set of built-in exceptions that are thrown by it when it encounters errors during dispatching or marshalling.

Anyways, you can work around it by adding an ExceptionMapper:

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.spi.NotFoundException;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;

public class ExceptionTest {

    @Path("/")
    public static class Service {
        @GET
        public String notFound() throws NotFoundException {
            throw new NotFoundException("");
        }
    }

    public static class FailureExceptionMapper implements ExceptionMapper<NotFoundException> {

        @Override
        public Response toResponse(NotFoundException exception) {
            return Response.status(exception.getErrorCode()).build();
        }
    }

    @Test
    public void test() throws Exception {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getProviderFactory().addExceptionMapper(new FailureExceptionMapper());
        dispatcher.getRegistry().addSingletonResource(new Service());


        MockHttpRequest request = MockHttpRequest.get("/");
        MockHttpResponse response = new MockHttpResponse();

        dispatcher.invoke(request, response);

        Assert.assertEquals(404, response.getStatus());


    }
}

Upvotes: 2

Vadzim
Vadzim

Reputation: 26180

Maybe a custom javax.servlet.Filter can help.

Upvotes: 0

groo
groo

Reputation: 4448

I believe that instead of throwing an exception you should use:

import javax.ws.rs.core.Response;
return Response.status(404).build();

in your rest method when you need to return a not found.

regards.

Upvotes: 1

Related Questions