pojo对象,要有默认构造方法
package com.jing.cxf.vo;
import java.io.Serializable;
public class User implements Serializable{
String name;
String age;
String sex;
public User(){
}
public User(String name,String age,String sex){
this.name=name;
this.age=age;
this.sex=sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
2.service接口
package com.jing.cxf;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.jing.cxf.vo.User;
@WebService
public interface HelloWorld {
@WebMethod
String sayHi(@WebParam(name="name") String text);
@WebMethod
User getUser(@WebParam(name="username") String username);
}
3.实现类:
package com.jing.cxf;
import com.jing.cxf.vo.User;
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String name) {
String msg = "Hello " + name + "!";
System.out.println(">>>>>>>>>>hello"+name);
return msg;
}
public User getUser(String username) {
System.out.println(">>>>>>>>>请求的用户是:"+username);
User user = new User(username,"18","男");
return user;
}
}
4.spring.xml 和web.xml和上一篇一样。
运行test3看效果:
package com.jing.cxf;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Test;
import com.jing.cxf.vo.User;
/**
* @author jingguoqiang
* @desc org.apache.cxf jar包写的客户端
*/
public class ClientFangWen {
/**
* @Description: 测试和spring集成,接口返回的是pojo
* @author: jingguoqiang
* @date 2015-9-19 下午4:20:56
*/
@Test
public void test3() {
//jaxws代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//访问服务类接口 ,注意:必须是接口
factory.setServiceClass(HelloWorld.class);
//访问地址
factory.setAddress("http://localhost:8081/Zt/ws/HelloWorld");
//得到服务类接口
HelloWorld helloworld = (HelloWorld) factory.create();
User usr = helloworld.getUser("XIAOJING");
System.out.println("返回信息:"+usr);
}
}