CXF框架的restful webservice 环境配置(一) 服务端

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 class ServiceImpl implements Service{
@POST
@Path(value="/sendMail")
@Consumes("*/*")
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个接口





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值