JAX-RS之jackson去处理json
来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/05/21 10:44
在JAX-RS中使用jackson去处理json,例子如下。下文讲解了,如何将一个对象转变为JSON对象,使用的是jackson。
1 放置resteasy-jackson-provider.jar
2
一个简单对象
3 使用注解@Produces("application/json").就可以转换JSON了
注意,要把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,如下:
运行后,输出:
Output from Server ....
{"qty":999,"name":"iPad 3"}
5 调用服务端的POST,将JSON传入,看其如何转化位product
输出为:
Output from Server ....
Product created : Product [name=iPad 4, qty=100]
1 放置resteasy-jackson-provider.jar
2
一个简单对象
- public class Product {
- String name;
- int qty;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getQty() {
- return qty;
- }
- public void setQty(int qty) {
- this.qty = qty;
- }
- }
public class Product { String name;int qty; public String getName() {return name;} public void setName(String name) {this.name = name;} public int getQty() {return qty;} public void setQty(int qty) {this.qty = qty;} }
3 使用注解@Produces("application/json").就可以转换JSON了
- import javax.ws.rs.Consumes;
- import javax.ws.rs.GET;
- import javax.ws.rs.POST;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.Response;
- @Path("/json/product")
- public class JSONService {
- @GET
- @Path("/get")
- @Produces("application/json")
- public Product getProductInJSON() {
- Product product = new Product();
- product.setName("iPad 3");
- product.setQty(999);
- return product;
- }
- @POST
- @Path("/post")
- @Consumes("application/json")
- public Response createProductInJSON(Product product) {
- String result = "Product created : " + product;
- return Response.status(201).entity(result).build();
- }
- }
import javax.ws.rs.Consumes;import javax.ws.rs.GET;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.Response; @Path("/json/product")public class JSONService { @GET@Path("/get")@Produces("application/json")public Product getProductInJSON() { Product product = new Product();product.setName("iPad 3");product.setQty(999); return product; } @POST@Path("/post")@Consumes("application/json")public Response createProductInJSON(Product product) { String result = "Product created : " + product;return Response.status(201).entity(result).build(); } }
注意,要把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,如下:
- try {
- URL url = new URL(
- "http://localhost:8085/Resetjason/json/product/get");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setRequestProperty("Accept", "application/json");
- if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
- }
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (conn.getInputStream())));
- String output;
- System.out.println("Output from Server .... \n");
- while ((output = br.readLine()) != null) {
- System.out.println(output);
- }
- conn.disconnect();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
try {URL url = new URL("http://localhost:8085/Resetjason/json/product/get");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setRequestProperty("Accept", "application/json");if (conn.getResponseCode() != 200) {throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());}BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));String output;System.out.println("Output from Server .... \n");while ((output = br.readLine()) != null) {System.out.println(output);}conn.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
运行后,输出:
Output from Server ....
{"qty":999,"name":"iPad 3"}
5 调用服务端的POST,将JSON传入,看其如何转化位product
- try {
- URL url = new URL(
- "http://localhost:8085/Resetjason/json/product/post");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/json");
- String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
- OutputStream os = conn.getOutputStream();
- os.write(input.getBytes());
- os.flush();
- if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
- throw new RuntimeException("Failed : HTTP error code : "
- + conn.getResponseCode());
- }
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (conn.getInputStream())));
- String output;
- System.out.println("Output from Server .... \n");
- while ((output = br.readLine()) != null) {
- System.out.println(output);
- }
- conn.disconnect();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
try {URL url = new URL("http://localhost:8085/Resetjason/json/product/post");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setDoOutput(true);conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/json");String input = "{\"qty\":100,\"name\":\"iPad 4\"}";OutputStream os = conn.getOutputStream();os.write(input.getBytes());os.flush();if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode());}BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));String output;System.out.println("Output from Server .... \n");while ((output = br.readLine()) != null) {System.out.println(output);}conn.disconnect();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}
输出为:
Output from Server ....
Product created : Product [name=iPad 4, qty=100]
- JAX-RS之jackson去处理json
- JAX-RS之上传文件
- JAX-RS之Jersey入门
- 集成JAX-RS和JSON-P
- JAX-RS 服务接受json转换对象
- json处理三部曲之第二曲:利用Jackson处理json
- JAX-RS 2.0 REST 请求处理
- 使用jackson去解析json
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- SpringMVC json处理 (Jackson)
- JAX-RS之上传文件
- Davinci内核编译常用命令
- 字符串函数源代码分析与比较
- 【转】用JS实现弹出模式对话框,实现页面传值
- JAX-RS之与XML打交道
- JAX-RS之jackson去处理json
- JAX-RS之resteasy跟spring整合
- 找出文件中每小时最大利用率值
- 合并排序
- 几个重要的TCP/IP选项解析(Java Socket)
- poj 2352 Star
- 用于ARM上的FFT与IFFT源代码(C语言,不依赖特定平台)
- 浪潮实习8.6
- tslib1.0移植