我正在使用Netbeans来创建一个Web应用程序项目。当我创建新的Web服务(代码优先WS)时,IDE添加了METRO2.0库。 SOAP web服务很好地部署在我的apache Tomcat 6中。但是,当我发送一个复杂类型时,客户端无法访问客户端中的sended对象的方法。 假设我有一个名为 Person 的类和一个操作:
@WebMethod(operationName = "getOnePerson")
public Person getOnePerson() {
return new Person("MyName", "MySurname", 24);
}
Person类:
public class Person {
private String name, surname;
private int age;
public Person() {
}
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
}
那么如何让客户知道 Person 方法? 谢谢 编辑:我试图通过添加Pesron的属性来更新我的XSD文件:
<xs:complexType name="person">
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="surname" type="xs:string" />
<xs:attribute name="age" type="xs:int" />
</xs:complexType>
通过添加此部分,客户端知道吸气剂和放大器。类Person的setter但是在尝试运行程序时,所有getter都返回null(客户端):
public class Main {
public static void main(String[] args) {
try {
Person p = getOnePerson();
System.out.println(p);
System.out.println(p.getSurname());
System.out.println(p.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
private static Person getOnePerson() {
com.company.ws.BeanWebService_Service service = new com.company.ws.BeanWebService_Service();
com.company.ws.BeanWebService port = service.getBeanWebServicePort();
return port.getOnePerson();
}
}
返回: com.company.ws.Person@d75415 空值 空 那么请你告诉我为什么客户端不知道类属性的不同值?
答案 0 :(得分:0)
由于JAX-WS使用JAXB来编组/解组对象,我建议:
如果您正在使用Maven(强烈推荐),那么您可以使用以下插件从XSD生成JAXB类:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatePackage>com.yourcompany.somecomponent.somepackage.jaxb</generatePackage>
</configuration>
</execution>
</executions>
</plugin>
预期XSD&amp; xjb(JAXB定制文件)位置:src / main / resources
答案 1 :(得分:0)
我找到并解决了问题:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType (name = "Person", propOrder = {"name","surname", "age"})
public class Person {
@XmlElement(name="name",required = true)
private String name;
@XmlElement(name="surname",required = true)
private String surname;
@XmlElement(name="age",required = true)
private int age;
//GETTER & SETTER
}
谢谢!