WS_Server:
package cxf.ws;
import java.util.List;
import javax.jws.WebService;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
@WebService
public interface HelloWorld {
String sayHi(String name);
List<Cat> getCatByUser(User user);
}
package cxf.ws.impl;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import cxf.ws.HelloWorld;
import cxf.ws.domain.Cat;
import cxf.ws.domain.User;
import cxf.ws.service.UserService;
import cxf.ws.service.impl.UserServiceImpl;
@WebService(endpointInterface="cxf.ws.HelloWorld",serviceName="HelloWorldWS")
public class HelloWorldWS implements HelloWorld {
@Override
public String sayHi(String name) {
return name+",您好,"+"现在时间是:"+new Date();
}
/**
* 在实际的应用中,webService只会调用业务组件,暴露接口。
*/
@Override
public List<Cat> getCatByUser(User user) {
UserService us=new UserServiceImpl();
return us.getCatByUser(user);
}
}
wsdl2java -frontend jaxws21 http://10.0.6.17/fightUp?wsdl
在 wsdl2java url出错时,采用的命令。
WS_Client
package lee;
import java.util.List;
import cxf.ws.Cat;
import cxf.ws.HelloWorld;
import cxf.ws.User;
import cxf.ws.impl.HelloWorldWS;
public class ClientMain {
public static void main(String[] args) {
HelloWorldWS factory=new HelloWorldWS();
HelloWorld hw=factory.getHelloWorldWSPort();
System.out.println(hw.sayHi("孙悟空"));
User user=new User();
user.setName("Janey");
user.setPass("202053");
List<Cat> cats=hw.getCatByUser(user);
//server里面带参数的构造方法在client里面没有起作用,而toString也同样不起作用。
for(Cat cat:cats){
System.out.println(cat.getName()+":"+cat.getColor());
}
}
}