JaxbRepresentation gives error “doesnt contain ObjectFactory.class or jaxb.index”

限于喜欢 提交于 2019-12-03 06:38:25

问题


I am trying to create a sample test application which converts an object to JaxbRepresentation. But when I try to run this, it gives me an error.

Main.java file

package test_jaxb;

import org.restlet.Server;
import org.restlet.data.Protocol;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
      Server helloServer = new Server(Protocol.HTTP, 8111,
      TestResource.class);
      helloServer.start();
    }

}

TestResource.java file

package test_jaxb;

import org.restlet.ext.jaxb.JaxbRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class TestResource extends ServerResource{

   @Override
   protected Representation get() throws ResourceException {
      SampleResponse res = new SampleResponse();
      res.setMsg("Success");
      res.setStatusCode(0);
      JaxbRepresentation<SampleResponse> representation = new JaxbRepresentation<SampleResponse>(res);
      return representation;
   }
}

SampleResponse.java file

package test_jaxb;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Response")
public class SampleResponse {
   private int statusCode;
   private String msg;

   @XmlElement(name="Msg")
   public String getMsg() {
      return msg;
   }

   public void setMsg(String msg) {
      this.msg = msg;
   }

   @XmlElement(name="StatusCode")
   public int getStatusCode() {
      return statusCode;
   }

   public void setStatusCode(int statusCode) {
      this.statusCode = statusCode;
   }
}

When I run this on the server, it throws the following error:

WARNING: Problem creating Marshaller
javax.xml.bind.JAXBException: "test_jaxb" doesnt contain ObjectFactory.class or jaxb.index
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:119)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:299)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
    at org.restlet.ext.jaxb.JaxbRepresentation.getContext(JaxbRepresentation.java:83)
    at org.restlet.ext.jaxb.internal.Marshaller$1.initialValue(Marshaller.java:68)
    at org.restlet.ext.jaxb.internal.Marshaller$1.initialValue(Marshaller.java:64)
    at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
    at java.lang.ThreadLocal.get(ThreadLocal.java:131)
    at org.restlet.ext.jaxb.internal.Marshaller.getMarshaller(Marshaller.java:163)
    at org.restlet.ext.jaxb.internal.Marshaller.marshal(Marshaller.java:216)
    at org.restlet.ext.jaxb.JaxbRepresentation.write(JaxbRepresentation.java:527)
    at org.restlet.representation.WriterRepresentation.write(WriterRepresentation.java:104)
    at org.restlet.engine.http.connector.Connection.writeMessageBody(Connection.java:873)
    at org.restlet.engine.http.connector.Connection.writeMessage(Connection.java:818)
    at org.restlet.engine.http.connector.ServerConnection.writeMessage(ServerConnection.java:334)
    at org.restlet.engine.http.connector.Connection.writeMessages(Connection.java:954)
    at org.restlet.engine.http.connector.BaseServerHelper.handleOutbound(BaseServerHelper.java:201)
    at org.restlet.engine.http.connector.BaseHelper.handleNextOutbound(BaseHelper.java:425)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:174)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
May 16, 2011 7:05:12 AM org.restlet.ext.jaxb.internal.Marshaller getMarshaller
WARNING: Unable to locate marshaller.
May 16, 2011 7:05:12 AM org.restlet.ext.jaxb.JaxbRepresentation write
WARNING: JAXB marshalling error caught.
javax.xml.bind.JAXBException: Unable to locate marshaller.
    at org.restlet.ext.jaxb.internal.Marshaller.getMarshaller(Marshaller.java:166)
    at org.restlet.ext.jaxb.internal.Marshaller.marshal(Marshaller.java:216)
    at org.restlet.ext.jaxb.JaxbRepresentation.write(JaxbRepresentation.java:527)
    at org.restlet.representation.WriterRepresentation.write(WriterRepresentation.java:104)
    at org.restlet.engine.http.connector.Connection.writeMessageBody(Connection.java:873)
    at org.restlet.engine.http.connector.Connection.writeMessage(Connection.java:818)
    at org.restlet.engine.http.connector.ServerConnection.writeMessage(ServerConnection.java:334)
    at org.restlet.engine.http.connector.Connection.writeMessages(Connection.java:954)
    at org.restlet.engine.http.connector.BaseServerHelper.handleOutbound(BaseServerHelper.java:201)
    at org.restlet.engine.http.connector.BaseHelper.handleNextOutbound(BaseHelper.java:425)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:174)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
May 16, 2011 7:05:12 AM org.restlet.ext.jaxb.internal.Marshaller$1 initialValue
WARNING: Problem creating Marshaller
javax.xml.bind.JAXBException: "failure" doesnt contain ObjectFactory.class or jaxb.index
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:119)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:299)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:372)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:337)
    at org.restlet.ext.jaxb.JaxbRepresentation.getContext(JaxbRepresentation.java:83)
    at org.restlet.ext.jaxb.internal.Marshaller$1.initialValue(Marshaller.java:68)
    at org.restlet.ext.jaxb.internal.Marshaller$1.initialValue(Marshaller.java:64)
    at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141)
    at java.lang.ThreadLocal.get(ThreadLocal.java:131)
    at org.restlet.ext.jaxb.internal.Marshaller.getMarshaller(Marshaller.java:163)
    at org.restlet.ext.jaxb.internal.Marshaller.marshal(Marshaller.java:216)
    at org.restlet.ext.jaxb.JaxbRepresentation.write(JaxbRepresentation.java:535)
    at org.restlet.representation.WriterRepresentation.write(WriterRepresentation.java:104)
    at org.restlet.engine.http.connector.Connection.writeMessageBody(Connection.java:873)
    at org.restlet.engine.http.connector.Connection.writeMessage(Connection.java:818)
    at org.restlet.engine.http.connector.ServerConnection.writeMessage(ServerConnection.java:334)
    at org.restlet.engine.http.connector.Connection.writeMessages(Connection.java:954)
    at org.restlet.engine.http.connector.BaseServerHelper.handleOutbound(BaseServerHelper.java:201)
    at org.restlet.engine.http.connector.BaseHelper.handleNextOutbound(BaseHelper.java:425)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:174)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
May 16, 2011 7:05:12 AM org.restlet.ext.jaxb.internal.Marshaller getMarshaller
WARNING: Unable to locate marshaller.
May 16, 2011 7:05:12 AM org.restlet.engine.http.connector.Connection writeMessage
WARNING: Exception while writing the message body.
java.io.IOException: Unable to locate marshaller.
    at org.restlet.ext.jaxb.JaxbRepresentation.write(JaxbRepresentation.java:539)
    at org.restlet.representation.WriterRepresentation.write(WriterRepresentation.java:104)
    at org.restlet.engine.http.connector.Connection.writeMessageBody(Connection.java:873)
    at org.restlet.engine.http.connector.Connection.writeMessage(Connection.java:818)
    at org.restlet.engine.http.connector.ServerConnection.writeMessage(ServerConnection.java:334)
    at org.restlet.engine.http.connector.Connection.writeMessages(Connection.java:954)
    at org.restlet.engine.http.connector.BaseServerHelper.handleOutbound(BaseServerHelper.java:201)
    at org.restlet.engine.http.connector.BaseHelper.handleNextOutbound(BaseHelper.java:425)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:174)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)
May 16, 2011 7:05:12 AM org.restlet.engine.http.connector.ServerConnection writeMessage
INFO: An exception occured while writing the response
java.io.IOException: Unable to locate marshaller.
    at org.restlet.ext.jaxb.JaxbRepresentation.write(JaxbRepresentation.java:539)
    at org.restlet.representation.WriterRepresentation.write(WriterRepresentation.java:104)
    at org.restlet.engine.http.connector.Connection.writeMessageBody(Connection.java:873)
    at org.restlet.engine.http.connector.Connection.writeMessage(Connection.java:818)
    at org.restlet.engine.http.connector.ServerConnection.writeMessage(ServerConnection.java:334)
    at org.restlet.engine.http.connector.Connection.writeMessages(Connection.java:954)
    at org.restlet.engine.http.connector.BaseServerHelper.handleOutbound(BaseServerHelper.java:201)
    at org.restlet.engine.http.connector.BaseHelper.handleNextOutbound(BaseHelper.java:425)
    at org.restlet.engine.http.connector.BaseServerHelper.handleInbound(BaseServerHelper.java:174)
    at org.restlet.engine.http.connector.BaseHelper.handleNextInbound(BaseHelper.java:418)
    at org.restlet.engine.http.connector.Connection.readMessages(Connection.java:695)
    at org.restlet.engine.http.connector.Controller$2.run(Controller.java:95)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
    at java.lang.Thread.run(Thread.java:619)

Please someone help!!!


回答1:


To get rid of additional jaxb.index files you may use Java class to instantiate the context:

http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)

Usually you need to pass in only single java class because other classes are "statically reachable from these class(es)" so JAXB is able to identify them.




回答2:


In my case I was able to resolve this by adding a file called "jaxb.index" in the same package folder as the JAXB annotated class. In that file list the simple, non-qualified names of the annotated classes.

For example, my file /MyProject/src/main/java/com/example/services/types/jaxb.index is simply one line (since I have only one JAXB typed class):

ServerDiagContent

which refers to the class com.example.services.types.ServerDiagContent




回答3:


I got this error because of a ClassLoader issue, and I was able to solve it by explicitly passing the ClassLoader that JAXB should use, so this:

JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName());

gave an error, but worked properly when using:

JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName(),
                        com.myexample.test.ObjectFactory.class.getClassLoader());

which is probably similar to user3243752's answer, I bet that JAXB is automatically choosing the ClassLoader from the passed in class when using the #newInstance(Class... classesToBeBound) method signature.




回答4:


I was using Spring and I just had to change

Jaxb2Marshaller mlr = new Jaxb2Marshaller();
mlr.setContextPaths("","");

to

Jaxb2Marshaller mlr = new Jaxb2Marshaller();
mlr.setPackagesToScan("","");

Ref1 & Ref2




回答5:


In my case I was able to solve the problem by changing the instantiation of the JAXBContext. It can either be instantiated with the package name or the ObjectFactory class as a parameter.

When instantiating with the package name:

com.myexample.test.ObjectFactory objectFactory = new com.myexample.test.ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(objectFactoryMessageBody.getClass().getPackage().getName());

It gave the error:

"com.myexample.test" doesnt contain ObjectFactory.class or jaxb.index

No errors when instantiating with the class name:

com.myexample.test.ObjectFactory objectFactory = new com.myexample.test.ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(objectFactoryMessageBody.getClass());



回答6:


If you have an instantiation of your object factory like

private ObjectFactory of;

..then the safest, most reliable way to get a Context to Marshall with is:

  JAXBElement<GreetingListType> gl = of.createGreetings( grList );
  JAXBContext jc = JAXBContext.newInstance(of.getClass());
  Marshaller m = jc.createMarshaller();



回答7:


Use the complete package name of the where the ObjectFactory class is present to declare and instantiate the Object.



来源:https://stackoverflow.com/questions/6017146/jaxbrepresentation-gives-error-doesnt-contain-objectfactory-class-or-jaxb-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!