CXF可以发布的SOAP Service 和 RESTful Service。
一、SOAP Service
1.引入jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
2.配置web.xml
<!-- Spring和mybatis的配置文件,Spring和CXF的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-cxf.xml</param-value>
</context-param>
<!--定义一个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>/webservice/*</url-pattern>
</servlet-mapping>
3.编写接口
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface SayHelloService {
@WebMethod
public String SayHello(String name);
}
4.实现接口
import javax.jws.WebService;
import com.it.service.SayHelloService;
@WebService(endpointInterface="com.it.service.SayHelloService")
public class SayHelloServiceImpl implements SayHelloService {
public String SayHello(String name) {
return "你好"+name;
}
}
5.配置spring-cxf.xml
<?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.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 引cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<jaxws:endpoint
id="SayHelloService"
implementor="com.it.service.impl.SayHelloServiceImpl"
address="/SayHelloService"/>
</beans>
6.访问http://ip:port/项目/webservice可以看到SOAP Service,就说明成功了。
二、RESTful Service
1.引入jar包(在上面的基础之上添加)
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.6</version>
</dependency>
<!-- CXF jaxrs description 依赖 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>3.1.6</version>
</dependency>
2.
配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-cxf.xml,classpath:spring-cxf2.xml</param-value>
</context-param>
3.
编写接口
public interface TestRestService {
public String sayHello( String userName);
}
4.实现接口
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.stereotype.Component;
import com.it.service.SayHelloService;
@Component
@Path("/test")
public class TestRestServiceImpl implements SayHelloService {
@GET
@Path("/hello/{username}")
//@Produces({ MediaType.APPLICATION_XML })
public String SayHello(@PathParam("username") String name) {
String rs = "你好"+name;
System.out.println(rs);
return rs;
}
}
5.
配置spring-cxf2.xml
<?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: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/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
">
<!-- 引cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<jaxrs:server id="TestRestService" address="/TestRestService">
<jaxrs:serviceBeans>
<ref bean="testRestServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
6.
访问
http://ip:port/项目/webservice可以看到RESTful Service,就说明成功了。
7.RESTful Service 可以这么使用http://ip:port/项目名/webservice/{address}/{path...}/{参数...}
如:http://127.0.0.1/SSM/webservice/TestRestService/test/hello/superman
三、访问地址中的webservice是在web.xml里进行配置的。具体位置如下。
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>