当前位置: 首页 > 面试题库 >

javax.xml.bind.JAXBException:此上下文已知类***或其任何超类

裴俊能
2023-03-14
问题内容

我正在尝试通过REST Web服务传递对象。以下是我的课程,使用一些示例代码说明了我需要的功能。

REST Web服务类方法

@POST
@Path("/find")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_JSON})
public Response getDepartments(){
    Response response = new Response();

    try {

        response.setCode(MessageCode.SUCCESS);
        response.setMessage("Department Names");
        Department dept = new Department("12", "Financial");
        response.setPayload(dept);

    } catch (Exception e) {
        response.setCode(MessageCode.ERROR);
        response.setMessage(e.getMessage());
        e.printStackTrace();
    }       
    return response;
}

反应等级

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement

public class Response implements Serializable{

    private static final long serialVersionUID = 1L;

    public enum MessageCode {
        SUCCESS, ERROR, UNKNOWN
    }

    private MessageCode code;
    private String message;
    private Object payload;

    public MessageCode getCode() {
        return code;
    }

    public void setCode(MessageCode code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getPayload() {
        return payload;
    }

    public void setPayload(Object payload) {
        this.payload = payload;
    }
}

部门课

@XmlRootElement
public class Department implements java.io.Serializable {


    private String deptNo;
    private String deptName;


    public Department() {
    }

    public Department(String deptNo, String deptName) {
        this.deptNo = deptNo;
        this.deptName = deptName;
    }

    public String getDeptNo() {
        return this.deptNo;
    }

    public void setDeptNo(String deptNo) {
        this.deptNo = deptNo;
    }

    public String getDeptName() {
        return this.deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

}

当我在其余Web服务类中调用getDepartments方法时,它将返回以下异常。但是,如果我在Response类中将 有效负载的
Object 类型更改为 Department
,它将正确返回json响应。但是由于我需要将此Response类用于不同类型的类,因此无法将有效负载重新串为一种类类型。有人可以在这件事上帮助我吗?

堆栈跟踪

Dec 27, 2012 9:34:18 PM com.sun.jersey.spi.container.ContainerResponse logException
SEVERE: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class Department nor any of its super class is known to this context.]
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159)
    at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:401)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: javax.xml.bind.MarshalException
 - with linked exception:
[javax.xml.bind.JAXBException: class Department nor any of its super class is known to this context.]
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:177)
    at com.sun.jersey.json.impl.BaseJSONMarshaller.marshallToJSON(BaseJSONMarshaller.java:103)
    at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.writeTo(JSONRootElementProvider.java:136)
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:157)
    ... 23 more
Caused by: javax.xml.bind.JAXBException: class Department nor any of its super class is known to this context.
    at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:250)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:265)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:657)
    at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:156)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:344)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:597)
    at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:328)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:498)
    at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320)
    ... 27 more
Caused by: javax.xml.bind.JAXBException: class Department nor any of its super class is known to this context.
    at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:611)
    at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:652)
    ... 33 more

问题答案:

JAX-
RS实现自动支持基于可发现的JAXB注释的类的编组/解组,但是由于您的有效载荷声明为Object,我认为所创建JAXBContextDepartment类会丢失该类,而何时该进行编组则不知道如何进行。

一个快速而肮脏的解决方法是XmlSeeAlso在响应类中添加一个注释:

@XmlRootElement
@XmlSeeAlso({Department.class})
public class Response implements Serializable {
  ....

或一些更复杂的是“丰富”为JAXB的情况下Response使用类ContextResolver

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

@Provider
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class ResponseResolver implements ContextResolver<JAXBContext> {
    private JAXBContext ctx;

    public ResponseResolver() {
        try {
            this.ctx = JAXBContext.newInstance(

                        Response.class, 
                        Department.class

                    );
        } catch (JAXBException ex) {
            throw new RuntimeException(ex);
        }
    }

    public JAXBContext getContext(Class<?> type) {
        return (type.equals(Response.class) ? ctx : null);
    }
}


 类似资料:
  • 我的回应如下: 用户类: 当我尝试生成“application/xml”时,我遇到了如下异常:com。太阳伊斯塔克。内部的SAXException2:class com。abcd。刀。领域使用者用户或其任何超级类都不为本文所知。javax。xml。绑定JAXBException:类com。abcd。刀。领域使用者用户或其任何超级类都不为本文所知。 如何在spring mvc中处理响应类中的“对象”

  • 当我在编组一个对象时,我得到了这个异常:javax.xml.bind.JAXBExctive: class订阅,它的任何超类对此上下文都是已知的。 我知道有@xmlsee和修改jaxb类的解决方案,但当我们从XSD/WSDL文件生成jaxb类时,我们无法更改它们。因此,这些解决方案不适用于这种情况。

  • 有人能帮我吗为什么下面被扔了 线程“main”javax中出现异常。xml。绑定JAXBException:类com。jaxb。模型复制复制Snapshot及其任何超类都不为本文所知。在com上。太阳xml。绑定v2。运行时。JAXBContextImpl。getBeanInfo(JAXBContextImpl.java:588)位于com。太阳xml。绑定v2。运行时。XMLSerializer

  • 问题内容: 当我调用使用 CXF 构建的特定静态服务方法时,出现以下错误,任何人都知道为什么以及如何解决它? 发生JAXBException:com.octory.ws.dto类。在此上下文中, ProfileDto 或其任何超类都不为人知。 以下是服务方法和相关的DTO: SearchResultDto: ProfileDto: 问题答案: 您的班级未在中引用。尝试添加到中。

  • 我不是想写复杂的代码,我只是想了解这里发生了什么(或没有发生什么)。我检查了其他问题,但它们都有复杂的情况,我认为这种情况是迄今为止最简单的。 我有以下代码: 在我看来,我们有一个

  • 我试图使用以下代码调用非活动类中的新活动: 我有一个UsageActivity,它有以下代码: 在onCreate()下面 我创建了这样一个方法: 它抛出一个异常,说: 08-23 12:51:39.439:E/AndroidRuntime(15059):致命异常:main 08-23 12:51:39.439:E/AndroidRuntime(15059):java。lang.NullPoint