JAX RS – 20 – JAXB & XML

Merhaba Arkadaslar
Bu bolumde JAXB & JAX-RS entegrasyonunu gerceklestirecegiz. JAXB (Java Architecture for XML Binding ) anlamina gelmektedir.

  • Marshalling – Java objelerini -> XML e donusturme islemi
  • Unmarshalling – XML icerigini Java objelererine donusturme islemi

Maven Dependency

Jersey Implementation icin herhangi bir dependency eklememiz gerekmiyor.
RESTEasy Implementation icin resteasy-jaxb-provider eklememiz gereklidir.
Eger eklemezsek su sekilde bir hata mesaji aliriz
Could not find MessageBodyWriter for response object of type: 

pom.xml

<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-jaxb-provider</artifactId>
	<version>${resteasy.version}</version>
</dependency>

Example 1

Person.java

package _14.jaxb.xml.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 = "person")
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

	@XmlElement
	private int id;
	@XmlElement
	private String name;
	@XmlElement
	private String surname;

	//getters and setters

}

Person sinifimiz uzerinde @XMLRootElement annotation tanimliyoruz.
XmlAccessorType icin XmlAccessType.FIELD degerini kullaniyoruz.
@XMLElement annotation’ini field’ler uzerinde tanimliyoruz. Default olarak getter metot uzerinde tanimlamamiz gereklidir.
Varsayilan olarak getter/setter cifti @XMLTransient olarak annotation yapilmadigi durumda bind edilecektir.

Metotlarda @Produces annotation’da MediaType.APPLICATION_XML kullaniyoruz.
Boylece response XML olarak uretilecektir.

PersonResource.java

package _14.jaxb.xml.service;

import java.io.StringWriter;
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 javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import _14.jaxb.xml.model.Person;

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

	@GET
	@Path("/person")
	@Produces(MediaType.APPLICATION_XML)
	public Person getPerson() {
		Person person = new Person();
		person.setId(1);
		person.setName("Levent");
		person.setSurname("Erguder");
		return person;

	}


	@GET
	@Path("/personWithJAXBContext")
	@Produces(MediaType.APPLICATION_XML)
	public String getPersonWithJAXBContext() throws JAXBException {

		Person person = new Person();
		person.setId(1);
		person.setName("Levent");
		person.setSurname("Erguder");

		JAXBContext ctx = JAXBContext.newInstance(Person.class);
		StringWriter writer = new StringWriter();
		ctx.createMarshaller().marshal(person, writer);

		return writer.toString();
	}

	@GET
	@Path("/persons")
	@Produces(MediaType.APPLICATION_XML)
        //@Wrapped(element="list", namespace="http://foo.org", prefix="foo") Only for RESTEasy
	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_XML)
	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;
	}
}

getPerson metodu geriye Person donmektedir

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

jaxb-xml

getPersonWithJAXBContext metodunda marshalling icin javax.xml.bind.JAXBContext sinifini kullaniyoruz.

http://localhost:8080/injavawetrust.resteasy.tutorial/person-jaxb-xml/personWithJAXBContext
http://localhost:8080/injavawetrust.jersey.tutorial/person-jaxb-xml/personWithJAXBContext

getPersons metodunu cagirmak icin ;

http://localhost:8080/injavawetrust.jersey.tutorial/person-jaxb-xml/persons
http://localhost:8080/injavawetrust.resteasy.tutorial/person-jaxb-xml/persons
You can change the namespace URI, namespace tag, and collection element name 
by using the @org.jboss.resteasy.annotations.providers.jaxb.Wrapped 
annotation on a parameter or method

getPersonsArray metodu geriye Person[] donmektedir.

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

Example 2

Bir baska ornek olarak Customer HAS-A Address olsun ;

Address.java

package _14.jaxb.xml.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 = "address")
@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

	@XmlElement
	private String city;

	@XmlElement
	private String zip;

	@XmlElement
	private String country;

	//getters and setters
	
}

Customer.java

package _14.jaxb.xml.model;

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

@XmlRootElement(name = "customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
	
	@XmlAttribute
	private int id;
	
	@XmlElement(name = "first-name")
	private String firstName;
	
	@XmlElement(name = "last-name")
	private String lastName;
	
	@XmlElement
	private Address address;

	//getters and setters

}

CustomerResource.java

package _14.jaxb.xml.service;

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

import _14.jaxb.xml.model.Address;
import _14.jaxb.xml.model.Customer;

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

	@GET
	@Path("/customer")
	@Produces(MediaType.APPLICATION_XML)
	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.resteasy.tutorial/customer-jaxb-xml/customer
http://localhost:8080/injavawetrust.jersey.tutorial/customer-jaxb-xml/customer

jaxb-xml-2

 

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 *