设为首页 收藏本站
查看: 760|回复: 0

[经验分享] Apache CXF REST WebService简单应用

[复制链接]

尚未签到

发表于 2017-1-4 11:30:50 | 显示全部楼层 |阅读模式
  <p>      本文目的就项目中的Apache CXF中的REST WebService风格的展现。下面言归正传,看实惠,代码如下:
DSC0000.jpg

  </p>

package com.easyway.rest.webservice;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.ext.MessageContext;
/**
* Apache CXF REST方式发布WebService的总结
* 1.定义Apache CXF CXFServlet的服务发布拦截类
*    org.apache.cxf.transport.servlet.CXFServlet;
* 2.定义相关REST WebService的服务类
*      RestWebService
* 3.在相关的Spring的配置文件配置相关的参数
*   <pre>
*     <import resource="classpath:META-INF/cxf/cxf-all.xml"/>
*              
*<http-conf:conduit name="*.http-conduit">
*<http-conf:client ReceiveTimeout="600000" ConnectionTimeout="600000"/><!-- 10分钟 -->
*</http-conf:conduit>
*
*
*        <bean id="RestWSBean" class="com.easyway.rest.webservice.RestWebService"/>
*        
*        <jaxrs:server id="RestWS" address="/restWS">
*<jaxrs:serviceBeans>
*<ref bean="restService" />
*</jaxrs:serviceBeans>
*</jaxrs:server>
* </pre>
* 4.发布相关的Web服务
*   查看相关的服务:
*    例如:
*       http://localhost:8080/ApacheCXFREST/service/
*        ApacheCXFREST为Web项目上下文
*        service为Apache CXF在Web.xml容器中配置的拦截路径
*   查看WADL的路径:
*      http://localhost:8080/ApacheCXFREST/service/restWS?_wadl&_type=xml
*        ApacheCXFREST为Web项目上下文
*        service为Apache CXF在Web.xml容器中配置的拦截路径
*        restWS为Spring的中配置的REST服务的拦截的路径
*        _wadl&_type=xml为WADL的协议的格式
*        
*         <resources base="http://localhost:8080/ApacheCXFREST/service/restWS">
* <resource path="/rest">
* <method name="GET">
* ..............
*   请求的路径=resources元的base属性+resource元素的path属性
*      例如:http://localhost:8080/ApacheCXFREST/service/restWS/rest
* 5.请求相关的服务
*   例如
*
* @author longgangbai
*
*/
@Path("/rest")
public class RestWebService {
private MessageContext messageContext;

/**
* 存储发送的数据
* @param messageContext
*/
@Context
public void setMessageContext(MessageContext messageContext) {
this.messageContext = messageContext;
}
/**
*
* 领导慰问士兵们的
*
* @param username xx
* @return
*/
@GET()
@Consumes(MediaType.TEXT_PLAIN)
public String helloworld(){
return "xx说: 同志们辛苦了 !!   ";
}
/**
*   当需要Servlet环境采用注释中的内容获取客户端中的Servlet环境的中的各种信息
*   如上传,下载之类,可能需要知道比较多的信息
*   采用这种方式
*   
@SuppressWarnings("unchecked")
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String postMethod(MultivaluedMap<String, Object> formParams){
Map<String,Object> requestMap=null;
boolean isGet=false;
if(formParams!=null){
requestMap=(Map)formParams;
}else{
try {
this.messageContext.getHttpServletRequest().setCharacterEncoding("UTF-8");
this.messageContext.getHttpServletResponse().setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
requestMap=this.messageContext.getHttpServletRequest().getParameterMap();
isGet=true;
}
return "";
}
*/

/**
*广大士兵 回答
* @return
*/
@POST()
@Produces(MediaType.TEXT_PLAIN)
public String service(){
return "士兵说:为人民服务";
}
}

  spring关于rest配置如下:

<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
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
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">
<!-- 针对CXF发布服务的需要的配置的加载的导入 -->
<import resource="classpath:META-INF/cxf/cxf-all.xml"/>
<!-- 设置服务相关的请求的配置 -->        
<http-conf:conduit name="*.http-conduit">
<http-conf:client ReceiveTimeout="600000" ConnectionTimeout="600000"/><!-- 10分钟 -->
</http-conf:conduit>
<!--
设置需要发布的bean
-->
<bean id="RestWSBean" class="com.easyway.rest.webservice.RestWebService"/>
<!--
发布相关的WebService
-->
<jaxrs:server id="RestWS" address="/restWS">
<jaxrs:serviceBeans>
<ref bean="RestWSBean" />
</jaxrs:serviceBeans>
</jaxrs:server>

</beans>
  web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name>RESTWebServices</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContent-*-services.xml</param-value>
</context-param>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-323813-1-1.html 上篇帖子: Web 压力测试工具 --Apache AB 下篇帖子: 经典的apache+tomcat虚拟机配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表