【问题标题】:JSONWithPadding - CORS related error 500 issue for JERSEY JAX-RS RESTJSONWithPadding - JERSEY JAX-RS REST 的 CORS 相关错误 500 问题
【发布时间】:2017-11-15 07:52:46
【问题描述】:

我使用的是 Jersey 1.9,在这种情况下,为了允许跨域问题,我使用 JsonWithPadding (com.sun.jersey.api.json.JSONWithPadding) 包装了我的对象。如果没有 JsonWithPadding,它会返回正确的 JSON 响应。

完美使用 JsonWithPadding,如果我切换到 Jersey 2,它会给我以下错误。我使用的是 eclipse neon,tomcat 8。

它甚至不会在控制台上抛出任何错误,而是我在访问日志中发现错误 500 以及我认为是安全(木马)问题的 1082。

访问日志

172.22.14.88 - - [13/Jun/2017:09:17:49 -0400] "GET /csmgt/rest/client/HTTP/1.1" 500 1082

回应:

<!DOCTYPE html>
<html>
    <head>
        <title>Apache Tomcat/8.0.28 - Error report</title>
        <style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style>
    </head>
    <body>
        <h1>HTTP Status 500 - Internal Server Error</h1>
        <div class="line"></div>
        <p>
            <b>type</b> Status report
        </p>
        <p>
            <b>message</b>
            <u>Internal Server Error</u>
        </p>
        <p>
            <b>description</b>
            <u>The server encountered an internal error that prevented it from fulfilling this request.</u>
        </p>
        <hr class="line">
        <h3>Apache Tomcat/8.0.28</h3>
    </body>
</html>

感谢您对 JsonWithPadding 的任何帮助。

【问题讨论】:

    标签: eclipse jersey jersey-2.0 tomcat8


    【解决方案1】:

    最后,我在挣扎之后解决了我的问题。首先,在 Eclipse 控制台上看不到 Jersey 抛出的错误,而不是 Error 500 响应。我创建了以下自定义 ExceptionMapper 以在控制台上显示 Jersey 错误。

    导入 javax.ws.rs.core.Response; 导入 javax.ws.rs.ext.Provider;

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

    ... 该异常映射器清楚地向我显示了原因是 JSON With Padding 我曾经支持 CORS(跨域),在我转移到 Jersey 2 之前它正在工作。我意识到 Jersey 2.0 默认为 MOXy Provider。

    带有填充库的 JSON 有两个 API: com.sun.jersey.api.json.JSONWithPadding - 我用过这个 org.eclipse.persistence.oxm.JSONWithPadding

    --- 解决方案

    设置@web.xml

    禁用 MOXY

     <init-param>
        <param-name>jersey.config.disableMoxyJson.server</param-name>
        <param-value>true</param-value>
    </init-param>
    

    使用杰克逊

    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>YOUR SERVICES;org.codehaus.jackson.jaxrs
        </param-value>
    </init-param>
    

    --- 确保您添加了 jersey-media-json-jackson-2.5.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多