【问题标题】:Why does jersey have no error log when the status is 500?为什么 jersey 状态为 500 时没有错误日志?
【发布时间】:2015-01-09 05:26:52
【问题描述】:

这是我的 web.xml:

<servlet>
  <servlet-name>Jersey REST Service</servlet-name>
  <servlet-class>
    org.glassfish.jersey.servlet.ServletContainer
  </servlet-class>
  <init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>rest</param-value>
  </init-param>
  <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

这是我的资源类:

@Path("/main")
public class MainResource {

    @XmlRootElement
    public class Planet {
        public int id;
        public String name;
        public double radius;
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Planet getPlanet() {
        final Planet planet = new Planet();

        planet.id = 1;
        planet.name = "Earth";
        planet.radius = 1.0;

        return planet;
    }
}

而当我运行tomcat服务器时,日志是这样的:

09-Jan-2015 13:19:10.501 INFO [http-apr-8080-exec-8] org.glassfish.jersey.filter.LoggingFilter.log 1 * Server has received a request on thread http-apr-8080-exec-8
1 > GET http://localhost:8080/rest/main
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
1 > accept-encoding: gzip, deflate, sdch
1 > accept-language: en,zh-CN;q=0.8,zh;q=0.6
1 > connection: keep-alive
1 > cookie: JSESSIONID=69F79D567E7ECA27A129C477B91C7758
1 > host: localhost:8080
1 > user-agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36

09-Jan-2015 13:19:10.597 INFO [http-apr-8080-exec-8] org.glassfish.jersey.filter.LoggingFilter.log 1 * Server responded with a response on thread http-apr-8080-exec-8
1 < 200
1 < Content-Type: application/xml

09-Jan-2015 13:19:10.794 INFO [http-apr-8080-exec-8] org.glassfish.jersey.filter.LoggingFilter.log 2 * Server responded with a response on thread http-apr-8080-exec-8
2 < 500

我想知道为什么它显示 500 错误,但我找不到任何关于异常堆栈的错误日志。

请帮帮我。

【问题讨论】:

    标签: jersey-2.0


    【解决方案1】:

    我解决了我的问题,现在我回答我自己的问题。

    首先,如何启用异常跟踪日志记录?

    @Provider
    public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<Exception> {
        @Override
        public Response toResponse(Exception exception) {
            exception.printStackTrace();
            return Response.status(500).build();
        }
    }
    

    是的,我们应该自己实现一个 ExceptionMapper。

    第二,我的代码有什么错误?

    Planet 类应该是静态的。

    【讨论】:

    • 不幸的是,这会禁用所有其他异常映射,例如为javax.ws.rs.NotFoundException 制作 500 而不是 404@
    • 在继续研究源代码之后,似乎Jersey框架用户立场的唯一解决方案,即不更改源代码,是implements javax.ws.rs.ext.ExceptionMapper,但仍然对上面的实现持怀疑态度跨度>
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 2021-04-07
    • 2011-11-01
    相关资源
    最近更新 更多