On today’s demo im trying to use RESTEasy to handle multiple REST requests. According to Wikipedia, REST (Representational state transfer) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. While RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS specification. JAX-RS is a new JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol.
On this project, im trying to create 2 different services. 1 service to get all objects, while another one for fetching a single objects. I create 2 different responses type for each service, JSON and XML. Okay, so lets try with 2 simple javabeans.
package com.edw.bean;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Student {
private String name;
private String address;
private int age;
@XmlElement(name="book")
@XmlElementWrapper(name = "books")
private List<Book> books;
public Student() {
}
public Student(String name, String address, int age, List<Book> books) {
this.name = name;
this.address = address;
this.age = age;
this.books = books;
}
// other setter getter
@Override
public String toString() {
return "Student{" + "name=" + name + ", address=" + address + ", age=" + age + ", books=" + books + '}';
}
}
package com.edw.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
private String title;
private double price;
public Book() {
}
public Book(String title, double price) {
this.title = title;
this.price = price;
}
// other setter getter
@Override
public String toString() {
return "Book{" + "title=" + title + ", price=" + price + '}';
}
}
Now, you need to create a java class to handle all REST services,
package com.edw.rest;
import com.edw.bean.Book;
import com.edw.bean.Student;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/service/student")
public class StudentRest {
@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
// path would be http://localhost:8080/YourProject/service/student/json
public List<Student> getStudentsJSON() {
return getStudents();
}
@GET
@Path("/xml")
@Produces(MediaType.APPLICATION_XML)
// path would be http://localhost:8080/YourProject/service/student/xml
public List<Student> getStudentsXML() {
return getStudents();
}
@GET
@Path("/json/{name}")
@Produces(MediaType.APPLICATION_JSON)
// path would be http://localhost:8080/YourProject/service/student/json/insertyournamehere
public Student getStudentJSON(@PathParam("name") String name) {
return new Student(name, name + " Address", 10, getBooks());
}
@GET
@Path("/xml/{name}")
@Produces(MediaType.APPLICATION_XML)
// path would be http://localhost:8080/YourProject/service/student/xml/insertyournamehere
public Student getStudentXML(@PathParam("name") String name) {
return new Student(name, name + " Address", 10, getBooks());
}
private List<Book> getBooks() {
List<Book> books = new ArrayList<Book>();
books.add(new Book("Harry Potter", 100d));
books.add(new Book("Lord of the Ring", 120d));
books.add(new Book("Chicken Soup", 20d));
books.add(new Book("Doraemon", 190d));
books.add(new Book("Laskar Pelangi", 200d));
return books;
}
private List<Student> getStudents() {
List<Student> students = new ArrayList<Student>();
students.add(new Student("Edwin", "Jakarta", 25, getBooks()));
students.add(new Student("Kamplenk", "Ciledug", 29, getBooks()));
students.add(new Student("Tebek", "Pamulang", 38, getBooks()));
students.add(new Student("Syamsu", "Bandung", 60, getBooks()));
return students;
}
}
And dont forget to register your service class
package com.edw.config;
import com.edw.rest.StudentRest;
import java.util.Set;
import java.util.HashSet;
import javax.ws.rs.core.Application;
public class ApplicationConfig extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public ApplicationConfig() {
singletons.add(new StudentRest());
}
@Override
public Set<Class<?>> getClasses() {
return empty;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
And last, register all in your web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.edw.config.ApplicationConfig</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Here are some screenshots,
Have fun with RESTEasy





Hi…
I am working on the same kind of example with jboss setup. I am getting the “Invalid Json namespace” exception..
Can you please help me in this.
Thanks,
Patil
Hi Patil,
try using this,
@GET
@Produces("application/json")
@Path("/books")
@Mapped(namespaceMap = {
@XmlNsMap(namespace = "http://yourlink.com/books", jsonName = "books")
})
Hi Edwin,
Still I am getting the same error. I have added below…
@XmlRootElement(namespace = “student”) to the Student class and @XmlRootElement(namespace = “book”) Book class.
in my Rest class,
@Mapped(namespaceMap = {
@XmlNsMap(namespace = “student”, jsonName = “data”),
@XmlNsMap(namespace = “book”, jsonName = “record”) })
I am getting following error-
org.jboss.resteasy.spi.WriterException: java.lang.IllegalStateException: Invalid JSON namespace: http://www.w3.org/2001/XMLSchema-instance.
Can you please help me to resolve this..
Thanks,
Patil
Hi Patil, could you please share your java bean?
Somehow i think it’s error due to miss-configuration of unmarshalling from java bean to json.