2015年2月4日 星期三

RESTful Web Services with Java



#本篇是翻譯文學(X)

RESTful Web Services with Java



J2EE  RESTful的解決方案一大堆

這次將外國人的實作教學翻成中文

自己做一遍,順便當成紀錄這樣

如果你喜歡CCR...不是... 如果你喜歡外國人的文

請直接看原文 3Q





開發環境與使用素材

1. Eclipse Kepler 
2. C:\Program Files (x86)\Java\jre7
3. jersey jersey-archive-1.17.1  (RESTful Web Services in Java.)
4. Apache Tomcat 7.0.x





在Eclipse 新增[其他]專案



選擇 dynamic web project

接著命名為RESTfulWS

那個 選阿帕契的湯姆貓第七版 然後下一步下一步完成


專案開好後

把剛剛Jersey (傑西) 下載回來的壓縮檔打開  jersey-archive-1.17.1 

裡面長這樣




lib資料夾裡面

  • asm-3.1.jar
  • jersey-client-1.17.1.jar
  • jersey-core-1.17.1.jar
  • jersey-json-1.17.1.jar
  • jersey-server-1.17.1.jar
  • jersey-servlet-1.17.1.jar
  • jsr311-api-1.1.1.jar
把這些複製貼上到專案目錄中WEB-INF -> lib

專案目錄右鍵Propertise -> Java build Path 並且加入引用




請在專案目錄中建立web.xml

/RESTfulWS/WebContent/WEB-INF/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"
    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>


請在專案目錄中

建立
1.package com.eviac.blog.restclient
2.class UserInfoClient.java

/RESTfulWS/src/com/eviac/blog/restclient/UserInfoClient.java

程式碼如下



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;




/**
*&nbsp;
* @author pavithra
*&nbsp;
*/
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));
}




/**
* Returns client response.
* e.g :&nbsp;
* GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra&nbsp;
* returned a response status of 200 OK
*
* @param service
* @return
*/
private static String getClientResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class)
.toString();
}




/**
* Returns the response as XML
* e.g : <User><Name>Pavithra</Name></User>&nbsp;
*&nbsp;
* @param service
* @return
*/
private static String getResponse(WebResource resource) {
return resource.accept(MediaType.TEXT_XML).get(String.class);
}
}


請在專案目錄中

建立
1.package com.eviac.blog.restws
2.class UserInfo.java

/RESTfulWS/src/com/eviac/blog/restws/UserInfo.java


程式碼如下





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 here defines class level path. Identifies the URI path that
// a resource class will serve requests for.
@Path("UserInfoService")
public class UserInfo {




// @GET here defines, this method will method will process HTTP GET
// requests.
@GET
// @Path here defines method level path. Identifies the URI path that a
// resource class method will serve requests for.
@Path("/name/{i}")
// @Produces here defines the media type(s) that the methods
// of a resource class can produce.
@Produces(MediaType.TEXT_XML)
// @PathParam injects the value of URI parameter that defined in @Path
// expression, into the method.
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>";
}
}



在專案上按右鍵 
     run as ->run on server.



按下Finish

別驚慌

http://localhost:8080/

不是你要去的地方

請訪問

http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra



http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25


age這最後面參數可以自己帶

http://localhost:8080/RESTfulWS/rest/UserInfoService/age/18




熱門文章