天天看點

Spring Boot實戰:Restful API的建構

本文講解了如果通過Spring boot來實作前後端的互動,首先示範了傳統的資料互動方式,然後重點講解如何設計一個Restful的API,并通過Spring boot來實作相關的API。

  上一篇文章講解了通過Spring boot與JdbcTemplate、JPA和MyBatis的內建,實作對資料庫的通路。今天主要給大家分享一下如何通過Spring boot向前端傳回資料。

  在現在的開發流程中,為了最大程度實作前後端的分離,通常後端接口隻提供資料接口,由前端通過Ajax請求從後端擷取資料并進行渲染再展示給使用者。我們用的最多的方式就是後端會傳回給前端一個JSON字元串,前端解析JSON字元串生成JavaScript的對象,然後再做處理。本文就來示範一下Spring boot如何實作這種模式,本文重點會講解如何設計一個Restful的API,并通過Spring boot來實作相關的API。不過,為了大家更好的了解Restful風格的API,我們先設計一個傳統的資料傳回接口,這樣大家可以對比着來了解。

一、非Restful接口的支援

  我們這裡以文章清單為例,實作一個傳回文章清單的接口,代碼如下:

@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,就可以看到如下的結果:

Spring Boot實戰:Restful API的建構

  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來為我們提供自動配置的服務,我們的工程裡面之前引入過這個依賴:

<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  查詢文章清單
 DELETE  删除文章
 PUT  更新文章資訊

   這裡可以看出,URL僅僅是辨別資源的路勁,而具體的行為由HTTP方法來指定。

三、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用起來很舒服的一個很重要的原因,因為它的侵入性非常非常小,你基本感覺不到它的存在。

四、測試

  代碼寫完了,怎麼測試?除了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等方法了。

五、總結

  本文講解了如果通過Spring boot來實作Restful的API,其實大部分東西都是Spring和Spring MVC提供的,Spring boot隻是提供自動配置的功能。但是,正是這種自動配置,為我們減少了很多的開發和維護工作,使我們能更加簡單、高效的實作一個web工程,進而讓我們能夠更加專注于業務本身的開發,而不需要去關心架構的東西。這篇文章中我們提到了可以通過postman和junit的方式來通路Restful 接口,下篇文章我們會介紹另外一種方式來通路,有興趣的可以繼續關注一下。

 作者:liuxiaopeng

 部落格位址:http://www.cnblogs.com/paddix/

 聲明:轉載請在文章頁面明顯位置給出原文連接配接。