1 放置resteasy-jackson-provIDer.jar
2
一个简单对象 Java代码 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了
Java代码 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,如下:
Java代码 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
Java代码 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之jackson去处理json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)