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());
}
}
}