JSON Example With RESTEasy + Jackson

发布时间:2017-3-27 10:57:05 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"JSON Example With RESTEasy + Jackson",主要涉及到JSON Example With RESTEasy + Jackson方面的内容,对于JSON Example With RESTEasy + Jackson感兴趣的同学可以参考一下。

Many like Jackson JSON processor, and it supported in RESTEasy. In this tutorial, we show you how to convert an object to JSON format and return it back to the client. 1. RESTEasy + Jackson To integrate Jackson with RESTEasy, you just need to include “resteasy-jackson-provider.jar“. Note When RESTEasy returned a json output, it will use Jackson provider to convert it automatically. You do not need to code a single line to integrate both. File : pom.xml <repositories> <repository> <id>JBoss repository</id> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>2.2.1.GA</version> </dependency> </dependencies> 2. Simple Object A simple object, later convert it into JSON format. package com.mkyong.rest; 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. JAX-RS Annotate the method with @Produces("application/json"). RESTEasy will use Jackson provider to handle the JSON conversion automatically. 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(); } } Disabled RESTEasy auto scanning.You must disabled the RESTEasy auto scanning, and register your REST service manually, otherwise, you will hits this error. Hope it get fix in future release. File : 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. Demo See GET and POST method. 1. GET method When URI pattern “/json/product/get” is requested, following JSON file will be returned. { "qty":999, "name":"iPad 3" } 2. POST method You can “post” the json format string to URI pattern “/json/product/post“, it will convert into “Product” automatically. Download Source Code Download it – JAX-RS-Download-JSON-Jackson-Example.zip (8 KB) References Jackson Official WebsiteJSON Support via Jackson

下一篇:jitsi 源代码编译及windows 安装包打包 之二