JAX RS – 21 – JSON & Jackson

Merhaba Arkadaslar
Bu bolumde JAX-RS , JSON implementation’ini gerceklestirecegiz.
JSON , JavaScript Object Notation anlamina gelmektedir.
Bir onceki bolumde kullandigimiz siniflarla benzer sekilde uygulamamizi gelistirecegiz , burada XML yerine JSON formatini kullanacagiz.

JSON is a much simpler format than XML. Objects are enclosed in curly brackets, “{}”, 
and contain key/value pairs. Values can be quoted strings, Booleans (true or false), 
numeric values, or arrays of these simple types
{
"id" : 42,
"name" : "Bill Burke", 
"married" : true,
"kids" : [ "Molly", "Abby" ]
}

Maven Dependency

Jersey

<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-json -->
<dependency>
	<groupId>org.glassfish.jersey.media</groupId>
	<artifactId>jersey-media-json-jackson</artifactId>
	<version>${jersey.version}</version>
</dependency>

RESTEasy

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

Example 1

Person.java

package _15.json.model;

public class Person {

	private int id;
	private String name;
	private String surname;

	//getters and setters

}

PersonResource.java

Burada @Produces annotation da MediaType.APPLICATION_JSON kullaniyoruz.
Boylece donus olarak uretilen cevap/response JSON tipinde olacaktir.

package _15.jackson.json.service;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import _15.jackson.json.model.Person;

@Path("/person-json")
public class PersonResource {

	@GET
	@Path("/person")
	@Produces(MediaType.APPLICATION_JSON)

	public Person getPerson() {
		Person person = new Person();
		person.setId(1);
		person.setName("Levent");
		person.setSurname("Erguder");
		return person;

	}

	@GET
	@Path("/persons")
	@Produces(MediaType.APPLICATION_JSON)

	public List<Person> getPersons() {
		Person person1 = new Person();
		person1.setId(1);
		person1.setName("Levent");
		person1.setSurname("Erguder");

		Person person2 = new Person();
		person2.setId(2);
		person2.setName("Joshua");
		person2.setSurname("Bloch");

		Person person3 = new Person();
		person3.setId(3);
		person3.setName("James");
		person3.setSurname("Gosling");

		List<Person> persons = new ArrayList<Person>();
		persons.add(person1);
		persons.add(person2);
		persons.add(person3);

		return persons;
	}
	
	@GET
	@Path("/personsArray")
	@Produces(MediaType.APPLICATION_JSON)

	public Person[] getPersonsArray() {
		Person person1 = new Person();
		person1.setId(1);
		person1.setName("Levent");
		person1.setSurname("Erguder");

		Person person2 = new Person();
		person2.setId(2);
		person2.setName("Joshua");
		person2.setSurname("Bloch");

		Person person3 = new Person();
		person3.setId(3);
		person3.setName("James");
		person3.setSurname("Gosling");

		Person[] persons = new Person[] { person1, person2, person3 };

		return persons;
	}
}

http://localhost:8080/injavawetrust.resteasy.tutorial/person-json/person
http://localhost:8080/injavawetrust.jersey.tutorial/person-json/person

json-jaxrs

http://localhost:8080/injavawetrust.resteasy.tutorial/person-json/persons
http://localhost:8080/injavawetrust.jersey.tutorial/person-json/persons


jax-rs-json-2

http://localhost:8080/injavawetrust.resteasy.tutorial/person-json/personsArray
http://localhost:8080/injavawetrust.jersey.tutorial/person-json/personsArray

Example 2

Address.java

package _15.json.model;

public class Address {

	private String city;
	private String zip;	
	private String country;

	//getters and setters
}

Customer.java

package _15.json.model;

public class Customer {

	private int id;
	private String firstName;
	private String lastName;
	private Address address;

	//getters and setters
}

CustomerResource.java

package _15.json.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import _15.json.model.Address;
import _15.json.model.Customer;

@Path("/customer-json")
public class CustomerResource {

	@GET
	@Path("/customer")
	@Produces(MediaType.APPLICATION_JSON)
	public Customer getCustomer() {
		Customer customer = new Customer();
		customer.setId(1);
		customer.setFirstName("levent");
		customer.setLastName("erguder");

		Address address = new Address();
		address.setCity("Istanbul");
		address.setCountry("Turkey");
		address.setZip("34000");
		customer.setAddress(address);

		return customer;
	}
}

http://localhost:8080/injavawetrust.jersey.tutorial/customer-json/customer
http://localhost:8080/injavawetrust.resteasy.tutorial/customer-json/customer

jax-rs-json

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 *