上一篇文章講解了經過Spring boot與JdbcTemplate、JPA和MyBatis的集成,實現對數據庫的訪問。今天主要給你們分享一下如何經過Spring boot向前端返回數據。前端
在如今的開發流程中,爲了最大程度實現先後端的分離,一般後端接口只提供數據接口,由前端經過Ajax請求從後端獲取數據並進行渲染再展現給用戶。咱們用的最多的方式就是後端會返回給前端一個JSON字符串,前端解析JSON字符串生成JavaScript的對象,而後再作處理。本文就來演示一下Spring boot如何實現這種模式,本文重點會講解如何設計一個Restful的API,並經過Spring boot來實現相關的API。不過,爲了你們更好的瞭解Restful風格的API,咱們先設計一個傳統的數據返回接口,這樣你們能夠對比着來理解。java
1、非Restful接口的支持web
咱們這裏以文章列表爲例,實現一個返回文章列表的接口,代碼以下:spring
@Controller @RequestMapping("/article") public class ArticleController { @Autowired private ArticleService articleService; @RequestMapping("/list.json") @ResponseBody public List<Article> listArticles(String title, Integer pageSize, Integer pageNum) { if (pageSize == null) { pageSize = 10; } if (pageNum == null) { pageNum = 1; } int offset = (pageNum - 1) * pageSize; return articleService.getArticles(title, 1L, offset, pageSize); } }
這個ArticleService的實現很簡單,就是簡單的封裝了ArticleMapper的操做,ArticleMapper的內容你們能夠參考上一篇的文章,ArticleService的實現類以下:數據庫
@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的人應該都熟悉這幾個註解,這裏簡單解釋一下:json
@Controller 標識一個類爲控制器。後端
@RequestMapping URL的映射。瀏覽器
@ResponseBody 返回結果轉換爲JSON字符串。mvc
@RequestBody 表示接收JSON格式字符串參數。
經過這個三個註解,咱們就能輕鬆的實現經過URL給前端返回JSON格式數據的功能。不過你們確定有點疑惑,這不都是Spring MVC的東西嗎?跟Spring boot有什麼關係?其實Spring boot的做用就是爲咱們省去了配置的過程,其餘功能確實都是Spring與Spring MVC來爲咱們提供的,你們應該記得Spring boot經過各類starter來爲咱們提供自動配置的服務,咱們的工程裏面以前引入過這個依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
這個是全部Spring boot的web工程都須要引入的jar包,也就是說只要是Spring boot的web的工程,都默認支持上述的功能。這裏咱們進一步發現,經過Spring boot來開發web工程,確實爲咱們省了許多配置的工做。
2、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方法來指定。
3、Restful API實現
如今咱們再來看看如何實現上面的接口,其餘就很少說,直接看代碼:
@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用起來很舒服的一個很重要的緣由,由於它的侵入性很是很是小,你基本感受不到它的存在。
4、測試
代碼寫完了,怎麼測試?除了GET的方法外,都不能直接經過瀏覽器來訪問,固然,咱們能夠直接經過postman來發送各類http請求。不過我仍是比較支持經過單元測試類來測試各個方法。這裏咱們就經過Junit來測試各個方法:
@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:
<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等方法了。
,
5、總結
本文講解了若是經過Spring boot來實現Restful的API,其實大部分東西都是Spring和Spring MVC提供的,Spring boot只是提供自動配置的功能。可是,正是這種自動配置,爲咱們減小了不少的開發和維護工做,使咱們能更加簡單、高效的實現一個web工程,從而讓咱們可以更加專一於業務自己的開發,而不須要去關心框架的東西。這篇文章中咱們提到了能夠經過postman和junit的方式來訪問Restful 接口,下篇文章咱們會介紹另一種方式來訪問,有興趣的能夠繼續關注一下。