Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grizzly Jersey swallowing exceptions

I'm building a Jersey Moxy service using the quickstart archetype at the end. My code works fine and I can get some JSON returned. However as I'm developing, if I make a mistake, say the request handler has an unsupported type, I will get an empty 500 response, which makes debugging difficult. For example, if I decorate an attribute incorrectly with @XmlElementRef, I will get a response like:

$ curl -i http://localhost:8080/myapp/test
HTTP/1.1 500 Internal Server Error
Date: Thu, 05 Sep 2013 10:27:55 GMT
Connection: close
Content-Length: 0

The server will act as if nothing is wrong:

Sep 5, 2013 11:27:46 AM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:8080/application.wadl
Hit enter to stop it...

I tried using a log config file with:

-Djava.util.logging.config.file=log.conf

This produces plenty of output, but still doesn't show any kind of exception.

I've tried looking into Grizzly config but I can't find a way turn off graceful error handling. Ideally I would like the server to throw an exception. Any suggestions on what I'm missing?

Here's my main code:

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.moxy.json.MoxyJsonConfig;
import org.glassfish.jersey.server.ResourceConfig;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import java.io.IOException;
import java.net.URI;
import java.util.*;

public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://localhost:8080/";

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
  * @return Grizzly HTTP server.
   */
   public static HttpServer startServer() {
       // create a resource config that scans for JAX-RS resources and providers
       // in com.myapp package
       final ResourceConfig rc = new ResourceConfig().packages("com.myapp").registerInstances(new JsonMoxyConfigurationContextResolver());

       // create and start a new instance of grizzly http server
       // exposing the Jersey application at BASE_URI
       return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
   }

   /**
    * Main method.
     * @param args
  * @throws IOException
   */
   public static void main(String[] args) throws IOException {
       final HttpServer server = startServer();
       System.out.println(String.format("Jersey app started with WADL available at "
               + "%sapplication.wadl\nHit enter to stop it...", BASE_URI));
       System.in.read();
       server.stop();
   }

   @Provider
   final static class JsonMoxyConfigurationContextResolver implements ContextResolver<MoxyJsonConfig> {

       @Override
       public MoxyJsonConfig getContext(Class<?> objectType) {
           final MoxyJsonConfig configuration = new MoxyJsonConfig();

           Map<String, String> namespacePrefixMapper = new HashMap<String, String>(1);
           namespacePrefixMapper.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");

           configuration.setNamespacePrefixMapper(namespacePrefixMapper);
           configuration.setNamespaceSeparator(':');

           return configuration;
       }
   }
}

The code is almost identical to the example here:

https://github.com/jersey/jersey/tree/2.2/examples/json-moxy/src/main/java/org/glassfish/jersey/examples/jsonmoxy

Full archetype generation I used:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.myapp -DartifactId=yarese-service -Dpackage=com.myapp \
-DarchetypeVersion=2.2

Suggestions gratefully received.

like image 659
Paul Avatar asked Sep 05 '13 10:09

Paul


1 Answers

The exception is not getting propagated to the Grizzly layer, so it should be logged by Jersey. I haven't found what kind of Logger you have to enable, but looks like custom ExceptionMapper could help.

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.glassfish.grizzly.utils.Exceptions;

@Provider
public class MyExceptionMapper  implements
        ExceptionMapper<WebApplicationException> {
    @Override
    public Response toResponse(WebApplicationException ex) {
        return Response.status(500).entity(Exceptions.getStackTraceAsString(ex)).type("text/plain")
                .build();
    }
}
like image 97
alexey Avatar answered Sep 18 '22 14:09

alexey