1、pom.xml添加cxf相关依赖,本人配置。
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.7.5</version> <type>pom</type> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.5</version> </dependency>
2、web.xml里面配置cxf servlet
<servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/webservice/*</url-pattern> </servlet-mapping>
3、 webservice配置文件
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="userWs" implementor="com.fpi.prd.test.service.Impl.UserTestServiceImpl" address="/user" /> </beans>
4、定义webservice服务端接口,参照
@WebService public interface UserTestService { @WebMethod public String getUsrById(String id); }
public class UserTestServiceImpl implements UserTestService{ @Override public String getUsrById(String id) { List<User> users = new ArrayList<User>(); User user = new User("张三","10"); JSONObject result = JSONObject.fromObject(user); users.add(user); User userb = new User("李四","20"); users.add(userb); JSONArray jaonArray = JSONArray.fromObject(users); return jaonArray.toString(); } }
4.1注意事项,数据传输可以对象,最好以JSon字符串传输,客户端反解析使用方便。
5、客户端调用,使用的是axis2的RPC方式,代码:
import java.util.List; import javax.xml.namespace.QName; import net.sf.json.JSONArray; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.commons.collections.CollectionUtils; import com.fpi.prd.test.po.User; /** * 模拟调用 * @author 07845 * */ public class ClientRevoke { //模拟客户端 private RPCServiceClient rpcServiceClient; private Options options; private EndpointReference endpointReference; /** * 初始化客户端 * @param endpoint * @throws AxisFault */ public ClientRevoke(String endpoint)throws AxisFault{ rpcServiceClient = new RPCServiceClient(); options = rpcServiceClient.getOptions(); endpointReference = new EndpointReference(endpoint); options.setTo(endpointReference); } /** * 模拟调用 * @param targetNamespace * @param opName * @param params * @param opReturnType * @return */ public Object[] getResult(String targetNamespace,String opName, Object[] params,Class<?>[] opReturnType){ try{ QName qName = new QName(targetNamespace,opName); return rpcServiceClient.invokeBlocking(qName, params, opReturnType); }catch(Exception e){ e.printStackTrace(); } return null; } public static void main(String[] args)throws AxisFault, ClassNotFoundException{ String endpoint = "http://localhost:8080/demo/services/user"; String targetNameSpace = "http://service.test.prd.fpi.com/"; ClientRevoke clientRevoke = new ClientRevoke(endpoint); String opName = "getUsrById"; Object[] parames = new Object[]{"1"}; Class<?>[] opReturnType = new Class[] {String.class }; Object[] response = clientRevoke.getResult(targetNameSpace, opName, parames, opReturnType); JSONArray jsonObject = JSONArray.fromObject(response[0]); @SuppressWarnings("unchecked") List<User> users = (List<User>)JSONArray.toCollection(jsonObject, User.class); if(CollectionUtils.isNotEmpty(users)){ for(User user:users){ System.out.println(user.getAge()+" "+user.getName()); } } } }