Java JAXBRE演示文稿给出错误“;不包含ObjectFactory.class或jaxb.index;

Java JAXBRE演示文稿给出错误“;不包含ObjectFactory.class或jaxb.index;,java,rest,jakarta-ee,jaxb,restlet-2.0,Java,Rest,Jakarta Ee,Jaxb,Restlet 2.0,我正在尝试创建一个示例测试应用程序,该应用程序将对象转换为JaxbRepresentation。但是当我尝试运行这个时,它会给我一个错误 Main.java文件 package test_jaxb; import org.restlet.Server; import org.restlet.data.Protocol; public class Main { /** * @param args the command line arguments */

我正在尝试创建一个示例测试应用程序,该应用程序将对象转换为JaxbRepresentation。但是当我尝试运行这个时,它会给我一个错误

Main.java文件

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();
    }

}
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;
   }
}
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;
   }
}
TestResource.java文件

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();
    }

}
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;
   }
}
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;
   }
}
在服务器上运行此操作时,会抛出以下错误:

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)

请有人帮忙

在我的例子中,我能够通过在与jaxb注释类相同的包文件夹中添加一个名为“jaxb.index”的文件来解决这个问题。在该文件中,列出注释类的简单、非限定名称

例如,我的文件/MyProject/src/main/java/com/example/services/types/jaxb.index 只是一行(因为我只有一个JAXB类型的类):


它引用了com.example.services.types.ServerDiagContent类

要删除额外的jaxb.index文件,可以使用Java类实例化上下文:


通常只需要传入一个java类,因为其他类“可以从这些类静态访问”,所以JAXB能够识别它们。

在我的例子中,我可以通过更改JAXBContext的实例化来解决这个问题。 它可以使用包名或ObjectFactory类作为参数进行实例化

使用包名称进行实例化时:

com.myexample.test.ObjectFactory objectFactory = new com.myexample.test.ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(objectFactoryMessageBody.getClass().getPackage().getName());
它给出了错误:

"com.myexample.test" doesnt contain ObjectFactory.class or jaxb.index
使用类名实例化时没有错误:

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

我之所以出现这个错误是因为类加载器问题,我能够通过显式地传递JAXB应该使用的类加载器来解决它,因此:

JAXBContext.newInstance(com.myexample.test.ObjectFactory.class.getPackage().getName());
提供了一个错误,但在使用时工作正常:

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

这可能与user3243752的答案类似,我打赌JAXB在使用#newInstance(class…classesToBeBound)方法签名时会自动从传入的类中选择类加载器。

如果您有对象工厂的实例化,例如

private ObjectFactory of;
…那么,获得马歇尔背景的最安全、最可靠的方法是:

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

使用存在ObjectFactory类的对象的完整包名来声明实例化对象。

我使用的是Spring,我刚刚不得不更改

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


&

我在查找对象工厂类的位置创建了一个空的jaxb.index文件。这解决了我的问题。

我注意到本应转换为xXSD文件的有效负载Java类的包名在PUM文件中的JAXB2插件配置中是错误的。当我纠正它时,问题就解决了。

当您生成与JAXWSW相同的代码时,这很有用。我们遇到了相同的问题,但在[Stackoverflow][1][1]上找到了解决方案:我有一个类似的问题,但不确定如何应用此问题,或者只是简单地执行此操作:
ObjectFactory.class.getPackage().getName()
,无需指定包名