最近在学jax-ws,有一个问题一直搞不定,特来请教各位大牛。
就是一个webservice,通过两种方法实现客户端代码。但使用(1)QName和Service 的可以执行成功,而使用(2)wsimport的,传递的参数一直为null。代码如下:
//服务端代码
//HelloWorld.java
import test.model.People;@WebService
@SOAPBinding(style=Style.DOCUMENT)
public interface HelloWorld {
@WebMethod
    public String hello(People people);
    @WebMethod
    public String say(String str);}//HelloWorldImpl.java
@WebService(endpointInterface="test.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{ @Override
public String hello(People people) {
        //在下面的两种调用方法中,第一种调用成功。
        //第二种调用时,people为null
return "Hello, " + people.getFirstName() + "." + people.getLastName()
+ "\n date is :" + people.getDate().toString()
+ "\n type is :" + people.getType().toString();
}

@Override
public String say(String str) {
return str;
}

public static void main(String[] args) {
Endpoint.publish("http://localhost:8999/ws/hello", new HelloWorldImpl());
}
}//People.java
@XmlRootElement
public class People {

public enum Type {
AA, BB, CC;
}

private String firstName;
private String lastName;
private Date date;
private Type type;
public People() {
}

public People(String first, String last, Date date, Type type) {
this.firstName = first;
this.lastName = last;
this.date = date;
this.type = type;
}

public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlJavaTypeAdapter(test.model.DateAdapter.class)
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Type getType() {
return type;
} public void setType(Type type) {
this.type = type;
}
}//DataAdapter.java
public class DateAdapter extends XmlAdapter<Long, Date>{ @Override
public Date unmarshal(Long v) throws Exception {
return new Date(v);
} @Override
public Long marshal(Date v) throws Exception {
return v.getTime();
}
}//客户端代码(1),使用 QName和Service, 这个能执行成功
public static void main(String[] args) {
URL url = new URL("http://localhost:8999/ws/hello");
QName qname = new QName("http://ws.test/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.hello(new People("aaa", "bbb", new Date(), Type.AA)));
}//客户端代码(2)
//先使用wsimport,根据webservice的wsdl文件生成一些类。再调用ws
//"D:\Program Files\Java\jdk1.6.0_33\bin\wsimport.exe" -s .
// http://localhost:8999/ws/hello?wsdl//然后用下面代码调用web service 
public class Main {
public static void main(String[] args) {
HelloWorldImplService service = new HelloWorldImplService();
HelloWorld hello = service.getHelloWorldImplPort();
People people = new People();
people.setFirstName("first");
people.setLastName("last");
people.setType(Type.AA);
people.setDate(new Date().getTime());
            //调用hello.hello时服务器端的参数为null
System.out.println(hello.hello(people)); 
}
}java webservicejax-ws