本篇將使用Spring Boot發佈REST Service,使用Spring REST Template來調用服務
本篇代碼均已上傳到GITHUB,https://github.com/IloveZiHan/restfultemplate.git
介紹
我們之前都使用過HttpClient來發送HTTP請求,但我們很多時間都需要將HttpClient請求或者響應的數據轉換爲Java對象,HttpClient需要我們自己手動來解析。我們今天要介紹的Spring REST Template比HttpClient用起來更加方便簡潔。
背景
案例分爲兩個部分:
- CRM_MANAGEMENT——後臺主要提供RESTFUL Service服務,因爲儘量使案例簡單,所以數據部分是直接使用內存中的列表來模擬
- PORTAL——門戶,也就是前端系統,用來處理瀏覽器提交的請求,接收到用戶請求會使用Spring REST Template來提交RESTFUL Service請求
環境說明
- 本案例基於Maven構建,使用Spring Boot 2.x開發
- 可以選擇IDEA或者Eclipse導入案例運行
項目開發
CRM_DOMAIN實體
POM依賴
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>resttemplate_common</artifactId>
<groupId>com.itheima</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion><artifactId>crm_domain</artifactId> <packaging>jar</packaging>
</project>
-
User實體類
package com.itheima.crm.domain;
public class User {
private Integer id; // id private String username; // 用戶名 private String password; // 密碼 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public boolean matchUsernameAndPassword(String username, String password) { if(this.username.equals(username) && this.password.equals(password)) { return true; } else { return false; } }
}
ResResult——封裝響應結果
package com.itheima.crm.domain;
/**
* 響應結果
*/
public class ResResult {
private Boolean success; // 是否操作成功
private String message; // 消息
public ResResult() {
}
public ResResult(Boolean success, String message) {
this.success = success;
this.message = message;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
CRM_MANAGEMENT後臺
POM依賴
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion> <artifactId>crm_management</artifactId> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.itheima</groupId> <artifactId>crm_domain</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
RESTFUL Service接口開發
package com.itheima.crm.webservice;
import com.itheima.crm.domain.ResResult;
import com.itheima.crm.domain.User ;
import com.itheima.crm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserRestService {
@Autowired
private UserService userService;
/**
* 註冊服務
* @param user 用戶
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public ResResult regist(@RequestBody User user) {
try {
userService.regist(user);
return new ResResult(true, "添加成功");
} catch (Exception e) {
e.printStackTrace();
return new ResResult(false, "添加失敗");
}
}
/**
* 登錄服務
* @param username 用戶名
* @param password 密碼
* @return
*/
@RequestMapping(value = "/{username}/{password}", method = RequestMethod.GET)
public User login(@PathVariable("username") String username, @PathVariable("password") String password) {
try {
return userService.login(username, password);
} catch (Exception e) {
return null;
}
}
@RequestMapping(method = RequestMethod.GET)
public List<User> findAll() {
return userService.findAll();
}
}
Service層開發
-
UserService接口
package com.itheima.crm.service;
import com.itheima.crm.domain.User ;
import java.util.List;
public interface UserService {
void regist(User user);User login(String username, String password); List<User> findAll();
}
-
UserServiceImpl實現類
package com.itheima.crm.service;
import com.itheima.crm.dao.UserDao;
import com.itheima.crm.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class UserServiceImpl implements UserService{@Autowired private UserDao userDao; @Override public void regist(User user) { userDao.save(user); } @Override public User login(String username, String password) { return userDao.findOne(username, password); } @Override public List<User> findAll() { return userDao.findAll(); }
}
DAO層開發
-
UserDao接口
package com.itheima.crm.dao;
import com.itheima.crm.domain.User ;
import java.util.List;
public interface UserDao {
/**- 保存用戶
-
@param user 用戶
*/
void save(User user);User findOne(String username, String password);
List<User> findAll();
}
-
UserDaoImpl實現類
package com.itheima.crm.dao;
import com.itheima.crm.domain.User;
import org.springframework.stereotype.Repository;import java.util.ArrayList;
import java.util.List;@Repository
public class UserDaoImpl implements UserDao{private List<User> users = new ArrayList<>(); @Override public void save(User user) { users.add(user); } @Override public User findOne(String username, String password) { User userTmp = new User(); userTmp.setUsername(username); userTmp.setPassword(password); for(User user : users) { if(user.matchUsernameAndPassword(username, password)) { return user; } } return null; } @Override public List<User> findAll() { return users; } public UserDaoImpl() { // 添加一些測試的用戶 User user1 = new User(); user1.setId(1); user1.setUsername("zhangsan"); user1.setPassword("0"); User user2 = new User(); user2.setId(2); user2.setUsername("lisi"); user2.setPassword("0"); User user3 = new User(); user3.setId(3); user3.setUsername("wangwu"); user3.setPassword("0"); User user4 = new User(); user4.setId(4); user4.setUsername("zhaoliu"); user4.setPassword("0"); users.add(user1); users.add(user2); users.add(user3); users.add(user4); }
}
Spring Boot入口
-
配置文件(application.properties)
server.port=9001
server.servlet.context-path=/ -
入口
package com.itheima.crm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}
RESTFUL Service接口調用URL
- 添加用戶
Requsest URL: http://localhost:9001/user/ Request Method: POST - 根據用戶名密碼查詢用戶
Requsest URL: http://localhost:9001/user/用戶名/密碼 Request Method: GET - 查詢所有用戶
Requeset URL: http://localhost:9001/user/ Request Method: GET
PORTAL門戶
POM依賴
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>portal</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency><dependency>
<groupId>com.itheima</groupId>
<artifactId>crm_domain</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Controller開發
package com.itheima.portal.controller;
import com.itheima.crm.domain.ResResult;
import com.itheima.crm.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private RestTemplate restTemplate;
private String USERSERVICE_URL = "http://localhost:9001/user";
@RequestMapping("/login")
public Map login(String username, String password) {
// 構建要傳遞的參數
Map<String, String> params = new HashMap<>();
params.put("username", username);
params.put("password", password);
// 發送GET請求
User user = restTemplate.getForObject(USERSERVICE_URL + "/{username}/{password}", User.class, params);
Map<String, Object> result = new HashMap<>();
if(user != null) {
result.put("success", true);
}
else {
result.put("success", false);
result.put("message", "登錄失敗");
}
return result;
}
@RequestMapping("/regist")
public ResResult login(@RequestBody User user) throws IOException {
// 使用REST Template發送POST請求
return restTemplate.postForObject(USERSERVICE_URL + "/", user, ResResult.class);
}
}
Spring Boot入口
-
配置文件(application.properties)
server.port=9002
server.servlet.context-path=/ -
入口
package com.itheima.portal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
public class Application {public static void main(String[] args) { SpringApplication.run(Application.class, args); } /** * 創建RestTemplate Bean */ @Bean public RestTemplate restTemplate() { return new RestTemplate(); }
}
- 前端頁面
- 導入jquery.js
-
login.html登錄頁面
<!doctype html>
<html lang="`">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>登錄</title>
<script src="jquery-1.11.3.js"></script>
</head>
<body>
<table class="table">
<tr>
<td>用戶名</td>
<td><input type="text" id="username"></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" id="password"></td>
</tr>
<tr>
<td></td>
<td>
<button id="btn">登錄</button>
<button id="btn1">註冊</button>
</td>
</tr>
</table><script type="text/javascript"> $(function() { $('#btn').click(function() { $.get('user/login?username=' + $('#username').val() + '&password=' + $('#password').val(), function(data) { if(!data.success) { alert(data.message); } else { window.location.href = "success.html"; } }); }); $('#btn1').click(function() { var user = { username: $('#username').val(), password: $('#password').val() }; $.ajax({ type:'POST', url:'user/regist', dataType:"json", contentType:"application/json", data:JSON.stringify(user), success:function(data){ if(!data.success) { alert(data.message); } else { alert("註冊成功!"); } } }); }); }); </script>
</body>
</html> -
success.html登錄成功頁面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄成功</title>
</head>
<body>
<h1>登錄成功</h1>
</body>
</html>
總結
Spring REST Template比HttpClient更加快捷方便的調用RS服務,它自動封裝和解析Java對象。而且可以很方便的與Spring整合,快速注入Spring的bean對象。
注意
注意Jquery $.get、$.post、$.ajax的區別
-
$.get(url, json對象)表示發送一個get請求,使用queryString的方式提交。url?username=tom...
- $.post(url, json對象)表示發送一個post請求,數據部分類似於提交表單。
-
$.ajax可以選擇數據傳遞的格式、類型等。我們要將一個json串發送到spring mvc @RequestBody來接收需要使用$.ajax。下面是示例代碼
$.ajax({
type:'POST',
url:'user/regist',
dataType:"json",
contentType:"application/json",
data:JSON.stringify(user),
success:function(data){
if(!data.success) {
alert(data.message);
}
else {
alert("註冊成功!");
}
}
});