1. 程式人生 > >jersey client 傳送Post請求 【帶引數】

jersey client 傳送Post請求 【帶引數】

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

[java] view plain copy print ?
  1. @Path("postUser"
    )  
  2. public class PostUser {  
  3.       
  4.     @POST  
  5.     @Produces(MediaType.TEXT_XML)  
  6.     public String postUser(){  
  7.           
  8.         return "濤哥";  
  9.     }  
  10.     @Path("bean")  
  11.     @POST  
  12.     @Consumes
    (MediaType.APPLICATION_FORM_URLENCODED)  
  13.     @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})  
  14.     public User postUser(@FormParam("username") String username){  
  15.         User user = new User();  
  16.         System.out.println(username);  
  17.         user.setUsername(username);  
  18.         user.setPassword("濤哥");  
  19.         return user;  
  20.     }  
  21.       
  22. }  
@Path("postUser")public class PostUser {  @POST @Produces(MediaType.TEXT_XML) public String postUser(){    return "濤哥"; } @Path("bean") @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}) public User postUser(@FormParam("username") String username){  User user = new User();  System.out.println(username);  user.setUsername(username);  user.setPassword("濤哥");  return user; } }

 

[java] view plain copy print ?
  1. public class PostUser {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      * @throws UnsupportedEncodingException  
  6.      */  
  7.     public static void main(String[] args) throws UnsupportedEncodingException {  
  8.           
  9.         ClientConfig config = new DefaultClientConfig();  
  10.         Client client = Client.create(config);  
  11.         WebResource service = client.resource(getBaseURI());  
  12.   
  13.           
  14.         MultivaluedMap<String, String> param = new MultivaluedMapImpl();  
  15.         param.add("username""ssss");  
  16.         System.out.println(service.path("services").path("postUser")  
  17.                 .path("bean").queryParams(param)  
  18.                 .type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class));  
  19.           
  20.           
  21.     }  
  22.   
  23.     private static URI getBaseURI() {  
  24.         return UriBuilder.fromUri(  
  25.                 "http://localhost:8080/jersey/").build();  
  26.     }  
public class PostUser { /**  * @param args  * @throws UnsupportedEncodingException   */ public static void main(String[] args) throws UnsupportedEncodingException {    ClientConfig config = new DefaultClientConfig();  Client client = Client.create(config);  WebResource service = client.resource(getBaseURI());    MultivaluedMap<String, String> param = new MultivaluedMapImpl();  param.add("username", "ssss");  System.out.println(service.path("services").path("postUser")    .path("bean").queryParams(param)    .type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class));     } private static URI getBaseURI() {  return UriBuilder.fromUri(    "http://localhost:8080/jersey/").build(); }

 

 

//  注意 .type(MediaType.APPLICATION_FORM_URLENCODED).

 

type  而不是 accept  否則將出錯的

 

 

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述