web-dev-qa-db-fra.com

La représentation Jaxb donne l'erreur "ne contient pas ObjectFactory.class ou jaxb.index"

J'essaie de créer un exemple d'application de test qui convertit un objet en JaxbRepresentation. Mais quand j'essaye de lancer ça, ça me donne une erreur.

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

}

Fichier 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;
   }
}

Fichier 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;
   }
}

Lorsque j'exécute cela sur le serveur, il génère l'erreur suivante:

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)

Veuillez aider quelqu'un !!!

30
user619237

Pour vous débarrasser des fichiers jaxb.index supplémentaires, vous pouvez utiliser Java pour instancier le contexte:

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

Habituellement, vous devez saisir uniquement une seule classe Java classe car les autres classes sont "joignables statiquement à partir de ces classes") afin que JAXB puisse les identifier.

12
Jukka

Dans mon cas, j'ai pu résoudre ce problème en ajoutant un fichier appelé "jaxb.index" dans le même dossier de package que la classe annotée JAXB. Dans ce fichier, répertoriez les noms simples et non qualifiés des classes annotées.

Par exemple, mon fichier /MyProject/src/main/Java/com/example/services/types/jaxb.index est simplement une ligne (car je n'ai qu'une seule classe typée JAXB):

ServerDiagContent

qui fait référence à la classe com.example.services.types.ServerDiagContent

21
itzg

J'ai eu cette erreur à cause d'un problème ClassLoader, et j'ai pu le résoudre en passant explicitement le ClassLoader que JAXB devrait utiliser, donc ceci:

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

a donné une erreur, mais a fonctionné correctement lors de l'utilisation:

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

qui est probablement similaire à la réponse de user3243752, je parie que JAXB choisit automatiquement le ClassLoader de la classe passée lors de l'utilisation de la signature de la méthode #newInstance (Class ... classesToBeBound).

9
choobablue

Dans mon cas, j'ai pu résoudre le problème en modifiant l'instanciation du JAXBContext. Il peut être instancié avec le nom du package ou la classe ObjectFactory comme paramètre.

Lors de l'instanciation avec le nom du package:

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

Il a donné l'erreur:

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

Aucune erreur lors de l'instanciation avec le nom de la classe:

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

J'utilisais Spring et je devais juste changer

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

à

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

Ref1 & Ref2

5
Anand Rockzz

Si vous avez une instanciation de votre fabrique d'objets comme

private ObjectFactory of;

.. alors le moyen le plus sûr et le plus fiable pour obtenir un Context avec Marshall est:

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

Utilisez le nom de package complet de l'endroit où la classe ObjectFactory est présente pour déclarer et instancier l'objet.

0
brax7