日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

采用CXF+spring+restful创建一个web接口项目

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 采用CXF+spring+restful创建一个web接口项目 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這篇文章是http://blog.csdn.net/zxnlmj/article/details/28880303下面,加入的基礎上的restful特征

1、參加restful必jar包裹

jsr311-api-1.0.jar CXF與JAX-RS版本號相應問題,參考自:http://bioubiou.iteye.com/blog/1866871 CXF支持REST風格的Web服務:JAX-RS2.0(JSR-339)和JAX-RS1.1(JSR-311)的Java API。 CXF2.7.0支持JAX-RS2.0(不包含clientAPI如今 - 引入的新功能。但注意CXFclientAPI已經更新,支持新的過濾器,攔截器,異常類和響應API,再加上client的異步調用API)。

CXF2.6.x版本號,在2.5.x。2.4.x和2.3.x的支持JSR-311 API1.1和JAX-RS1.1 TCK符合。

CXF2.2.x的支持JSR-311 API1.0和JAX-RS1.0 TCK標準。 CXF的2.1.x支持JSR-311 API0.8。

本文選擇cxf-2.4.2.jar與jsr311-api-1.0.jar

2、開發restful服務

新建RestfulRegeditService.java接口

package zxn.ws.service;import java.io.IOException;import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.core.MediaType;@Path(value = "/") public interface RestfulRegeditService {@POST@Path("/regedit")@Consumes(MediaType.APPLICATION_JSON)public String regedit(String username, String password) throws IOException;} 新建RestfulRegeditServiceImpl.java接口
package zxn.ws.service.impl;import java.io.IOException;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 javax.ws.rs.core.Request; import javax.ws.rs.core.UriInfo;import zxn.ws.service.RestfulRegeditService;@Path(value = "/") public class RestfulRegeditServiceImpl implements RestfulRegeditService {@Contextprivate UriInfo uriInfo;@Contextprivate Request request;@POST@Path("/regedit")@Produces(MediaType.APPLICATION_JSON)public String regedit(String username, String password) throws IOException {return "";} }
3、改動spring配置文件applicationContext.xml(粗體部分為需加入的)

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/b<span style="background-color: rgb(255, 255, 0);">eans"
?? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc</span>e" xmlns:aop="http://www.springframework.org/schema/aop"
?? ?xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
??? xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
?? ?xsi:schemaLocation="http://www.springframework.org/schema/beans
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/aop
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/context
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/context/spring-context-3.0.xsd
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/tx
?? ??? ??? ??? ??? ??? ?http://www.springframework.org/schema/tx/spring-tx-3.0.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 ">
?? ?<import resource="classpath:META-INF/cxf/cxf.xml" />
?? ?<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

?? ?<!-- webservice配置 ,myeclipse檢測到此處有錯沒影響-->
?? ?<jaxws:endpoint id="regeditService" implementor="zxn.ws.service.RegeditServiceImpl" address="/Regedit" />

<bean id="restfulRegeditService" class="zxn.ws.service.impl.RestfulRegeditServiceImpl" />
<!--restful服務 -->
?? ?<jaxrs:server id="restServiceContainer" address="/regedit">
?? ??? ?<jaxrs:serviceBeans>
?? ??? ??? ?<ref bean="restfulRegeditService" />
?? ??? ?</jaxrs:serviceBeans>
?? ??? ?<jaxrs:extensionMappings>
?? ??? ??? ?<entry key="json" value="application/json" />
?? ??? ??? ?<entry key="xml" value="application/xml" />
?? ??? ?</jaxrs:extensionMappings>
?? ??? ?<jaxrs:languageMappings>
?? ??? ??? ?<entry key="en" value="en-gb" />
?? ??? ?</jaxrs:languageMappings>
?? ?</jaxrs:server></strong>
</beans>

4、部署到tomcat,執行。成功。界面下圖:

5、源碼地址下載:http://download.csdn.net/detail/zxnlmj/7458403

版權聲明:本文博主原創文章,博客,未經同意不得轉載。

轉載于:https://www.cnblogs.com/blfshiye/p/4942280.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的采用CXF+spring+restful创建一个web接口项目的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。