发生JAXBException:类com.jaxb.model.copy.copy.Snapshot或其任何超类都是此上下文已知的。 [英] JAXBException occurred : class com.jaxb.model.copy.copy.Snapshot nor any of its super class is known to this context..

查看:82
本文介绍了发生JAXBException:类com.jaxb.model.copy.copy.Snapshot或其任何超类都是此上下文已知的。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我解决这个问题吗,
发生了JAXBException:类com.jaxb.model.copy.copy.Snapshot也没有任何超类知道这个上下文..

Can someone help me to resolve the issue, JAXBException occurred : class com.jaxb.model.copy.copy.Snapshot nor any of its super class is known to this context..

接口JaxbWebResource

Interface JaxbWebResource

package com.jaxb.model.copy.copy;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

@Path("/jaxb")
public interface JaxbWebResource
{
    @GET
    @Produces({ "application/xml", "application/json" })
    public Response readOffer(@Context HttpServletRequest req);
}

Class JaxbWebResourceImpl

Class JaxbWebResourceImpl

package com.jaxb.model.copy.copy;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.bind.JAXBElement;

import org.springframework.stereotype.Component;

import com.jaxb.model.copy.copy.Foo;
import com.jaxb.model.copy.copy.Moo;
import com.jaxb.model.copy.copy.ObjectFactory;
import com.jaxb.model.copy.copy.Snapshot;

@Component("jaxbWebResource")
public class JaxbWebResourceImpl implements JaxbWebResource
{

    @Override
    public Response readOffer(final HttpServletRequest req) {

        System.out.println("JaxbWebResourceImpl readMarketingOffer");
        Snapshot snapshot = new Snapshot();
        Foo.Detail.Bar barV = new Foo.Detail.Bar();
        barV.setBaz("heloo baz");
        JAXBElement<Foo.Detail.Bar> bar = new ObjectFactory().createBar(barV);
        bar.setNil(true);
        Foo.Detail detail = new Foo.Detail();
        detail.setBar(bar);
        Foo foo = new Foo();
        foo.setDetail(detail);
        snapshot.setMoo(foo);

        return Response.status(Status.OK).entity(snapshot).build();

    }

}

ObjectFactory class

ObjectFactory class

    package com.jaxb.model.copy.copy;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(namespace="",name="bar")
    public JAXBElement<Moo.Detail.Bar> createBar(Moo.Detail.Bar bar) {
        return new JAXBElement<Moo.Detail.Bar>(new QName("bar"), Moo.Detail.Bar.class, bar);
    }

}

Foo class
package com.jaxb.model.copy.copy;

Foo class package com.jaxb.model.copy.copy;

import java.math.BigDecimal;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

import com.guthyrenker.soma.ws.rest.v1.model.ObjectFactory;
import com.guthyrenker.soma.ws.rest.v1.model.WSMarketingOfferOP;
import com.guthyrenker.soma.ws.rest.v1.model.WSMarketingOfferWEB;
import com.jaxb.model.copy.Snapshot;
import com.jverstry.annotations.generics.Market;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)

public class Foo extends Moo{

    @XmlElement
   String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Moo class
package com.jaxb.model.copy.copy;

Moo class package com.jaxb.model.copy.copy;

import java.math.BigDecimal;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;

import com.jverstry.annotations.generics.Market;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Moo {


     @XmlElement
        protected Detail detail;

        public Detail getDetail() {
            return detail;
        }

        public void setDetail(Detail detail) {
            this.detail = detail;
        }

        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = { "bar"})
        public static class Detail
        {
            @XmlElementRef(name="bar")
            private JAXBElement<Foo.Detail.Bar> bar;


                public JAXBElement<Foo.Detail.Bar> getBar() {
                return bar;
            }

            public void setBar(JAXBElement<Foo.Detail.Bar> bar) {
                this.bar = bar;
            }




            @XmlAccessorType(XmlAccessType.FIELD)
            @XmlType(name = "", propOrder = { "value" })
                public static class Bar
                {

                     @XmlAttribute
                     protected String baz;

                     @XmlValue
                        protected BigDecimal value;

                        public String getBaz() {
                            return baz;
                        }

                        public void setBaz(String baz) {
                            this.baz = baz;
                        }

                        public BigDecimal getValue() {
                            return value;
                        }

                        public void setValue(BigDecimal value) {
                            this.value = value;
                        }
                }
        }
}

快照class
package com.jaxb.model.copy.copy;

Snapshot class package com.jaxb.model.copy.copy;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.guthyrenker.soma.ws.rest.v1.adapter.JsonDateTimeSerializer;
/**
 * @author Srinivasa Kankipati
 * @since 1.0
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "mask", "xmlns", "currentAsOf","moo" })
@XmlRootElement(name = "snapshot")
public class Snapshot<T extends Moo>
{
    @XmlAttribute
    public String mask;

    @XmlElement
    public Date currentAsOf;

    @XmlAttribute
    @JsonIgnore
    public String xmlns;

    @XmlElement(name = "moo")
    private Moo moo;

    public String getMask() {
        return mask;
    }

    public void setMask(String mask) {
        this.mask = mask;
    }

    public Date getCurrentAsOf() {
        return currentAsOf;
    }

    public void setCurrentAsOf(Date currentAsOf) {
        this.currentAsOf = currentAsOf;
    }

    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }

    public Moo getMoo() {
        return moo;
    }

    public void setMoo(Moo moo) {
        this.moo = moo;
    }
}


推荐答案

JAX -RS将根据JAX-RS方法返回的内容构建 JAXBContext 。然后,JAXB将通过传递关系引入更多类。这并不总是导致所有类都被处理。为了更好地控制创建的 JAXBContext ,您可以创建一个 ContextResolver 来构建 JAXBContext 你想要的方式。

JAX-RS will build a JAXBContext based on what the JAX-RS method returns. JAXB will then pull in more classes via transitive relationships. This doesn't always cause all the classes to be processed. To get more control over the JAXBContext created you can create a ContextResolver that builds the JAXBContext the way that you want.

import java.util.*;
import javax.ws.rs.Produces;
import javax.ws.rs.ext.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;

@Provider
@Produces(MediaType.APPLICATION_XML)
public class MyContextResolver implements ContextResolver<JAXBContext> {

    private JAXBContext jc;

    public MyContextResolver() {
        try {
           jc = // Build Your JAXBContext on all classes it needs to be aware of.
        } catch(JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    public JAXBContext getContext(Class<?> clazz) {
       // Return the JAXBContext for any class it is aware of otherwise return null.
    }

}

我提供了一些额外的细节如何构建你的 JAXBContext 来回答你的相关问题:

I have provided some additional details on how your JAXBContext should be built in an answer to your related question:


  • < a href =https://stackoverflow.com/questions/23676605/exception-in-thread-main-javax-xml-bind-jaxbexception-class-nor-any-of-its-su/23677884#23677884>例外情况在线程主中javax.xml.bind.JAXBException:类或其任何超类都在此上下文中已知

  • Exception in thread "main" javax.xml.bind.JAXBException: class nor any of its super class is known to this context

这篇关于发生JAXBException:类com.jaxb.model.copy.copy.Snapshot或其任何超类都是此上下文已知的。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆