简体   繁体   English

Grizzly Jersey吞咽异常

[英]Grizzly Jersey swallowing exceptions

I'm building a Jersey Moxy service using the quickstart archetype at the end. 我正在使用最后的quickstart原型构建Jersey Moxy服务。 My code works fine and I can get some JSON returned. 我的代码工作正常,我可以返回一些JSON。 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. 然而,正如我正在开发的那样,如果我犯了一个错误,说请求处理程序有一个不支持的类型,我将得到一个空的500响应,这使调试变得困难。 For example, if I decorate an attribute incorrectly with @XmlElementRef, I will get a response like: 例如,如果我使用@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

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. 我试过调查Grizzly配置,但我找不到办法关闭优雅的错误处理。 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 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. 感谢收到的建议。

The exception is not getting propagated to the Grizzly layer, so it should be logged by Jersey. 例外情况不会传播到Grizzly层,因此应该由Jersey记录。 I haven't found what kind of Logger you have to enable, but looks like custom ExceptionMapper could help. 我还没有找到你必须启用什么样的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();
    }
}

As you've seen, Grizzly uses java.util.logging . 正如您所见,Grizzly使用java.util.logging If you'd like to see the stacktrace, you need to make sure the levels are correctly set in your log.conf file. 如果您想查看log.conf ,则需要确保在log.conf文件中正确设置了级别。

Here are settings that have worked for me in the past: 以下是过去对我有用的设置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM