当前位置: 首页 > 面试题库 >

JaxbRepresentation给出错误“不包含ObjectFactory.class或jaxb.in​​dex”

方德宇
2023-03-14
问题内容

我正在尝试创建一个将对象转换为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)

请有人帮助!!!


问题答案:

为了摆脱其他jaxb.in​​dex文件,您可以使用Java类实例化上下文:

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

通常,您只需要传递单个Java类,因为其他类“可以从这些类静态访问”,因此JAXB能够识别它们。



 类似资料:
  • 这真的是一个令人头痛的问题,我不知道这里出了什么问题。我几周前才开始使用猫鼬,所以我想知道这是不是我在这里误解了一些基本的东西。

  • 下面是我的代码 函数executeCommand 误差 libavdevice 57。0.101/57。0.101 libavfilter 6。31.100/6。31.100 libswscale 4。0.100/4。0.100 输出#0,mp4,到'ffmpeg-ss 0-I/storage/emulated/0/video/demo.mp4-t 30-c copy/storage/emulat

  • 我正在使用springboot和gradle,我正在尝试在控制器中执行下面的代码。 在编译时,我得到以下错误 错误:找不到symbol=List.of(“水星”,“金星”,“地球”,“火星”,^symbol:方法of(String,String,String,String,String,String,String,String,String,String) 位置:接口列表 我的分级档案 sourc

  • 问题内容: 我想从字符串中删除最后一次出现的“ \”这个特殊字符。我尝试了像这样的字符串函数 但是每次遇到错误时,我都要求加一个额外的报价。同时我发现(“ \”“)用来传递”这个特殊字符。我该如何进行? 问题答案: 您需要使用 字符串中的字符转义特殊字符(依此类推)。因此,在它们之前使用a 会使它成为 文字 ,这意味着java会将其后的内容视为常规字符。 你可以测试看看 将打印。它会打印。 所以:

  • 除了捆绑包,我对构建任何构建类型的APK绝对没有问题...是的,他们几乎总是出错,平均需要3-4个构建才能得到一个可以上传到游戏商店的捆绑包。有时他们不会出错,但是. aab不在构建目录中(然而. apk和output-metadata.json)。 在每个“新”构建中,第一次尝试总是返回此错误 没有错,文件不在那里。。。令人敬畏的是,如果您正在观看该目录,它会以同样快的速度出现,然后消失。 当我

  • 当我试图在Eclipse中的图形布局中显示android xml文件时,我得到以下消息:从片段布局上下文菜单中选择预览布局。我按照说明右键单击布局-- 我做错了什么? 以下是我的xml: 这里是我的java: 这是一个打印屏幕: