使用Apache CXF搭建基于SOAP的Web Service
1)创建domain实体对象
package com.domain;
public class User {
private int id;
private String name;
private String address;
private String email;
public User(){}
public User(String name,String address,String email){
this.name = name;
this.address = address;
this.email = email;
}
---------
get和set方法
---------
@Override
public String toString() {
return this.name+","+this.email+","+this.address;
}
}
2)创建业务处理接口
package com.service;
import com.domain.User;
public interface IUserManager {
public User getUserById(int id);
public int setUser(String name,String address,String email);
}
3)创建业务实现类
package com.service;
import com.domain.User;
import com.service.IUserManager;
public class UserManager implements IUserManager{
@Override
public User getUserById(int id) {
User user = new User();
user.setId(1);
user.setName("Rickesy");
user.setAddress("china");
user.setEmail("Rickesy@test.com");
return user;
}
@Override
public int setUser(String name,String address,String email) {
return 0;
}
}
4)创建Web Service 接口
package com.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.domain.User;
@WebService
public interface IService {
public User getUserById(@WebParam(name="id") int id);
public int setUser(String name,String address,String email);
}
5)创建Web Service 实现类
package com.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.domain.User;
import com.service.IUserManager;
import com.ws.IService;
@WebService
public class Service implements IService{
private IUserManager userManager;
public IUserManager getUserManager() {
return userManager;
}
public void setUserManager(IUserManager userManager) {
this.userManager = userManager;
}
@Override
public User getUserById(@WebParam(name="id") int id) {
return userManager.getUserById(id);
}
@Override
public int setUser(String name,String email,String address) {
return userManager.setUser(name,email,address);
}
}
6)配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<bean id="userManagerImpl" class="com.service.UserManagerImpl"></bean>
<bean id="userManagerService" class="com.ws.ServiceImpl">
<property name="userManager" ref="userManagerImpl"></property>
</bean>
<jaxws:endpoint id="userService"
implementor="#userManagerService"
address="/userManager">
</jaxws:endpoint>
</beans>
7)配置web.xml
<listener>
<listene-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
8)在客户端根据wsdl文档生成代码
可通过CXF 中wsdl2java 工具
URL:http://localhost:8080/SOAPWebService/UserManager?wsdl
9)在客户端编写测试代码
Apache CXF 代理工厂
package com.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cxf_server.IService;
import cxf_server.User;
public class ClientMain {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IService.class);
factory.setAddress(
"http://localhost:8080/SOAPWebService/userManager");
Iservice service = (IService) factory.create();
User user = service.getUserById(1);
System.out.println(user.getEmail());
System.out.println(user.getAddress());
}
}
JAX-WS规范
package com.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cxf_server.IService;
import cxf_server.User;
public class ClientMain {
public static void main(String[] args) {
Service service = (IService) new
ServiceImplService().getServiceImplPort();
User user = service.getUserById(1);
System.out.println(user.getEmail());
System.out.println(user.getAddress());
}
}