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“.

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

		<id>JBoss repository</id>

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;


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;
public class JSONService {
	public Product getProductInJSON() {
		Product product = new Product();
		product.setName("iPad 3");
		return product; 
	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> -->

4. Demo

See GET and POST method.

1. GET method
When URI pattern “/json/product/get” is requested, following JSON file will be returned.

	"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


  1. Jackson Official Website
  2. JSON Support via Jackson
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
钱包余额 0