CXF webservice之手動啟動服務方法(restful )
首先介紹一下CXF加深一下對於CXF webservice的認識:
Apache CXF =Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,現在已經正式更名為 Apache CXF 了,以下簡稱為 CXF。CXF 繼承了 Celtix 和 XFire 兩大開源專案的精華,提供了對 JAX-WS 全面的支援,並且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支援,並且可以根據實際專案的需要,採用程式碼優先(Code First)或者 WSDL 優先(WSDL First)來輕鬆地實現
Web Services 的釋出和使用。Apache CXF已經是一個正式的Apache頂級專案。
Apache CXF 是一個開源的Services 框架,CXF 幫助您利用 Frontend 程式設計 API 來構建和開發 Services ,像 JAX-WS 。這些 Services 可以支援多種協議,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,並且可以在多種傳輸協議上執行,比如:HTTP、JMS 或者 JBI,CXF 大大簡化了 Services 的建立,同時它繼承了 XFire 傳統,一樣可以天然地和 Spring 進行無縫整合。
好吧我承認這麼官方的說法我是說不出來;網上找到比較準確的解釋;
手動啟動的方式不需要掛在tomcat伺服器,使用cxf提供的jetty伺服器。
廢話少說,直接進入主題:
第一步:環境配置:
eclipse jee mars.2
tomcat 7
jdk1.7
maven
CXF 3.#
環境變數配置問度娘(都是些基本的)
第二步:CXF官網下載CXF3.# (-src的原始碼包暫時沒有用到,但是那一個全部的一定要下載)
傻瓜式操作--->解壓之後直接將lib下的所有的jar包都放到eclipse工程下;
使用maven的話;
<dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.4</version> </dependency> <!-- Jetty is needed if you're using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.4</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.9.8</version> </dependency>
第三步:伺服器端實現:Service.class
@Path("/baoxian") @Produces({ MediaType.APPLICATION_JSON }) public class BaoXianService { private final static Log logger = LogFactory.getLog(BaoXianService.class); @GET @Path("/before/{vincode}") public String getinfo(@PathParam("vincode") String vincode, @QueryParam("name") String name) { System.out.println(vincode); String ret = "my name is :" + name; return ret; } @POST @Path("/post/query") @Consumes({ "application/json", "application/xml" }) public String Before_query(String bx) throws Exception { System.err.println(bx); return bx; } }
下面提供了兩種最基本的GET和POSt方法,目的是為了測試資料的傳遞;需要擴充套件的自行新增;
第四步:手動啟動服務類:
public class Server {
public static void main(String[] args) {
//String address = PropertyUtil.getProperties("ADDRESS");
//BaoXianService bxservice = new BaoXianService();
// Service instance
<span style="white-space:pre"> </span>String address = "http://localhost:8080/rest";//埠號和埠號後面的是可以自定義的;但是要與客戶端請求的地址對應;否則不會識別,無法訪問
JAXRSServerFactoryBean restServer = new JAXRSServerFactoryBean();
restServer.setResourceClasses(Detail.class);
restServer.getInInterceptors().add(new IpAddressInInterceptor());//自定義的攔截器
restServer.setServiceBean(bxservice);
restServer.setAddress(address);
restServer.create();
}
服務啟動成功:
第五步:客戶端測試:
static String baseAddress = "localhost:8080/rest";//與啟動類的地址相同
public static void main(String[] args) {
String data = "{\"name\":\"zhangsan\"}";
WebClient webclient = WebClient.create(baseAddress);
webclient.path("post/query")
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON);
String bx = webclient.post(data, String.class);
}
ok
心得:由於是第一次接觸webservice;對於裡面的邏輯、方法還不是很熟悉,但是萬事開頭難,邁出去第一步以後的路就會好走很多;現在稍微熟悉一點了,就會發現之前遇到的問題其實都不是問題,堅持堅持再堅持,不斷的總結分析,就會有收穫。
程式碼比較簡單,列了寫主要的思路,其實我覺得只要能進行資料的傳遞,剩下的就是按部就班了