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"
}
jackson resteasy demo

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

References

  1. Jackson Official Website
  2. JSON Support via Jackson
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值