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
Download it –  JAX-RS-Download-JSON-Jackson-Example.zip (8 KB)
References
  1. Jackson Official Website
  2. JSON Support via Jackson
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值