Spring集成CXF
程序员文章站
2022-07-13 12:05:49
...
Spring集成CXF
零) jar依赖
一) server端
二) client端
零) jar依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-common</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-java2ws</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-validator</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-wsdlto-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-tools-wsdlto-databinding-jaxb</artifactId> <version>2.4.3</version> </dependency>
一) server端
package ws; import java.util.Date; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService(targetNamespace = "http://github.com/yingzhuo/ws-test/ws") @SOAPBinding(style = Style.RPC) public interface Service { @WebMethod public Date getDate(); }
package ws.impl; import java.util.Date; import ws.Service; public class ServiceImpl implements Service { public Date getDate() { return new Date(); } }
<?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:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.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" /> <bean id="serviceBean" class="ws.impl.ServiceImpl" /> <jaxws:server id="service" serviceClass="ws.Service" address="/service"> <jaxws:serviceBean> <ref bean="serviceBean" /> </jaxws:serviceBean> </jaxws:server> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-cxf-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
二) client端
package main; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import ws.Service; public class Main { @SuppressWarnings("all") public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-cxf-client.xml"); Service service = ac.getBean("client", Service.class); System.out.println(service.getDate()); } }
<?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:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.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:client id="client" serviceClass="ws.Service" address="http://localhost:8080/service"/> </beans>
上一篇: 比较Axis、Axis2和Apache CXF ws
下一篇: jquery禁用按钮60秒
推荐阅读
-
Spring Cloud Gateway网关XSS过滤Filter
-
从安装到使用springboot集成RocketMq4.5.2
-
Spring Cloud动态配置实现原理与源码分析
-
Spring+Junit4进行接口测试实例代码
-
在Spring Boot中使用swagger-bootstrap-ui的方法
-
spring mvc中直接注入的HttpServletRequst安全吗
-
Spring boot工具类静态属性注入及多环境配置详解
-
Spring+Hibernate+Struts(SSH)框架整合实战
-
Spring Cloud Zuul的重试配置详解
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码