我正在使用JAXWS-RT构建一个小型SOAP Web服务;我有一个看起来像这样的简单类:
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)
}
这是我的Web服务中使用任务类的方法:
@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;
}
在编译我的代码时,我得到了异常输出:
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
我还试过给我的任务类私有类变量一个不同的名称,例如:
public class Task {
public String taskName;
public String taskContext;
public String taskProject;
public PriorityType taskPriority;
// etc.
}
这样我的代码编译得很完美,我的Web服务工作正常,但我得到了带有重复变量的 SOAP XML响应。使用 SoapUI 工具,这是我通过调用 addTask()方法重命名我的类变量后得到的响应:
<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>
可以找到我的所有网络服务代码HERE: Github Repo。
为什么会这样?谢谢!
答案 0 :(得分:3)
JAXB 通过查找遵循 JavaBeans 约定和公共字段的属性来发现对象中的属性。以下示例显示了几个可以读取(get
)和写入(set
)的属性。请注意,JavaBeans属性不要求基础字段具有相同的名称。其他组合也是可能的。例如,只读属性具有getter方法但没有setter。只写属性仅具有setter方法。布尔属性的一个特例允许使用is
而不是get
来定义访问器方法。如果 JAXB 发现冲突(例如名为name
的公共字段和名为getName
的getter),则会失败。
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;
}
}
答案 1 :(得分:2)
我认为Task对象的所有属性都应该是私有的。例外情况表明您有重复的属性。在服务中,您使用两种方法返回List,因此Task对象必须与JAXB兼容,或者换句话说,至少是Java Bean。