1

I'm using Jersey 1.19, Java 1.6, and Tomcat 5.5.28 (which is Servlet 2.4).

The code is similar to this example:

package com.mkyong.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mkyong.Track;

@Path("/json/metallica")
public class JSONService {

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public Track getTrackInJSON(@Context HttpServletRequest request) {

        if (request.getRequestedSessionId() == null ||
            request.getSession(false) == null) {

            // return to caller with an HTTP 404 error
            // code goes here
        }
        else {

            Track track = new Track();
            track.setTitle("Enter Sandman");
            track.setSinger("Metallica");

            return track;
        }
    }
}

The class returns JSON and that's what the caller is expecting. But if I wanted to perform some validation like a bad or non-existent session and return an HTTP 404 error instead, how is this done?

Thank you for any help.

user3621633
  • 1,371
  • 3
  • 25
  • 38

2 Answers2

1

You can use HttpServletResponse to set status code.

    ...

@Path("/json/metallica")
public class JSONService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON(@Context HttpServletRequest request, HttpServletResponse response) {

    if (request.getRequestedSessionId() == null ||
        request.getSession(false) == null) {
        response.setStatus(404);
        // code goes here
    }
    else {

        Track track = new Track();
        track.setTitle("Enter Sandman");
        track.setSinger("Metallica");

        return track;
    }
}
}
Hare Kumar
  • 689
  • 7
  • 23
  • I appreciate the help, thank you. Unfortunately, that didn't work. It won't compile without specifying a return value. Even when I specified a return value of `null`, the response set in the browser was 204 (No Content), not 404. – user3621633 Jul 23 '15 at 18:04
1

Since you're using JAX-RS, my preferred approach is to throw an exception and have an exception mapper catch it and return a response with desired response code.

So your resource class would be:

@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Track getTrackInJSON(@Context HttpServletRequest request) {

    if (request.getRequestedSessionId() == null ||
        request.getSession(false) == null) {
        throw new IllegalStateException("NOT_FOUND");
    }
}

And then create an exception mapper class:

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class IllegalStateExceptionMapper implements ExceptionMapper<IllegalStateException> {

    @Override
    public Response toResponse(final IllegalStateException exception) {
        if ("NOT_FOUND".equals(exception.getMessage()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    }
}

You can throw your own exception instead of a generic IllegalStateException, but the gist is the same.

Ibrahim Arief
  • 7,840
  • 4
  • 32
  • 53
  • Yeah, this worked quite well actually. Thank you! And the article: http://stackoverflow.com/questions/583973/jax-rs-jersey-how-to-customize-error-handling%3E also was of great, great help. So great. – user3621633 Jul 24 '15 at 19:17