restfull环境搭建-helloword(二)

时间:2023-03-09 08:27:22
restfull环境搭建-helloword(二)

原文地址:http://only81.iteye.com/blog/1689537

本文描述,获取XML或json格式数据

首先,创建一个bean,比如Todo(JAXB自动将bean文件,转换成xml或者json,需要添加@XmlRootElement)

package sample.hello.resources.bean;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Todo {
private String summary;
private String description;
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
} }

创建对应的resource

package sample.hello.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType; import sample.hello.resources.bean.Todo; @Path("todo")
public class TodoResource { @GET
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Todo getXml(){
Todo todo = new Todo();
todo.setSummary("This is first todo");
todo.setDescription("This is my first todo");
return todo;
} @GET
@Produces({MediaType.TEXT_XML})
public Todo getHtml(){
Todo todo = new Todo();
todo.setSummary("This is first todo");
todo.setDescription("This is my first todo");
return todo;
}
}

对应的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Restfull</display-name>
<welcome-file-list>
<welcome-file>index.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>sample.hello.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping> </web-app>

到此为止,服务器端程序,创建完毕。

客户端取xml或json数据程序:

package sample.hello.resources.client;

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 TodoTest { public static void main(String[] args) {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
System.out.println(service.path("rest").path("todo").accept(MediaType.TEXT_XML).get(String.class));
System.out.println("....");
System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_JSON).get(String.class));
// Get JSON for application
System.out.println("....");
System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_XML).get(String.class)); } private static URI getBaseURI(){
return UriBuilder.fromUri("http://localhost:8080/Restfull").build();
} }

运行结果,如下:

restfull环境搭建-helloword(二)

对应源码下载链接:http://pan.baidu.com/s/1jHO5eVs 密码:vqbj