【Developer Log】javax ws rs實現Restful
阿新 • • 發佈:2018-11-08
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
如何使用RestFul
下表來自wiki說得很清楚,GET一般用於查詢,POST一般用於建立,PUT用於update(如無則建立),DELETE使用者刪除。POST和PUT的不同在於,呼叫兩次POST則建立兩個資源,而呼叫兩次PUT,仍關聯一個資源。
Uniform Resource Locator (URL) | GET | POST | PUT | DELETE |
---|---|---|---|---|
Collection, such as http://api.example.com/resources/ | List the URIs and perhaps other details of the collection’s members. | Replace the entire collection with another collection. | Create a new entry in the collection. The new entry’s URI is assigned automatically and is usually returned by the operation. | Delete the entire collection. |
Element, such as http://api.example.com/resources/item17 | Retrieve |
Replace the addressed member of the collection, or if it does not exist, create it. | Not generally used. Treat the addressed member as a collection in its own right and create a new entry in it. | Delete the addressed member of the collection. |
Java中使用RestFul
RestFul是個web介面,因此在J2EE中使用。Spring含有對RestFul的支援,但此處,我們只討論普通情況下如何實現Restful。
pom:關聯的jar包
<!-- 匯入Bundles,可以替代Part1部分 <dependency> <groupId>org.glassfish.jersey.bundles</groupId> <artifactId>jaxrs-ri</artifactId> <version>2.23.2</version> </dependency> --> <!-- Part 1 --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.23.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-common</artifactId> <version>2.23.2</version> </dependency> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>2.23.2</version> </dependency> <!-- Part 2:支援Json格式 ,否則會報: MessageBodyWriter not found for media type=application/json ... 的錯誤 --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>2.23.2</version> </dependency> <!-- Part 3:支援複雜的Json格式翻譯,例如Map<K,E>,當然我們也可以使用Gson --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.23.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency> <!-- Part 4: 支援XML格式,否則會報MessageBodyWriter not found for media type=application/xml....--> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-xml-provider</artifactId> <version>2.8.1</version> </dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
web.xml
在web.xml中,需要加入以下內容。作用也很容易理解,需要在啟動時載入javax.ws.rs.core.Application,對應路徑為/rest/*。Jaxrs通過javax.ws.rs.core.Application這個servlet,關聯到對應的Restful介面
<servlet> <servlet-name>javax.ws.rs.core.Application</servlet-name> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>javax.ws.rs.core.Application</servlet-name> <url-pattern>/rest/*</url-pattern></servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
GET的小例子:text,json,xml三種輸出格式
@Path("/hello") //本類的Result路徑為/rest/hello/*(結合web.xml),如果直接在rest下面,可以用@path("/")public class MyService0 { private final String ROOT_NODE = "root"; //-----------例子1:返回text ---------- @GET //這是Restful中的GET方法 @Path("/text") //路徑為/rest/hello/text @Produces(MediaType.TEXT_PLAIN) //response的ContentType為text/plain public String getHelloWorld() { 因為輸出是text/plain,所以返回一個String,經過測試,即使有toString(),也不能是其他型別 return "Hello, my frist RESTFul Test"; } //-----------例子2:返回Json,JAXBElement<String>格式 ---------- @GET @Path("/json") @Produces(MediaType.APPLICATION_JSON) public JAXBElement<String> getHelloWorldJSON() { JAXBElement<String> result = new JAXBElement<String>( new QName("",ROOT_NODE), String.class, "Hello,JSR!"); return result; } //-----------例子3:URL帶引數,返回Json,直接物件格式 ---------- @GET @Path("/json/user/{id}") @Produces(MediaType.APPLICATION_JSON) public User getMe(@PathParam("id") String id) { User user = new User(); user.setId(id); user.setName(id + "-Test"); user.setEmail(id + "@hello"); return user; } //-----------例子4:URL帶引數,返回XML格式 ---------- @GET @Path("/xml/user/{id}") @Produces(MediaType.APPLICATION_XML) public User getUserInXML(@PathParam("id") String id) { User user = new User(); user.setId(id); user.setName(id + "-TestXML"); user.setEmail(id + "@XML"); return toReturn; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
POST,PUT,DELETE
從程式設計來講,和GET沒有什麼區別,都是從URL中獲得引數,根據需要返回text,json或者xml,知道如何獲取引數和返回結果,在程式設計上沒有區別,但是在使用上請看最前的表格說明,什麼情況下該用什麼。
@Path("/test1")public class MyService1 { //從結果看,Map適合模擬PUT的方式,對應的Vector適合模擬POST方式 private static final Map<String,User> dbMap = new HashMap<>(); private static final Vector<User> dbVector = new Vector<>(); //為實驗用,預先存放內容在dbMap中 static{ User user= new User(); user.setId("old"); user.setEmail("[email protected]"); user.setName("test1.old"); dbMap.put(user.getId(), user); } //【例子1.1】通過POST建立一個User,並存放在dbVector中 //請求訊息中帶有訊息體,訊息體是json格式(@consumes),翻譯存放在user引數中。 @POST @Path("/json/user/add") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result addUser(User user) { dbVector.add(user); return new Result(0,"OK"); } //【例子1.2】讀取dbVector的內容 @GET @Path("/json/user") @Produces(MediaType.APPLICATION_JSON) public Vector<User> showUser() { return dbVector; } //【例子2.1】通過PUT的方式,就要Id,建立或者更新一個使用者 @PUT @Path("/json/user/update") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Result updateUser(User user) { dbMap.put(user.getId(), user); return new Result(0,"OK"); } //【例子2.2】通過DELETE,引數某ID的使用者 @DELETE @Path("/json/user/delete/{id}") @Produces(MediaType.APPLICATION_JSON) public Result deleteUser(@PathParam("id") String id) { System.out.println("delete : id = " + id); dbMap.remove(id); return new Result(0,"OK"); } //【例子2.3.1】通過GET,使用Gson,來讀取dbMap @GET @Path("/json/userdb1") @Produces(MediaType.APPLICATION_JSON) public String showUser1() { Gson g = new Gson(); return g.toJson(dbMap); } //【例子2.3.2】通過GET,來讀取dbMap,在pom中需要加上part3,相對而言,我覺得Gson更為簡潔。 @GET @Path("/json/userdb2") @Produces(MediaType.APPLICATION_JSON) public Map<String,User> showUser2() { return db; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71