Grizzly jersey swallowing exceptions

I am building a Jersey Moxy service using the quickstart artifact at the end. My code works fine and I can return JSON. However, when I develop, if I am mistaken, say, the request handler is of an unsupported type, I will get an empty response of 500, which makes debugging difficult. For example, if I incorrectly decorate an attribute with @XmlElementRef, I 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 happening:

 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 the log configuration file with:

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

This gives a lot of output, but still does not show any exception.

I tried to view the Grizzly configuration, but I cannot find a way to disable graceful error handling. Ideally, I would like the server to throw an exception. Any suggestions on what I am missing?

Here is 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 given here:

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

The 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 are gratefully received.

+7
java logging exception jersey grizzly
source share
2 answers

The exception does not apply to the Grizzly layer, so it must be registered with a jersey. I have not found which logger you should enable, but looks like a custom ExceptionMapper might 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(); } } 
+12
source share

As you saw, Grizzly uses java.util.logging . If you want to see stacktrace, you need to make sure that the levels are set correctly in your log.conf file.

Here are the settings that worked for me in the past:

 handlers=java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level=ALL .level=ALL org.glassfish.level=CONFIG 
+1
source share

All Articles