tomcat發佈基於jersey的WebService

週末繼續學習jersey,測試了幾個get和post方式的webservice請求,並模擬客戶端測試調用情況。在google中找到了這麼一篇相關的文章:

REST: CRUD with JAX-RS (Jersey) 。   結果做到一半出了問題,服務端總是獲取不到客戶端請求的數據流內容!調試了一個下午未果,卻在第二天如夢方醒般的找到了原因。教訓1:學習下http協議;教訓2:老外的東西不一定是對的。

 

週末的測試主要還是基於jersey的webservice服務。對get和post兩種方式發送請求的幾種情況都做了簡單的示例(上傳文件的情況未測試,待續)。

 

service端:

[java] view plaincopy
  1. @Path("/hello")  
  2. public class HelloService {  
  3.     @GET  
  4.     @Produces("text/plain")  
  5.     public String helloWorld(){  
  6.         return "hello world";  
  7.     }  
  8.     /* 
  9.      * post param  test 
  10.      */  
  11.     @POST     
  12.     @Path("echo")  
  13.     @Consumes("application/x-www-form-urlencoded")    
  14.     public String echo(@FormParam("msg") String msg){  
  15.         return "are you say "+msg;  
  16.     }  
  17.     /* 
  18.      * get param test 
  19.      */  
  20.     @GET  
  21.     @Path("sex")  
  22.     @Produces("text/plain")  
  23.     public String getSex(@PathParam("name") String name){  
  24.         return "male";  
  25.     }  
  26.       
  27.     /* 
  28.      * get {} request  
  29.      * http://houfeng:8080/jerseyWebServiceTest/services/hello/age/houfeng 
  30.      */  
  31.     @GET  
  32.     @Path("age/{name}")  
  33.     @Produces("text/plain")  
  34.     public String getAge(@PathParam("name") String name){  
  35.         return "18";  
  36.     }  
  37.       
  38.       
  39.     /* 
  40.      * get {} request 
  41.      * http://houfeng:8080/jerseyWebServiceTest/services/hello/223232323 
  42.      */  
  43.     @GET  
  44.     @Path ("{id}")  
  45.     @Produces ("application/xml")  
  46.     public StreamingOutput retrieveCustomer(@PathParam ("id") String customerId) {  
  47.         String customerDetails = "hou,feng,232";   
  48.         final String[] details = customerDetails.split(",");   
  49.         return new StreamingOutput() {    
  50.             public void write(OutputStream outputStream) {    
  51.                 PrintWriter out = new PrintWriter(outputStream);  
  52.                 out.println("<?xml version=/"1.0/" encoding=/"UTF-8/"?>");  
  53.                 out.println("<customer>");  
  54.                 out.println("<firstname>" + details[0] + "</firstname>");  
  55.                 out.println("<lastname>" + details[1] + "</lastname>");  
  56.                 out.println("<zipcode>" + details[2] + "</zipcode>");  
  57.                 out.println("</customer>");  
  58.                 out.close();  
  59.             }   
  60.         };   
  61.     }  
  62.       
  63.       
  64.     // get  vs  post   
  65.       
  66.     @GET  
  67.     @Path("test_get")  
  68.     @Produces("text/plain")  
  69.     public String getTest1(@PathParam("name") String name, @Context HttpServletRequest request){  
  70.         System.out.println("name:"+name);// null  
  71.         String result;  
  72.         result = request.getParameter("name");  
  73.         System.out.println("name="+result); //houfeng  
  74.         result+= "--------"+request.getContextPath();   
  75.         return result;  
  76.     }  
  77.       
  78.     /* 
  79.      * get 方式 正確的獲取參數方法 @QueryParam 或者 用 request; url裏有參數的用PathParam 
  80.      */  
  81.     @GET  
  82.     @Path("test_get2")  
  83.     @Produces("text/plain")  
  84.     public String getTest11(@QueryParam("name") String name, @Context HttpServletRequest request){  
  85.         System.out.println("name:"+name);// houfeng  
  86.         String result;  
  87.         result = request.getParameter("name");  
  88.         System.out.println("name="+result); //houfeng    
  89.         result+= "--------"+request.getContextPath();   
  90.         return result;  
  91.     }  
  92.    
  93.       
  94.     @POST  
  95.     @Path("test_post1")  
  96.     @Consumes("application/x-www-form-urlencoded")   
  97.     @Produces("text/plain")  
  98.     public String getTest2(@FormParam("name") String name){   
  99.         System.out.println(name);//houfeng  
  100.         String result=name;    
  101.         return result;  
  102.     }  
  103.       
  104.     @POST  
  105.     @Path("test_post2")  
  106.     @Consumes("application/x-www-form-urlencoded")   
  107.     @Produces("text/plain")  
  108.     public String getTest22(@QueryParam("name") String name){  
  109.         System.out.println("name:"+name);//houfeng,但是有警告。提示用FormParam  
  110.         String result = name;   
  111.         return result;  
  112.     }  
  113.       
  114.       
  115.     @POST  
  116.     @Path("test_post3")   
  117.     @Produces("text/plain")  
  118.     public String getTest2222(String entity, @Context HttpServletRequest request){  
  119.         System.out.println("entity:"+entity);//hello 傳入方式:resource.entity("hello").post(String.class);  
  120.         String result;   
  121.         result= "--------"+request.getContextPath();   
  122.         return result;  
  123.     }  
  124.       
  125.     @POST  
  126.     @Path("test_post4")  
  127.     //@Consumes("application/xml"),這樣就會出錯;@Consumes("application/x-www-form-urlencoded") 可以。  
  128.     @Produces("text/plain")  
  129.     public String getTest22222(InputStream is, @Context HttpServletRequest request) throws Exception{  
  130.         byte[] buf = new byte[is.available()];  
  131.         is.read(buf);  
  132.         System.out.println("buf:"+new String(buf));  
  133.         String result;   
  134.         result= "--------"+request.getContextPath();   
  135.         return result;  
  136.     }  

 

客戶端可以採用兩種方式測試。

1,採用jersey實現的測試api:jersey-twitter-client-1.0-SNAPSHOT-jar-with-dependencies.jar 

2,採用apache httpclient 模擬客戶端的各種請求。

上面提到的參考e文中是採用的第二種方式。在這裏我使用jersey測試api來實現。

[java] view plaincopy
  1. public  void testHelloService() throws URISyntaxException {  
  2.     Client client = Client.create();  
  3.     URI u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello");  
  4.     System.out.println(u);  
  5.     WebResource resource = client.resource(u);  
  6.     //get  
  7.     String result = resource.get(String.class);  
  8.     System.out.println(result);  
  9.       
  10.     //get param  
  11.     u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/sex");  
  12.     System.out.println(u);  
  13.     resource = client.resource(u);  
  14.     MultivaluedMapImpl params = new MultivaluedMapImpl();  
  15.     params.add("name""houfeng");  
  16.     result = resource.queryParams(params).get(String.class);  
  17.     System.out.println(result);  
  18.       
  19.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get");  
  20.     System.out.println(u);  
  21.     resource = client.resource(u);  
  22.     params = new MultivaluedMapImpl();  
  23.     params.add("name""houfeng");  
  24.     result = resource.queryParams(params).get(String.class);  
  25.     System.out.println(result);  
  26.       
  27.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get2");  
  28.     System.out.println(u);  
  29.     resource = client.resource(u);  
  30.     params = new MultivaluedMapImpl();  
  31.     params.add("name""houfeng");  
  32.     result = resource.queryParams(params).get(String.class);  
  33.     System.out.println(result);  
  34.   
  35.       
  36.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post1");  
  37.     System.out.println(u);  
  38.     resource = client.resource(u);  
  39.     params = new MultivaluedMapImpl();  
  40.     params.add("name""houfeng");  
  41.     result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class,params);  
  42.     System.out.println(result);  
  43.       
  44.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post2");  
  45.     System.out.println(u);  
  46.     resource = client.resource(u);  
  47.     params = new MultivaluedMapImpl();  
  48.     params.add("name""houfeng");  
  49.     result = resource.queryParams(params).type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class);  
  50.     System.out.println(result);  
  51.       
  52.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post3");  
  53.     System.out.println(u);  
  54.     resource = client.resource(u);   
  55.     result = resource.entity("hello").post(String.class);  
  56.     System.out.println(result);  
  57.       
  58.     u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post4");  
  59.     System.out.println(u);  
  60.     resource = client.resource(u);   
  61.     String buf = "inputstream content.";  
  62.     ByteArrayInputStream bais = new ByteArrayInputStream(buf.getBytes());  
  63.     result = resource.entity(bais).post(String.class);  
  64.     System.out.println(result);  
  65. }  

 

過程中遇到的問題就是提交流的時候,錯誤的參考了e文中 “@Consumes ( "application/xml" ) ”的請求類型! 結果導致service 端 接受請求的方法參數InputStream 得不到內容。換作@Context HttpServeltRequest request 參數也無濟於事。於是在網上搜索,在一個國外論壇中有人提到相似的問題“上傳文件得不到流裏的內容,但是jetty裏可以,tomcat裏不可以。?”。好像沒有太大參考,但我也試了下,還是失敗。。。

今天修改提交類型註解爲:@Consumes("application/x-www-form-urlencoded") ,測試通過!終於才恍然大悟:application/xml是客戶端接受的內容類型。哎,是應該學習下http協議的相關知識,這樣的問題耽誤了大半天的時間!

    另外,對於jax-ws中幾個註解,簡單總結下:
       QueryParam--url ? 後面表示的參數  .  get post 通用.
       PathParam---url中的一部分,例如用{}表示的url中的一部分。get post 通用。
       FormParam---post提交的form表單參數。     用於 post     

  ( 其他幾個param稍後再學習)。

發佈了16 篇原創文章 · 獲贊 14 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章