java webService之CXF的使用
程序员文章站
2022-07-10 16:20:42
...
使用场景:华为VOD系统与媒资系统的接口,资产数据(元数据XML、正片、片花、海报等)采用FTP方式传输,系统间消息采用WebService方式进行通信。
框架:采用webService的CXF框架
具体实现操作:在已有的项目中集成CXF框架,提供webService服务接口;
1、依赖CXF所需的依赖包
2、开发接口样例:
UserInfo.java对象作为返回值
UserService.java接口
UserServiceImpl.java 接口实现类
测试类:
这里主要是测试接口是否可用;通过http://localhost:8080/user/service??wsdl浏览器链接可以得到
当然如要要测试接口;可以用SOAPUI工具测试;如图:
上面测试了web service接口的开发;
接下来需要将UserService集成到SpringMVC中;
javaEE中CXF配置:
添加spring-cxf.xml配置文件到resource目录
javaEE项目中web.xml配置:其中要配置Spring-cxf.xml
框架:采用webService的CXF框架
具体实现操作:在已有的项目中集成CXF框架,提供webService服务接口;
1、依赖CXF所需的依赖包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-security</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-ws-policy</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-bundle-jaxrs</artifactId> <version>2.7.13</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency>
2、开发接口样例:
UserInfo.java对象作为返回值
package com.jiahejiankang.operation.api.test; public class UserInfo { private String name; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
UserService.java接口
package com.jiahejiankang.operation.api.test; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; @WebService(serviceName="userService",targetNamespace = "http://localhost/user/service") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface UserService { @WebMethod @WebResult(name = "userReturn", partName = "userReturn") UserInfo getUserInfo(@WebParam(name = "name", partName = "name")String name, String phone); }
UserServiceImpl.java 接口实现类
package com.jiahejiankang.operation.api.test; public class UserServiceImpl implements UserService { public UserInfo getUserInfo(String name, String phone) { UserInfo userInfo = new UserInfo(); userInfo.setName(name); userInfo.setPhone(phone); System.out.println(name+":"+phone); return userInfo; } }
测试类:
package com.jiahejiankang.operation.api.test; import javax.xml.ws.Endpoint; public class TestMain { public static void main(String[] args) { System.out.println("Server is starting..."); UserService userService = new UserServiceImpl(); Endpoint.publish("http://localhost:8080/user/service",userService); System.out.println("Server is started..."); } }
这里主要是测试接口是否可用;通过http://localhost:8080/user/service??wsdl浏览器链接可以得到
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test.api.operation.jiahejiankang.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://localhost/user/service" name="UserServiceImplService" targetNamespace="http://test.api.operation.jiahejiankang.com/"> <wsdl:import location="http://localhost:8080/user/service?wsdl=UserService.wsdl" namespace="http://localhost/user/service"></wsdl:import> <wsdl:binding name="UserServiceImplServiceSoapBinding" type="ns1:UserService"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="getUserInfo"> <soap:operation soapAction="" style="rpc"/> <wsdl:input name="getUserInfo"> <soap:body namespace="http://localhost/user/service" use="literal"/> </wsdl:input> <wsdl:output name="getUserInfoResponse"> <soap:body namespace="http://localhost/user/service" use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="UserServiceImplService"> <wsdl:port binding="tns:UserServiceImplServiceSoapBinding" name="UserServiceImplPort"> <soap:address location="http://localhost:8080/user/service"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
当然如要要测试接口;可以用SOAPUI工具测试;如图:
上面测试了web service接口的开发;
接下来需要将UserService集成到SpringMVC中;
javaEE中CXF配置:
添加spring-cxf.xml配置文件到resource目录
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="userService" address="/UserService" implementor="com.jiahejiankang.operation.api.test.UserServiceImpl" /> <bean id="client" class="com.jiahejiankang.operation.api.wsdl.notice.VODNoticeService" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.jiahejiankang.operation.api.test.UserService"/> <property name="address" value="http://10.10.40.246:8888/user/service?wsdl"/> </bean> </beans>
javaEE项目中web.xml配置:其中要配置Spring-cxf.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:spring/spring-core.xml classpath*:spring/spring-cxf.xml </param-value> </context-param> <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>/user/*</url-pattern> </servlet-mapping>
下一篇: 低血糖饮食要均衡