上一篇文章講解了通過Spring boot與JdbcTemplate、JPA和MyBatis的集成,實現對數據庫的訪問。今天主要給大家分享一下如何通過Spring boot向前端返回數據。
在現在的開發流程中,爲了最大程度實現前後端的分離,通常後端接口只提供數據接口,由前端通過Ajax請求從後端獲取數據並進行渲染再展示給用戶。我們用的最多的方式就是後端會返回給前端一個JSON字符串,前端解析JSON字符串生成JavaScript的對象,然後再做處理。本文就來演示一下Spring boot如何實現這種模式,本文重點會講解如何設計一個Restful的API,並通過Spring boot來實現相關的API。不過,爲了大家更好的瞭解Restful風格的API,我們先設計一個傳統的數據返回接口,這樣大家可以對比着來理解。
一、非Restful接口的支持
我們這裏以文章列表爲例,實現一個返回文章列表的接口,代碼如下:
?
REST是英文representational state transfer(表象性狀態轉變)或者表述性狀態轉移;Rest是web服務的一種架構風格;使用HTTP,URI,XML,JSON,HTML等廣泛流行的標準和協議;輕量級,跨平臺,跨語言的架構設計;它是一種設計風格,不是一種標準,是一種思想
Rest架構的主要原則
網絡上的所有事物都被抽象爲資源
每個資源都有一個唯一的資源標識符
同一個資源具有多種表現形式(xml,json等)
對資源的各種操作不會改變資源標識符
所有的操作都是無狀態的
符合REST原則的架構方式即可稱爲RESTful
什麼是Restful:
對應的中文是rest式的;Restful web service是一種常見的rest的應用,是遵守了rest風格的web服務;rest式的web服務是一種ROA(The Resource-Oriented Architecture)(面向資源的架構).
爲什麼會出現Restful
在Restful之前的操作:
http://127.0.0.1/user/query/1 GET 根據用戶id查詢用戶數據
http://127.0.0.1/user/save POST 新增用戶
http://127.0.0.1/user/update POST 修改用戶信息
http://127.0.0.1/user/delete GET/POST 刪除用戶信息
RESTful用法:
http://127.0.0.1/user/1 GET 根據用戶id查詢用戶數據
http://127.0.0.1/user POST 新增用戶
http://127.0.0.1/user PUT 修改用戶信息
http://127.0.0.1/user DELETE 刪除用戶信息
之前的操作是沒有問題的,大神認爲是有問題的,有什麼問題呢?你每次請求的接口或者地址,都在做描述,例如查詢的時候用了query,新增的時候用了save,其實完全沒有這個必要,我使用了get請求,就是查詢.使用post請求,就是新增的請求,我的意圖很明顯,完全沒有必要做描述,這就是爲什麼有了restful.
如何使用:
SpringMVC實現restful服務:
SpringMVC原生態的支持了REST風格的架構設計
所涉及到的註解:
--@RequestMapping
---@PathVariable
---@ResponseBody
package cn.itcast.mybatis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.mybatis.pojo.User;
import cn.itcast.mybatis.service.NewUserService;
@RequestMapping("restful/user")
@Controller
public class RestUserController {
@Autowired
private NewUserService newUserService;
/**
* 根據用戶id查詢用戶數據
*
* @param id
* @return
*/
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
try {
User user = this.newUserService.queryUserById(id);
if (null == user) {
// 資源不存在,響應404
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
// 200
// return ResponseEntity.status(HttpStatus.OK).body(user);
return ResponseEntity.ok(user);
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 新增用戶
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Void> saveUser(User user) {
try {
this.newUserService.saveUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 更新用戶資源
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<Void> updateUser(User user) {
try {
this.newUserService.updateUser(user);
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/**
* 刪除用戶資源
*
* @param user
* @return
*/
@RequestMapping(method = RequestMethod.DELETE)
public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {
try {
if (id.intValue() == 0) {
// 請求參數有誤
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
this.newUserService.deleteUserById(id);
// 204
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
} catch (Exception e) {
e.printStackTrace();
}
// 500
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
HTTP相應狀態碼:
總結:
restful就是舊技術,新風格.之前寫過一篇關於restful接口的博客:【Restful接口】restful接口的兩種使用方式
---------------------
作者:陳曉嬋
來源:CSDN
原文:https://blog.csdn.net/chenxiaochan/article/details/73716617
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!
|
|
這個ArticleService的實現很簡單,就是簡單的封裝了ArticleMapper的操作,ArticleMapper的內容大家可以參考上一篇的文章,ArticleService的實現類如下:
?
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
|
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleMapper articleMapper;
@Override
public Long saveArticle( @RequestBody Article article) {
return articleMapper.insertArticle(article);
}
@Override
public List<Article> getArticles(String title,Long userId, int offset, int pageSize) {
Article article = new Article();
article.setTitle(title);
article.setUserId(userId);
return articleMapper.queryArticlesByPage(article,offset,pageSize);
}
@Override
public Article getById(Long id) {
return articleMapper.queryById(id);
}
@Override
public void updateArticle(Article article) {
article.setUpdateTime( new Date());
articleMapper.updateArticleById(article);
}
}
|
運行Application.java這個類,然後訪問:http://locahost:8080/article/list.json,就可以看到如下的結果:
ArticleServiceImpl這個類是一個很普通的類,只有一個Spring的註解@Service,標識爲一個bean以便於通過Spring IoC容器來管理。我們再來看看ArticleController這個類,其實用過Spring MVC的人應該都熟悉這幾個註解,這裏簡單解釋一下:
@Controller 標識一個類爲控制器。
@RequestMapping URL的映射。
@ResponseBody 返回結果轉換爲JSON字符串。
@RequestBody 表示接收JSON格式字符串參數。
通過這個三個註解,我們就能輕鬆的實現通過URL給前端返回JSON格式數據的功能。不過大家肯定有點疑惑,這不都是Spring MVC的東西嗎?跟Spring boot有什麼關係?其實Spring boot的作用就是爲我們省去了配置的過程,其他功能確實都是Spring與Spring MVC來爲我們提供的,大家應該記得Spring boot通過各種starter來爲我們提供自動配置的服務,我們的工程裏面之前引入過這個依賴:
?
1
2
3
4
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-web</ artifactId >
</ dependency >
|
這個是所有Spring boot的web工程都需要引入的jar包,也就是說只要是Spring boot的web的工程,都默認支持上述的功能。這裏我們進一步發現,通過Spring boot來開發web工程,確實爲我們省了許多配置的工作。
二、Restful API設計
好了,我們現在再來看看如何實現Restful API。實際上Restful本身不是一項什麼高深的技術,而只是一種編程風格,或者說是一種設計風格。在傳統的http接口設計中,我們一般只使用了get和post兩個方法,然後用我們自己定義的詞彙來表示不同的操作,比如上面查詢文章的接口,我們定義了article/list.json來表示查詢文章列表,可以通過get或者post方法來訪問。而Restful API的設計則通過HTTP的方法來表示CRUD相關的操作。因此,除了get和post方法外,還會用到其他的HTTP方法,如PUT、DELETE、HEAD等,通過不同的HTTP方法來表示不同含義的操作。下面是我設計的一組對文章的增刪改查的Restful API:
接口URL |
HTTP方法 |
接口說明 |
/article |
POST |
保存文章 |
/article/{id} |
GET |
查詢文章列表 |
/article/{id} |
DELETE |
刪除文章 |
/article/{id} |
PUT |
更新文章信息 |
這裏可以看出,URL僅僅是標識資源的路勁,而具體的行爲由HTTP方法來指定。
三、Restful API實現
現在我們再來看看如何實現上面的接口,其他就不多說,直接看代碼:
?
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
|
@RestController
@RequestMapping ( "/rest" )
public class ArticleRestController {
@Autowired
private ArticleService articleService;
@RequestMapping (value = "/article" , method = POST, produces = "application/json" )
public WebResponse<Map<String, Object>> saveArticle( @RequestBody Article article) {
article.setUserId(1L);
articleService.saveArticle(article);
Map<String, Object> ret = new HashMap<>();
ret.put( "id" , article.getId());
WebResponse<Map<String, Object>> response = WebResponse.getSuccessResponse(ret);
return response;
}
@RequestMapping (value = "/article/{id}" , method = DELETE, produces = "application/json" )
public WebResponse<?> deleteArticle( @PathVariable Long id) {
Article article = articleService.getById(id);
article.setStatus(- 1 );
articleService.updateArticle(article);
WebResponse<Object> response = WebResponse.getSuccessResponse( null );
return response;
}
@RequestMapping (value = "/article/{id}" , method = PUT, produces = "application/json" )
public WebResponse<Object> updateArticle( @PathVariable Long id, @RequestBody Article article) {
article.setId(id);
articleService.updateArticle(article);
WebResponse<Object> response = WebResponse.getSuccessResponse( null );
return response;
}
@RequestMapping (value = "/article/{id}" , method = GET, produces = "application/json" )
public WebResponse<Article> getArticle( @PathVariable Long id) {
Article article = articleService.getById(id);
WebResponse<Article> response = WebResponse.getSuccessResponse(article);
return response;
}
}
|
我們再來分析一下這段代碼,這段代碼和之前代碼的區別在於:
(1)我們使用的是@RestController這個註解,而不是@Controller,不過這個註解同樣不是Spring boot提供的,而是Spring MVC4中的提供的註解,表示一個支持Restful的控制器。
(2)這個類中有三個URL映射是相同的,即都是/article/{id},這在@Controller標識的類中是不允許出現的。這裏的可以通過method來進行區分,produces的作用是表示返回結果的類型是JSON。
(3)@PathVariable這個註解,也是Spring MVC提供的,其作用是表示該變量的值是從訪問路徑中獲取。
所以看來看去,這個代碼還是跟Spring boot沒太多的關係,Spring boot也僅僅是提供自動配置的功能,這也是Spring boot用起來很舒服的一個很重要的原因,因爲它的侵入性非常非常小,你基本感覺不到它的存在。
四、測試
代碼寫完了,怎麼測試?除了GET的方法外,都不能直接通過瀏覽器來訪問,當然,我們可以直接通過postman來發送各種http請求。不過我還是比較支持通過單元測試類來測試各個方法。這裏我們就通過Junit來測試各個方法:
?
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
|
@RunWith (SpringJUnit4ClassRunner. class )
@SpringBootTest (classes = Application. class )
public class ArticleControllerTest {
@Autowired
private ArticleRestController restController;
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(restController).build();
}
@Test
public void testAddArticle() throws Exception {
Article article = new Article();
article.setTitle( "測試文章000000" );
article.setType( 1 );
article.setStatus( 2 );
article.setSummary( "這是一篇測試文章" );
Gson gosn = new Gson();
RequestBuilder builder = MockMvcRequestBuilders
.post( "/rest/article" )
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(gosn.toJson(article));
MvcResult result = mvc.perform(builder).andReturn();
System.out.println(result.getResponse().getContentAsString());
}
@Test
public void testUpdateArticle() throws Exception {
Article article = new Article();
article.setTitle( "更新測試文章" );
article.setType( 1 );
article.setStatus( 2 );
article.setSummary( "這是一篇更新測試文章" );
Gson gosn = new Gson();
RequestBuilder builder = MockMvcRequestBuilders
.put( "/rest/article/1" )
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(gosn.toJson(article));
MvcResult result = mvc.perform(builder).andReturn();
}
@Test
public void testQueryArticle() throws Exception {
RequestBuilder builder = MockMvcRequestBuilders
.get( "/rest/article/1" )
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8);
MvcResult result = mvc.perform(builder).andReturn();
System.out.println(result.getResponse().getContentAsString());
}
@Test
public void testDeleteArticle() throws Exception {
RequestBuilder builder = MockMvcRequestBuilders
.delete( "/rest/article/1" )
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_UTF8);
MvcResult result = mvc.perform(builder).andReturn();
}
}
|
執行結果這裏就不給大家貼了,大家有興趣的話可以自己實驗一下。整個類要說明的點還是很少,主要這些東西都與Spring boot沒關係,支持這些操作的原因還是上一篇文章中提到的引入對應的starter:
?
1
2
3
4
5
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-test</ artifactId >
< scope >test</ scope >
</ dependency >
|
因爲要執行HTTP請求,所以這裏使用了MockMvc,ArticleRestController通過注入的方式實例化,不能直接new,否則ArticleRestController就不能通過Spring IoC容器來管理,因而其依賴的其他類也無法正常注入。通過MockMvc我們就可以輕鬆的實現HTTP的DELETE/PUT/POST等方法了。
五、總結
本文講解了如果通過Spring boot來實現Restful的API,其實大部分東西都是Spring和Spring MVC提供的,Spring boot只是提供自動配置的功能。但是,正是這種自動配置,爲我們減少了很多的開發和維護工作,使我們能更加簡單、高效的實現一個web工程,從而讓我們能夠更加專注於業務本身的開發,而不需要去關心框架的東西。