JAX RS – 22 – JSON & Jettison

Merhaba Arkadaslar
Bu bolumde Jettison JSON kutuphanesini kullanacagiz.

Jettison 2 mapping format destekler ;

  • Jettison Mapped Convetion (default)
  • BadgerFish
JSON is a much simpler format than XML. 
While XML has elements, attributes, and namespaces, JSON only has name/value pairs

RESTEasy , JAXB siniflarini JSON formatina marshalling yapmamiza izin verir. Bunun icin Jettison JSON kutuphanesini kullanabiliriz.

RESTEasy allows you to marshall JAXB annotated POJOs to and from JSON.

Maven Dependency

RESTEasy
Jackson Provider’i disable edip Jettison Provider’i ekleyeyelim.

 
<!-- 
<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-jackson-provider</artifactId>
	<version>${resteasy.version}</version>
</dependency>
  -->
  
<!-- RESTEasy + Jettison -->
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jettison-provider -->		
<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-jettison-provider</artifactId>
	<version>${resteasy.version}</version>
</dependency>		

Product.java

package _16.jettison.json.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

	@XmlElement
	private int id;

	@XmlElement
	private String name;

	@XmlElement
	private String brand;

	@XmlElement
	private double price;

	@XmlElement
	private String category;

	//getters and setters

}

ProductResource.java

package _16.jettison.json.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import _16.jettison.json.model.Product;

@Path("/product-jettison-json")
public class ProductResource {

	@GET
	@Path("/product")
	@Produces(MediaType.APPLICATION_JSON)
	public Product getProduct() {
		Product product = new Product();
		product.setId(1);
		product.setName("K-360");
		product.setBrand("Logitech");
		product.setPrice(99.99);
		product.setCategory("Keyboard");
		return product;
	}
}

 http://localhost:8080/injavawetrust.resteasy.tutorial/product-jettison-json/product

jettison-resteasy

....
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
...
@Path("/product-jettison-json")
public class ProductResource {
	@GET
	@Path("/product")
	@Produces(MediaType.APPLICATION_JSON)
	@BadgerFish
	// http://localhost:8080/injavawetrust.resteasy.tutorial/product-jettison-json/product
	public Product getProduct() {
        ...
        }
}

badgerfish-resteasy

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *