RESTEasy是JBoss的開源項目之一,是一個RESTful Web Services框架。java
趁今天有空,學習一下RESTEasy。看了兩篇博客後,本身寫了一個demo。web
依賴以下:apache
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fengyuan</groupId> <artifactId>RESTEasyDemo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>RESTEasyDemo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.4</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.3.GA</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>2.2.3.GA</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
UserService.java:json
package com.fengyuan.restapi; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import com.fengyuan.domain.User; @Path("userservice") // 服務路徑 public class UserService { /** * 初始化三個用戶數據,存入map中,key爲用戶id,value爲用戶對象 */ static Map<Integer, User> userMap = new HashMap<>(); static { User user1 = new User("Lee", 24, "138***"); userMap.put(1, user1); User user2 = new User("Cathy", 25, "188***"); userMap.put(2, user2); User user3 = new User("Aaron", 26, "186***"); userMap.put(3, user3); } /** * 獲取指定id的用戶 * * @param id * @return */ @GET @Path("user/{id}") // 具體服務的路徑, id是入參 @Produces("application/json") // 返回的格式 public User getById(@PathParam("id") Integer id) { return (User) userMap.get(id); } /** * 以json格式返回全部用戶 * * @return */ @GET @Path("users") @Produces("application/json") public List<User> getUsers() { List<User> userList = new ArrayList<User>(); for (Entry<Integer, User> user : userMap.entrySet()) { userList.add(user.getValue()); } return userList; } }
MessageService.java:api
package com.fengyuan.restapi; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/messageservice") public class MessageService { public MessageService(){} @GET @Path("/{param}") public Response printMessage(@PathParam("param") String msg) { String result = "Hello : " + msg; return Response.status(200).entity(result).build(); } }
User.java:瀏覽器
package com.fengyuan.domain; import lombok.AllArgsConstructor; import lombok.Data; public @Data @AllArgsConstructor class User { private String name; private int age; private String tel; }
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Restful Web Application</display-name> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> <!-- <context-param> <param-name>resteasy.resources</param-name> <param-value>com.fengyuan.restapi.MessageService, com.fengyuan.restapi.UserService</param-value> </context-param> --> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
配置resteasy.scan爲true表示自動掃描服務。若是不配置,也能夠手動指定resteasy.resources,見註釋掉的部分。tomcat
package com.fengyuan.test; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import junit.framework.TestCase; public class TestUserAPI extends TestCase { public static final String USER_API = "http://127.0.0.1:8080/resteasy-demo/rest/userservice/users"; public void testCreateUserAndGetUser() throws IOException { URL url = new URL(USER_API); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(1000); byte[] bytes = new byte[1024]; //讀取請求返回值 InputStream inStream=connection.getInputStream(); inStream.read(bytes, 0, inStream.available()); System.out.println(new String(bytes, "utf-8")); connection.disconnect(); } }
控制檯輸出:app
[{"name":"Lee","tel":"138***","age":24},{"name":"Cathy","tel":"188***","age":25},{"name":"Aaron","tel":"186***","age":26}]
固然,這裏只是簡單了測試了GET類型的服務,還有POST等類型的服務等有空了再補充。框架
另外,我在寫這個demo項目的時候,發現若是使用了2.1.多版本的resteasy-jackson-provider和resteasy-jaxrs包,而且在web.xml中配置了resteasy.scan爲true的話,會出一些問題,這邊提醒一下,讓看這篇博文的同窗不用再踩進這個坑~。dom