Grizzly Jersey吞咽异常

时间:2013-09-05 10:37:19

标签: java exception logging jersey grizzly

我正在使用最后的quickstart原型构建Jersey Moxy服务。我的代码工作正常,我可以返回一些JSON。然而,正如我正在开发的那样,如果我犯了一个错误,说请求处理程序有一个不支持的类型,我将得到一个空的500响应,这使调试变得困难。例如,如果我用@XmlElementRef错误地修饰了一个属性,我会收到如下响应:

$ 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

服务器将表现为没有错:

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...

我尝试使用日志配置文件:

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

这会产生大量输出,但仍然没有显示任何异常。

我试过调查Grizzly配置,但我找不到办法关闭优雅的错误处理。理想情况下,我希望服务器抛出异常。关于我缺少什么的任何建议?

这是我的主要代码:

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;
       }
   }
}

代码几乎与此处的示例完全相同:

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

我使用的完整原型生成:

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

感谢收到的建议。

2 个答案:

答案 0 :(得分:13)

异常没有传播到Grizzly层,所以应该由Jersey记录。我还没有找到你必须启用哪种Logger,但看起来像自定义的ExceptionMapper可以帮助。

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();
    }
}

答案 1 :(得分:1)

正如您所见,Grizzly使用java.util.logging。如果您想查看堆栈跟踪,则需要确保在log.conf文件中正确设置了级别。

以下是过去对我有用的设置:

handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=ALL
.level=ALL
org.glassfish.level=CONFIG