JAXWS-RT:获取com.sun.xml.ws.spi.db.DatabindingException绑定一个简单的类 [英] JAXWS-RT: getting com.sun.xml.ws.spi.db.DatabindingException binding a simple class

查看:50
本文介绍了JAXWS-RT:获取com.sun.xml.ws.spi.db.DatabindingException绑定一个简单的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXWS-RT构建一个小型SOAP Web服务;我有一个简单的类,看起来像这样:

I'm building a small SOAP Web Service using JAXWS-RT; I have a simple class that looks like this:

public class Task {

    public String name;
    public String context;
    public String project;
    public PriorityType priority;

    public Task(String name, String context, String project, PriorityType priority) {
        this.name = name;
        this.context = context;
        this.project = project;
        this.priority = priority;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // ... etc (setters and getters)

}

这是我使用此 Task 类的Web服务中的方法:

and here's the method in my Web Service where I use this Task Class:

    @WebMethod()
    public Task addTask(String taskName, String taskContext, String taskProject, PriorityType taskPriority) {    
        ToDoList taskList = new ToDoList(); // some ToDoList class
        taskList = // some method from which I get an existing To-Do List

        // ADDING a Task to my To-Do List
        Task task = new Task(taskName, taskContext, taskProject, taskPriority);
        taskList.addTask(task);

        // Returning the created task as a result of calling this method
        return task;
    }

在编译我的代码时,我得到了这个异常输出:

While compiling my code, I am getting this Exception output:

    Exception in thread "main" com.sun.xml.ws.spi.db.DatabindingException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
    La clase tiene dos propiedades con el mismo nombre "context"
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.getContext()
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.context
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
    La clase tiene dos propiedades con el mismo nombre "name"
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.getName()
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.name
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
    La clase tiene dos propiedades con el mismo nombre "priority"
        this problem is related to the following location:
            at public bigws.todows.PriorityType bigws.todows.Task.getPriority()
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
        this problem is related to the following location:
            at public bigws.todows.PriorityType bigws.todows.Task.priority
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
    La clase tiene dos propiedades con el mismo nombre "project"
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.getProject()
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse
        this problem is related to the following location:
            at public java.lang.String bigws.todows.Task.project
            at bigws.todows.Task
            at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
            at bigws.todows.jaxws.AddTaskResponse

        at com.sun.xml.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:104)
        at com.sun.xml.ws.spi.db.BindingContextFactory.create(BindingContextFactory.java:182)
        at com.sun.xml.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:218)
        at com.sun.xml.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:191)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.xml.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:191)
        at com.sun.xml.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:110)
        at com.sun.xml.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:339)
        at com.sun.xml.ws.db.DatabindingImpl.<init>(DatabindingImpl.java:100)
        at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:74)
        at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:58)
        at com.sun.xml.ws.db.DatabindingFactoryImpl.createRuntime(DatabindingFactoryImpl.java:127)
        at com.sun.xml.ws.server.EndpointFactory.createSEIModel(EndpointFactory.java:487)
        at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:283)
        at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:158)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:577)
        at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:560)
        at com.sun.xml.ws.transport.http.server.EndpointImpl.createEndpoint(EndpointImpl.java:323)
        at com.sun.xm

l.ws.transport.http.server.EndpointImpl.publish(EndpointImpl.java:246)
    at com.sun.xml.ws.spi.ProviderImpl.createAndPublishEndpoint(ProviderImpl.java:134)
    at javax.xml.ws.Endpoint.publish(Unknown Source)
    at bigws.todows.Server.main(Server.java:8)
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 4 counts of IllegalAnnotationExceptions
La clase tiene dos propiedades con el mismo nombre "context"
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.getContext()
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.context
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
La clase tiene dos propiedades con el mismo nombre "name"
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.getName()
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.name
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
La clase tiene dos propiedades con el mismo nombre "priority"
    this problem is related to the following location:
        at public bigws.todows.PriorityType bigws.todows.Task.getPriority()
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
    this problem is related to the following location:
        at public bigws.todows.PriorityType bigws.todows.Task.priority
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
La clase tiene dos propiedades con el mismo nombre "project"
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.getProject()
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
    this problem is related to the following location:
        at public java.lang.String bigws.todows.Task.project
        at bigws.todows.Task
        at public bigws.todows.Task bigws.todows.jaxws.AddTaskResponse._return
        at bigws.todows.jaxws.AddTaskResponse
at com.sun.xml.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:106)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:471)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:303)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:142)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1174)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
at com.sun.xml.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:168)
at com.sun.xml.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:112)
at com.sun.xml.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:113)
at com.sun.xml.ws.db.glassfish.JAXBRIContextFactory.newContext(JAXBRIContextFactory.java:92)
... 21 more

我还尝试为我的 Task 类别私有类变量赋予不同的名称,例如:

I have also tried giving my Task Class private class variables a different name, for example:

public class Task {

    public String taskName;
    public String taskContext;
    public String taskProject;
    public PriorityType taskPriority;

    // etc. 
}

这样,我的代码可以完美地编译并且Web服务可以正常工作,但是我得到的 SOAP XML响应具有重复的变量.使用 SoapUI 工具,这是通过调用 addTask()方法重命名我的类变量后得到的响应:

This way my code compiles perfectly and my Web Service works fine, but I get my SOAP XML Response with duplicated variables. Using SoapUI tool, this is the response I get once my class variables have been renamed by calling the addTask() method:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:addTaskResponse xmlns:ns2="http://todows.bigws/">
         <return>
            <taskName>Task Name</taskName>
            <taskContext>Task Context</taskContext>
            <taskProject>Task Project</taskProject>
            <taskPriority>LOW</taskPriority>
            <context>Task Context</context>
            <name>Task Name</name>
            <priority>LOW</priority>
            <project>Task Project</project>
         </return>
      </ns2:addTaskResponse>
   </S:Body>
</S:Envelope>

我的所有Web服务代码都可以找到此处:Github回购.

All my Web Service code can be found HERE: Github Repo.

为什么会这样?谢谢!

推荐答案

JAXB 通过查找 JavaBeans 之后的 properties 查找对象中的属性>约定和公共字段.以下示例显示了几个可以读取(get)和写入(set)的属性.请注意,JavaBeans属性不需要基础字段具有相同的名称.其他组合也是可能的.例如,只读属性具有getter方法,但没有setter.只写属性仅具有setter方法.布尔属性的一种特殊情况允许使用is而不是get定义访问器方法.如果 JAXB 发现冲突(例如,名为name的公共字段和名为getName的吸气剂),它将失败.

JAXB discovers properties in a object by looking for properties that follow JavaBeans convention and public fields. The following example shows several properties than can be read (get) and written (set). Note that a JavaBeans property does not require that the underlying field has the same name. Other combinations are also possible. A read-only property, for example, has a getter method but no setter. A write-only property has a setter method only. A special case for boolean properties allows the accessor method to be defined using is instead of get. If JAXB discovers a clash (e.g. a public field named name and a getter named getName), it will fail.

public class Example {
   public String  first;         // visible -> maps to <first>
   private String second;        
   private String third;         
   private boolean fourth;       
   public String getSecond() {   // visible -> maps to <second>
      return second;
   }
   public void setSecond(String s) 
      second = s;
   }
   public String getSecondBis() {   // visible -> maps to <secondBis>
      return third;
   }
   public void setSecondBis(String s) 
      third = s;
   }
   public boolean isFourth() {   // visible -> maps to <fourth>
      return fourth;
   }
   public vois setFourth(boolean b) 
      fourth = b;
   }
   public String getFirst() {   // invalid!!! getFirst clashes with public first
      return first;
   }
   public void setFirst(String s) 
      first = s;
   }
}

这篇关于JAXWS-RT:获取com.sun.xml.ws.spi.db.DatabindingException绑定一个简单的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆