首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

使用Java创设RESTful Web Service

2014-04-19 
使用Java创建RESTful Web Service???package com.eviac.blog.restws import javax.ws.rs.GETimport java

使用Java创建RESTful Web Service

?

使用Java创设RESTful Web Service

?

?

package com.eviac.blog.restws; 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; /**** @author pavithra**/ // 这里@Path定义了类的层次路径。// 指定了资源类提供服务的URI路径。@Path("UserInfoService")public class UserInfo { // @GET表示方法会处理HTTP GET请求@GET// 这里@Path定义了类的层次路径。指定了资源类提供服务的URI路径。@Path("/name/{i}")// @Produces定义了资源类方法会生成的媒体类型。@Produces(MediaType.TEXT_XML)// @PathParam向@Path定义的表达式注入URI参数值。public String userName(@PathParam("i") String i) { String name = i;return "<User>" + "<Name>" + name + "</Name>" + "</User>";} @GET@Path("/age/{j}")@Produces(MediaType.TEXT_XML)public String userAge(@PathParam("j") int j) { int age = j;return "<User>" + "<Age>" + age + "</Age>" + "</User>";}}

?

?

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 <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</a> id="WebApp_ID" version="2.5"><display-name>RESTfulWS</display-name><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>com.eviac.blog.restws</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>

?

?

将此URL拷贝到浏览器地址栏中运行:http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

使用Java创设RESTful Web Service

输出结果如下:

使用Java创设RESTful Web Service创建客户端

?

?

创建一个“com.eviac.blog.restclient”包,然后新建“UserInfoClient”类。

package com.eviac.blog.restclient; import javax.ws.rs.core.MediaType; import com.sun.jersey.api.client.Client;import com.sun.jersey.api.client.ClientResponse;import com.sun.jersey.api.client.WebResource;import com.sun.jersey.api.client.config.ClientConfig;import com.sun.jersey.api.client.config.DefaultClientConfig; /**** @author pavithra**/public class UserInfoClient { public static final String BASE_URI = "http://localhost:8080/RESTfulWS";public static final String PATH_NAME = "/UserInfoService/name/";public static final String PATH_AGE = "/UserInfoService/age/"; public static void main(String[] args) { String name = "Pavithra";int age = 25; ClientConfig config = new DefaultClientConfig();Client client = Client.create(config);WebResource resource = client.resource(BASE_URI); WebResource nameResource = resource.path("rest").path(PATH_NAME + name);System.out.println("Client Response \n"+ getClientResponse(nameResource));System.out.println("Response \n" + getResponse(nameResource) + "\n\n"); WebResource ageResource = resource.path("rest").path(PATH_AGE + age);System.out.println("Client Response \n"+ getClientResponse(ageResource));System.out.println("Response \n" + getResponse(ageResource));} /*** 返回客户端请求。* 例如:* GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra* 返回请求结果状态“200 OK”。** @param service* @return*/private static String getClientResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString();} /*** 返回请求结果XML* 例如:<User><Name>Pavithra</Name></User>** @param service* @return*/private static String getResponse(WebResource resource) {return resource.accept(MediaType.TEXT_XML).get(String.class);}}

?运行客户端程序后,可以看到以下输出:

Client ResponseGET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OKResponse<User><Name>Pavithra</Name></User> Client ResponseGET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OKResponse<User><Age>25</Age></User>

?

?

热点排行