web-dev-qa-db-ja.com

JaxbRepresentationは「ObjectFactory.classまたはjaxb.in​​dexを含まない」というエラーを出します

オブジェクトを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();
    }

}

TestResource.Javaファイル

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ファイル

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)

誰か助けてください!!!

30
user619237

追加のjaxb.in​​dexファイルを削除するには、Javaクラスを使用してコンテキストをインスタンス化できます。

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

通常、単一のJavaクラスのみを渡す必要があります。これは、他のクラスが「これらのクラスから静的に到達可能」であるため、JAXBがそれらを識別できるためです。

12
Jukka

私の場合、JAXB注釈付きクラスと同じパッケージフォルダーに「jaxb.in​​dex」というファイルを追加することで、これを解決できました。そのファイルには、注釈付きクラスの単純な非修飾名がリストされています。

たとえば、私のファイル/MyProject/src/main/Java/com/example/services/types/jaxb.in​​dexは単純に1行です(JAXB型付きクラスが1つしかないため)。

ServerDiagContent

クラスcom.example.services.types.ServerDiagContentを参照します

21
itzg

ClassLoaderの問題が原因でこのエラーが発生しましたが、JAXBが使用するClassLoaderを明示的に渡すことで解決できました。

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の答えに似ていますが、#newInstance(Class ... classesToBeBound)メソッドシグネチャを使用する場合、JAXBは渡されたクラスから自動的にClassLoaderを選択しているに違いありません。

9
choobablue

私の場合、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());
5
user3243752

私はSpringを使用していましたが、変更する必要がありました

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

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

Ref1Ref2

5
Anand Rockzz

次のようなオブジェクトファクトリのインスタンス化がある場合

private ObjectFactory of;

..その後、マーシャルへのコンテキストを取得する最も安全で信頼性の高い方法は次のとおりです。

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

ObjectFactoryクラスが存在する場所の完全なパッケージ名をdeclareおよびinstantiateに使用します。

0
brax7