Javaを使用してrestサービスを作成し、HTTPを通じてリソースへのアクセスを要求します.

3500 ワード

jereyを使ってrest webserviceを作成します.
1はeclipseの中で動的なウェブプロジェクトを創建します.
2 build jerey 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 
" + getClientResponse(nameResource)); System.out.println("Response
" + getResponse(nameResource) + "

"); WebResource ageResource = resource.path("rest").path(PATH_AGE + age); System.out.println("Client Response
" + getClientResponse(ageResource)); System.out.println("Response
" + 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>