首页 > 代码库 > maven+springmvc+cxf 实现简单webservice小例子
maven+springmvc+cxf 实现简单webservice小例子
1.首先你需要创建一个maven项目【当然是web项目】
2.pom.xml添加以下
<properties>
<cxf.version>2.2.3</cxf.version>
</properties>
<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>
没错只需要引入这两个,然后cxf需要的其他jar也会自动添加到项目中。
3.在项目的web.xml中添加
<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>/cxf/*</url-pattern>
</servlet-mapping>
4.具体代码的实现如下:
4.1写一个接口IHelloService.java
package com.niuniu.service;
import javax.jws.WebService;
@WebService
public interface IHelloService {
public String wolaile(String userName);
}
4.2写接口的实现HelloServiceImpl.java
package com.niuniu.service.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.niuniu.service.IHelloService;
@WebService(endpointInterface="com.niuniu.service.IHelloService",serviceName="helloService")
@Component
public class HelloServiceImpl implements IHelloService {
@Override
public String wolaile(String userName) {
return "Hello, wolaile, " + userName;
}
}
4.3配置文件如下:
<?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: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://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-servlet.xml" />
<jaxws:endpoint id="helloService" implementor="com.niuniu.service.impl.HelloServiceImpl" address="/cxfService" />
</beans>
spring的相关配置就不在这里叙述了。
【如果没有搭过spring的web项目的,可以先搜索一个能跑起来的web项目,然后把这些配置放到能跑起来的项目中就可以了】
把项目发布到tomcat上,然后访问http://localhost:8080/ownweb/cxf/cxfService?wsdl,看下能否成功访问。
【ownweb是你的项目名,cxf是在web.xml中配置的<url-pattern>,cxfService是4.3中address的值,根据自己的需要可以修改下】
maven+springmvc+cxf 实现简单webservice小例子