您的位置:首页 > 编程语言 > Java开发

使用Java创建rest 服务 通过HTTP请求访问资源

2015-03-18 11:38 686 查看
使用jersey创建rest webservice

1 在eclipse中创建动态web工程

2 build jersey jar包

3 创建rest 服务端

package com.kcharf.gis.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;

//这里@Path定义了类的层次路径
//指定了资源类提供该服务的URI路径
@Path("UserInfoService")
public class UserInfo {
//@Get表示方法会处理HTTP get请求
@GET
@Path("/name/{i}")//指定资源类提供服务的uri路径
@Produces(MediaType.TEXT_XML)//资源类方法会产生媒体类型
//PathParam向Path定义的表达式注入uri参数值
public String userName(@PathParam("i") String i){
String name = i;
return "<User>" + "<Name>" + name + "</Nmae>" + "</User>";
}
}


4 创建客户端测试

package com.kcharf.gis.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/restws";
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/restws/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);
}


5 部署web.xml到WEB-INF下

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<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.kcharf.gis.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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐