JAX-RS之jackson去处理json

来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/05/21 10:44
在JAX-RS中使用jackson去处理json,例子如下。下文讲解了,如何将一个对象转变为JSON对象,使用的是jackson。

1 放置resteasy-jackson-provider.jar

2
一个简单对象
Java代码 复制代码
  1.   
  2. public class Product {   
  3.     
  4.     String name;   
  5.     int qty;   
  6.     
  7.     public String getName() {   
  8.         return name;   
  9.     }   
  10.     
  11.     public void setName(String name) {   
  12.         this.name = name;   
  13.     }   
  14.     
  15.     public int getQty() {   
  16.         return qty;   
  17.     }   
  18.     
  19.     public void setQty(int qty) {   
  20.         this.qty = qty;   
  21.     }   
  22.     
  23. }  


3 使用注解@Produces("application/json").就可以转换JSON了
Java代码 复制代码
  1. import javax.ws.rs.Consumes;   
  2. import javax.ws.rs.GET;   
  3. import javax.ws.rs.POST;   
  4. import javax.ws.rs.Path;   
  5. import javax.ws.rs.Produces;   
  6. import javax.ws.rs.core.Response;   
  7.     
  8. @Path("/json/product")   
  9. public class JSONService {   
  10.     
  11.     @GET  
  12.     @Path("/get")   
  13.     @Produces("application/json")   
  14.     public Product getProductInJSON() {   
  15.     
  16.         Product product = new Product();   
  17.         product.setName("iPad 3");   
  18.         product.setQty(999);   
  19.     
  20.         return product;    
  21.     
  22.     }   
  23.     
  24.     @POST  
  25.     @Path("/post")   
  26.     @Consumes("application/json")   
  27.     public Response createProductInJSON(Product product) {   
  28.     
  29.         String result = "Product created : " + product;   
  30.         return Response.status(201).entity(result).build();   
  31.     
  32.     }   
  33.     
  34. }  

  注意,要把web.xml中的自动扫描注释掉,否则会出错:
<!-- disabled auto scan
        <context-param>
             <param-name>resteasy.scan</param-name>
             <param-value>true</param-value>
</context-param> -->

<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.JSONService</param-value>
</context-param>

4 客户端调用服务端的GET,用于将服务端的对象转为JSON,如下:
 
Java代码 复制代码
  1. try {   
  2.   
  3.             URL url = new URL(   
  4.                     "http://localhost:8085/Resetjason/json/product/get");   
  5.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
  6.             conn.setRequestMethod("GET");   
  7.             conn.setRequestProperty("Accept""application/json");   
  8.   
  9.             if (conn.getResponseCode() != 200) {   
  10.                 throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());   
  11.             }   
  12.   
  13.             BufferedReader br = new BufferedReader(new InputStreamReader(   
  14.                     (conn.getInputStream())));   
  15.   
  16.             String output;   
  17.             System.out.println("Output from Server .... \n");   
  18.             while ((output = br.readLine()) != null) {   
  19.   
  20.                 System.out.println(output);   
  21.             }   
  22.                
  23.             conn.disconnect();   
  24.   
  25.         } catch (MalformedURLException e) {   
  26.   
  27.             e.printStackTrace();   
  28.         } catch (IOException e) {   
  29.   
  30.             e.printStackTrace();   
  31.                
  32.         }  


运行后,输出:
Output from Server ....

{"qty":999,"name":"iPad 3"}

5 调用服务端的POST,将JSON传入,看其如何转化位product
 
Java代码 复制代码
  1. try {   
  2.   
  3.         URL url = new URL(   
  4.                 "http://localhost:8085/Resetjason/json/product/post");   
  5.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
  6.         conn.setDoOutput(true);   
  7.         conn.setRequestMethod("POST");   
  8.         conn.setRequestProperty("Content-Type""application/json");   
  9.   
  10.         String input = "{\"qty\":100,\"name\":\"iPad 4\"}";   
  11.   
  12.         OutputStream os = conn.getOutputStream();   
  13.         os.write(input.getBytes());   
  14.         os.flush();   
  15.   
  16.         if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {   
  17.             throw new RuntimeException("Failed : HTTP error code : "  
  18.                     + conn.getResponseCode());   
  19.         }   
  20.   
  21.         BufferedReader br = new BufferedReader(new InputStreamReader(   
  22.                 (conn.getInputStream())));   
  23.   
  24.         String output;   
  25.         System.out.println("Output from Server .... \n");   
  26.         while ((output = br.readLine()) != null) {   
  27.   
  28.             System.out.println(output);   
  29.         }   
  30.   
  31.         conn.disconnect();   
  32.   
  33.     } catch (MalformedURLException e) {   
  34.   
  35.         e.printStackTrace();   
  36.     } catch (IOException e) {   
  37.   
  38.         e.printStackTrace();   
  39.   
  40.     }   
  41.   
  42. }  


输出为:
  Output from Server ....

Product created : Product [name=iPad 4, qty=100] 
原创粉丝点击