1. 程式人生 > >Jersey使用總結一(jersey初步使用和簡單post請求幾種引數傳遞,String方式,不含物件操作)

Jersey使用總結一(jersey初步使用和簡單post請求幾種引數傳遞,String方式,不含物件操作)

首先說明一下,jersey是幹什麼的呢,我個人的理解就是類似於一種webservice技術或框架

jersey-rest為service端,其它的jersey請求為客戶端,客戶端傳送一個請求(帶參或不帶參)呼叫service端的方法,方法返回結果給客戶端。

如以下使用步奏:

一.建立伺服器端:

       1.下載jersey的相關jar

       2.建立一個web project,並匯入相關jar包

            2.1 配置xml配置檔案:使用jersey 相關jar包中提供的servlet,預設請求為/rest/*的都通過這裡

  <?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">
 
 <servlet>
         <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
                 <param-name>com.sun.jersey.config.property.packages</param-name>
                  <param-value>com.rest.jersey.first</param-value>
          </init-param>
         <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
       <servlet-name>Jersey REST Service</servlet-name>
         <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

       2.2 編寫配置檔案中配置的servlet請求對應的操作類或訪問方法

package com.rest.jersey.first;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.client.ClientResponse;

/**
 * 設定客戶端請求訪問的路徑為:“hello”
 * 實際上類上的面這個path(hello)是針對整個類生效的
 * 在類中的每個方法中可以繼續設定進一步的path
 * @author xianbo
 *
 */
@Path("/hello")
public class HelloResource {
	/**
	 * 
	 *客戶端請求方式為:
	 *service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN).get(String.class)
	 * @return
	 */
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String sayPlainTextHello() {
		return "Hello Jersey";
	}
    
	/**
	 * 客戶端請求方式為:
	 * service.path("rest").path("hello").accept(MediaType.TEXT_XML).get(String.class)
	 * @return
	 */
	@GET
	@Produces(MediaType.TEXT_XML)
	public String sayXMLHello() {
		return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
	}

	/**
	 * 客戶端請求方式為
	 * service.path("rest").path("hello").accept(MediaType.TEXT_HTML).get(String.class)
	 * @return
	 */
	@GET
	@Produces(MediaType.TEXT_HTML)
	public String sayHtmlHello() {
		return "<html> " + "<title>" + "Hello Jersey" + "</title>"
				+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
	}
	
	/**
	 * 客戶端傳送post請求
	 * service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN_TYPE).post(String.class)
	 * service.path("rest").path("hello").post(String.class,"9999999")
	 */
	@POST
	public String sayStringHellp(String str){
		System.out.println(str);
		return "success";
	}
	
	/**
	 * 也可以在“hello” 的路徑下繼續新增訪問路徑
	 * 此時的訪問路徑就是/hello/first
	 * service.path("rest").path("hello").path("first").post(String.class,"9999999")
	 * @return
	 */
	@Path("/first")
	@POST
	public String sayPathHelp(String str){
		System.out.println("first:"+str);
		return "success";
	}
	
	/**
	 * 也可以以傳遞的引數作為訪問路徑,起到動態路徑設定
	 * 此時的訪問路徑就是"/hello/變數str"
	 * "@PathParam("str")String str"說明這個“String str”引數的來源是包含在請求路徑中的請求引數
	 * service.path("rest").path("hello").path(param).post(String.class)
	 * @return
	 */
	@Path("/{str}")
	@POST
	public String sayParamHelp(@PathParam("str")String str){
		System.out.println("first/"+str+":"+str);
		return "success";
	}
	
	/**
	 * 也可以以傳遞的引數作為訪問路徑,起到動態路徑設定
	 * 此時的訪問路徑就是"/hello/變數str"
	 * "@PathParam("str")String str"說明這個“String str”引數的來源是包含在請求路徑中的請求引數
	 * service.path("rest").path("hello").path("123").path("456").post(String.class)
	 * @return
	 */
	@Path("/{str}/{str2}")
	@POST
	public String sayManyParamHelp(@PathParam("str")String str,@PathParam("str2")String str2){
		System.out.println("first/"+str+":"+str+":str2"+str2);
		return "success";
	}	
}

到此,service工程可以結束了,只等編寫客戶端進行對應的請求進行訪問了:

2.3 編寫客戶端請求類進行測試:

package com.rest.jersey.first;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;


/**
 * @Author:xianbo
 */

public class HelloClient {
	public static void main(String[] args) {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());
		// Fluent interfaces
		System.out.println("1:"+service.path("rest").path("hello").accept(
				MediaType.TEXT_PLAIN).get(ClientResponse.class).toString());
		// Get plain text
		System.out.println("2:"+service.path("rest").path("hello").accept(
				MediaType.TEXT_PLAIN).get(String.class));
		// Get XML
		System.out.println("3:"+service.path("rest").path("hello").accept(
				MediaType.TEXT_XML).get(String.class));
		// The HTML
		System.out.println("4:"+service.path("rest").path("hello").accept(
				MediaType.TEXT_HTML).get(String.class));
		
		
		System.out.println("5:"+service.path("rest").path("hello").accept(MediaType.TEXT_PLAIN_TYPE).post(String.class));
		System.out.println("6:"+service.path("rest").path("hello").post(String.class,"9999999"));
		System.out.println("7:"+service.path("rest").path("hello").path("first").post(String.class,"9999999"));
		

		String param = "test";
		//傳遞單個引數param變數作為動態路徑並且作為引數
		System.out.println("8:"+service.path("rest").path("hello").path(param).post(String.class));
		
		//多個引數包含在路徑中傳遞到方法中作為引數使用	,123,456作為引數	
		System.out.println("9:"+service.path("rest").path("hello").path("123").path("456").post(String.class));
		
		
		
		
	}

	private static URI getBaseURI() {
		return UriBuilder.fromUri(
				"http://192.168.13.84:8088/TestJerseyRest/").build();
	}
}




另外:貌似從spring 3才開始支援整合這個東西,所以很多的(屬性注入之類)new 物件的在spring 2.X必須自己手動獲取

如果要和spring 整合使用,那麼上面的web.xml中的servlet就必須使用和spring相對應的servlet了,至於是哪個,直接到jar包中找就明白了