使用Jersey 2.5.1抛出自定义WebApplicationException时的XMLMarshalException

时间:2014-02-04 14:15:18

标签: java jaxb jersey moxy jersey-2.0

我从Jersey 2.4.1升级到2.5.1,我的代码中的错误报告突然出现问题。我为文本墙道歉,但我认为一个记录良好的问题将是有益的。

我的pom.xml的一部分:

    <!-- Jersey specific information -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.5.1</version>
    </dependency>
    <!-- Required only when you are using JAX-RS Client -->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-bean-validation</artifactId>
        <version>2.5.1</version>
    </dependency>

以下是我的POJO:

摘要

package com.mimi.rest.common;

public abstract class AbstractRestStatus
{
    protected int statusCode = 0;
    protected String statusMessage = "OK";
    protected String userMessage = null;
    protected String internalMessage = null;
    protected boolean exception = false;

    // constructors, getters/setters
}

实施水平:

package com.mimi.product.rest;

@XmlRootElement(name = "restStatus")
public class RestStatus extends AbstractRestStatus
{
    // constructors, getters/setters

    @Override
    public String getInternalMessage()
    {
        if (!RestConfiguration.INTERNAL_MESSAGES_ENABLED)
        {
            return null;
        }

        return internalMessage;
    }
}

以下是例外情况:

摘要:

package com.mimi.rest.common;

public abstract class AbstractRestException extends WebApplicationException
{
    private static final long serialVersionUID = 1L;

    private AbstractRestStatus restStatus = null;

    /**
     * Build a rest exception with the provided information to relay to Jersey
     * 
     * @param status
     * @param restStatus
     */
    public AbstractRestException(Status status, AbstractRestStatus restStatus)
    {
        super(Response.status(status).entity(restStatus.asException()).build());
        this.restStatus = restStatus;
    }

    public AbstractRestException(Status status)
    {
        super(Response.status(status).build());
    }

    //restStatus getter
}

默认地将Impl:

package com.mimi.product.rest;

public class RestException extends AbstractRestException
{
    private static final long serialVersionUID = 1L;

    public RestException(Status status, RestStatus error)
    {
        super(status, error);
    }

    public RestException(Status status)
    {
        super(status);
    }
}

我的调试自定义资源:

@Path("error")
@GET
@Produces({ MediaType.APPLICATION_XML + ";charset=utf-8", MediaType.APPLICATION_JSON + ";charset=utf-8" })
public String errorGet()
{
    throw new RestException(Status.BAD_REQUEST, new RestStatus(100, "Not allowed", "Whoops, user error"));
}

到目前为止完成的工作和测试:

在2.4.1中使用GET执行Accept: application/json会产生此(预期)结果:

{
   "type": "restStatus",
   "exception": true,
   "statusCode": 100,
   "statusMessage": "Not allowed",
   "userMessage": "Whoops, user error"
}

在2.5.1中做同样的事情有这个意想不到的结果:

Caused by: Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor for class com.mimi.product.rest.RestStatus was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.
  • 我只是更改了我的pom.xml中的版本号以重现此错误。
  • 请求XML
  • 时不会出现此问题
  • 在摘要中添加@XmlSeeAlso似乎无法解决问题。由于我的AbstractRestStatus是两个不同产品之间的共享对象的性质,我宁愿不必放入import语句或者必须将它合并到单个对象中,因为它们以不同方式实现getInternalMessage()

奇怪的是,如果我将资源中的返回类型更改为RestStatus而不是String,则抛出的异常将在2.5.1中正确序列化。在这两个版本中,返回常规RestStatus而不是抛出RestException会正确序列化,这使我相信问题更多地围绕异常而不是继承问题。

谢谢。

0 个答案:

没有答案