Rest之路 - 第一个Rest程序
在 Eclipse 里新建一个 Dynamic project
将 Jersey 的 jar 包,拷贝到 WebContent -> WEB-INF -> lib 文件夹
Add jars to "build path"
根据业务层次编写程序
User
package com.example.tuo.rest.entity; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="user") public class User { private int id; private String name; private String profession; public int getId() { return id; } //@XmlElement public void setId(int id) { this.id = id; } public String getName() { return name; } //@XmlElement public void setName(String name) { this.name = name; } public String getProfession() { return profession; } //@XmlElement public void setProfession(String profession) { this.profession = profession; } public User() { super(); // TODO Auto-generated constructor stub } public User(int id, String name, String profession) { super(); this.id = id; this.name = name; this.profession = profession; } }
P.S 由于后面使用XML格式进行数据展现,在这里使用了@XMLRootElement和@XMLElement 注解。
UserDao
package com.example.tuo.rest.dao; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import com.example.tuo.rest.entity.User; public class UserDao { private final String CONFIG_FILE = "D:/User.dat"; public List<User> getAllUsers() { List<User> uList = null; File file = new File(CONFIG_FILE); if(!file.exists()){ User u = new User(1,"tuo","software engineer"); uList= new ArrayList<User>(); uList.add(u); saveUserList(uList); } else{ FileInputStream in = null; ObjectInputStream oin = null; try { in = new FileInputStream(file); oin = new ObjectInputStream(in); uList=(List<User>) oin.readObject(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { if(oin != null) oin.close(); if(in != null) in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return uList; } private void saveUserList(List<User> userList){ try { File file = new File(CONFIG_FILE); FileOutputStream fos; fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(userList); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
在这里,使用了一个在磁盘上的文件作模拟数据库的存储
UserService
package com.example.tuo.rest.service; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.example.tuo.rest.dao.UserDao; import com.example.tuo.rest.entity.User; @Path("/UserService") public class UserService { private UserDao userDao = new UserDao(); @GET @Path("/users") @Produces(MediaType.APPLICATION_XML) public List<User> getAllUsers(){ return userDao.getAllUsers(); } }
使用 @Path 注解,制定了 Rest 的路径(完整路径为:project/class/method)
使用 @GET 注解,标识 Rest 方法的类型
使用 @Produces(MediaType.APPLICATION_XML) 注解,标识数据表现的格式为 XML
修改 Web.xml configuration File
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>FirstHeadRestfulWebService</display-name>
<servlet>
<servlet-name>My Jersey restful service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example.tuo.rest.service</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>My Jersey restful service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
注: 橙色部分表明需要扫描的包, 即 service 所在的 package
启动Rest服务并且验证
输入rest 服务地址 http://localhost:8080/FirstHeadRestfulWebService/rest/UserService/users
FirstHeadRestfulWebService - 项目目录(名称)
rest - web.xml配置的根目录
UserService/users - Class 路径 + Method 路径