最近由于项目需要,在一个已经正常的系统中添加webservice,原系统由spring framework(3.2.0.RELEASE)、spring mvc(3.2.0.RELEASE)、spring security(3.1.0.RELEASE)搭建,maven(3.1)管理包依赖。
1.首先编辑原pom.xml,添加apache cxf(2.7.10)包:
<!-- Apache CXF (start)--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-core</artifactId> <version>${cxf.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Apache CXF (end)-->
2.在src目录添加目录com.ltpc.hwws.wservice包,包中创建接口HelloWorld,如下:
import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { public String sayHello(@WebParam(name="name") String name); }
3.在wservice包下创建impl包,impl包中添加接口HelloWorld的实现类HelloWorldImpl,如下:
import com.itrus.ukey.wservice.HelloWorld; public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String name) { System.out.println(name+"say hello"); return "Hello "+name+",This is world."; } }
4.在src下添加webservice配置文件applicationContext-ws.xml,文件内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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="helloService" class="com.ltpc.hwws.wservice.impl.HelloWorldImpl" /> <jaxws:endpoint id="helloWService" implementorClass="com.ltpc.hwws.wservice.HelloWorld" address="/hellows"> <jaxws:implementor> <ref bean="helloService"/> </jaxws:implementor> </jaxws:endpoint> </beans>
5.修改配置web.xml文件,添加cxf servlet,如下:
<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>/wservices/*</url-pattern> </servlet-mapping>
同时要添加对applicationContext-ws.xml文件的加载,全部配置完之后的web.xml文件,如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>ltpc</display-name> <description>Roo generated ltpc application</description> <context-param> <param-name>defaultHtmlEscape</param-name> <param-value>true</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <filter-name>HttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>HttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/wservices/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>ukey</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/webmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ukey</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6.配置完成,启动tomcat,在浏览器中访问http://127.0.0.1:8888/ltpc/wservices/hellows?wsdl确定是否正常工作。
至此工作完成,用于备忘。
相关推荐
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <value>...
- **服务端集成**:Spring可以管理CXF的bean,例如WebService服务器实例、服务实现类等,通过Spring配置文件进行声明式配置,使得Web服务的启动和停止更加便捷。 - **客户端集成**:Spring也支持CXF的Web服务...
<groupId>org.springframework <artifactId>spring-webmvc <version>5.x.x.RELEASE <groupId>org.apache.cxf <artifactId>cxf-rt-frontend-jaxws <version>3.x.x.RELEASE <groupId>org.apache.cxf ...
<listener-class>org.springframework.web.context.ContextLoaderListener <servlet-name>CXFServlet <servlet-class>org.apache.cxf.transport.servlet.CXFServlet <servlet-name>CXFServlet ...
<bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus"> <!-- 添加自定义拦截器 --> ``` 2. **CXF服务提供者配置** 接下来,我们需要定义CXF的服务提供者,这通常是一个实现了JAX-WS接口的...
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <servlet-name>...
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:cxf="http://cxf.apache.org/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=...
SSM整合CXF是Java开发中的一个重要话题,它涉及到Spring、Spring MVC和MyBatis三个框架与Apache CXF服务框架的集成。这篇文章将深入探讨这一技术整合的核心概念、优势以及实施步骤。 首先,Spring(Spring ...
SOAP WebService: JAX-WS2.0的注解 + Apache CXF 无疑是最成熟 Restful Service: JAX-RS 1.0 + Jersey/CXF,够标准,但直接使用Spring MVC能使架构更简单 **展现层:**JSP2.0且尽量使用JSP EL而不是taglib,万一要...