1.导入CXF的jar包
2.写对外发布的接口
package com.service;
public interface Service {
public String SendMail(String xml);//POST方法参数常规写法
public String getMail(@PathParam("appID")String appID){};//GET方法的路径参数要这样写
}
3.写实现类
POST方法
package com.service.impl;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.service.Service;
@Path("/xml") //发布路径参数,在配置文件中配置的
@Produces("**")
public String SendMail(String xml) {
System.out.println("收到啦"); //测试使用,这里写业务逻辑
return "0";
}
}
GET方法
@GET
@Path(value="/getMail")
@Consumes("*/*")
public String getMail(@PathParam("appID")String appID) {
//业务逻辑
return null;
}
4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<!--spring管理-->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
5.beans.xml //跟web.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.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="Service" class="com.service.impl.ServiceImpl" />
<jaxrs:server id="restfulService" address="/public">
<jaxrs:serviceBeans>
<ref bean="Service"/>
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
6.打包部署
7.浏览器输入
http://localhost:8080/Test //Test输入工程名
显示

点开后看到自己发布的2个接口