WS Restful with JAXB


Chào các bạn hôm nay mình xin giới thiệu các bạn tạo webservice với RESTful và sử dụng JaxB

Cấu trúc của thư mục chúng ta như sau :

d1

Chúng ta tạo class Todo như  sau


package thaihoanghai.jersey;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Todo {
 private String description;
 private String sumary;
 public String getDescription() {
 return description;
 }
 public void setDescription(String description) {
 this.description = description;
 }
 public String getSumary() {
 return sumary;
 }
 public void setSumary(String sumary) {
 this.sumary = sumary;
 }
}

Tiếp đến chúng ta tạo Resource với class TodoResource


package thaihoanghai.jersey;

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

@Path("/todo")
public class TodoResource {
 @GET
 @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
 public Todo getXML(){
 Todo todo = new Todo();
 todo.setSumary("This sumary...");
 todo.setDescription("This is description...");
 return todo;
 }
 @GET
 @Produces({MediaType.TEXT_XML})
 public Todo getHTML(){
 Todo todo = new Todo();
 todo.setSumary("This sumary...");
 todo.setDescription("This sumary...");
 return todo;
 }
}

Va thiết lập servlet trong file web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>WS-RESTful-JAXB</display-name>
 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
 <servlet-name>Jersey REST Service</servlet-name>
 <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
 <init-param>
 <param-name>com.sun.jersey.config.property.packages</param-name>
 <param-value>thaihoanghai.jersey</param-value>
 </init-param>
 </servlet>

 <servlet-mapping>

OK h tao đả tạo xong phía server :  chúng ta chạy thử URL sau : http://localhost:8080/WS-RESTful-JAXB/rest/todo

kết quả của url trên là :

d1

======================================================================

Phần client :

Chúng ta viết class text sau để xem kết quả hiển thị :


package test;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Test {
 public static void main(String[] args) {
 ClientConfig config = new DefaultClientConfig();
 Client client = Client.create(config);

 WebResource service = client.resource(getBaseURI());

 // getXML
 System.out.println(service.path("rest").path("todo").accept(MediaType.TEXT_HTML).get(String.class));

 // get XML for application
 System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_JSON).get(String.class));

 // get XML for application
 System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_XML).get(String.class));
 }
 private static URI getBaseURI() {
 // TODO Auto-generated method stub
 return UriBuilder.fromUri("http://localhost:8080/WS-RESTful-JAXB").build();
 }
}

khi run chương trình đây là kết quả của chúng ta :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<todo>
   <description>This sumary...</description>
   <sumary>This sumary...</sumary>
</todo>

{"description":"This is description...","sumary":"This sumary..."}

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<todo>
   <description>This is description...</description>
   <sumary>This sumary...</sumary>
</todo>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: