JAXWS-RT: getting com.sun.xml.ws.spi.db.Databindin

2019-08-11 07:44发布

问题:

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)

}

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

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. 
}

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>

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

Why can this be happening? Thank you!

回答1:

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 vois setSecond(String s) 
      second = s;
   }
   public String getSecondBis() {   // visible -> maps to <secondBis>
      return third;
   }
   public vois 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 vois setFirst(String s) 
      first = s;
   }
}


回答2:

I think all the properties of your Task object shoud be private. The exception says you have duplicated properties. In the service you return List in two methods, so Task object has to be JAXB compatible or in other words, at least a Java Bean.