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()); } } } }
相关推荐
基于Soap协议下CXF框架开发Webservice
第16讲-SOAP与CXF开发WebService.pptx
通过CXF开发webService接口(soap协议、maven项目).pdf通过CXF开发webService接口(soap协议、maven项目).pdf
Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法(重点) Soap1.1和soap1.2区别: 跟踪soap协议 综合案例: 使用webservice传输xml...
用于使用CXF打印SOAP报文,记录WebService日志~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计...
基于 cxf 2.7.5 开发的 webservice [soap restful]
使用CXF技术发布webservice,以及客户端的调用方法(可直接复制使用),基于soap12
rest soap cxf webservice maven springmvc
启动后访问http://localhost:8080/cxf-ws-restful-spring-server/ws63
cxf实现soap webservice服务,服务端\客户端均与spring结合使用,多种方式实现,花了三天时间弄懂,还有许多不懂的地方,欢迎大家交流.参考我的博文:http://blog.csdn.net/qq_33764491/article/details/78831010
Spring+CXF+Maven+SOAP方式搭建WebService服务端demo测试用例
范例包含3个部分,分别为基于soap消息的cxf开发,基于rest风格的cxf开发,spring发布soap和rest的webservice
CXF实现WebService,maven项目,博客,http://blog.csdn.net/binyulong/article/details/78764648
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- DAO --> ...
1. 支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。 2. Frontends:CXF 支持多种“Frontend”编程模型,...