ArticleServiceImpl這個類是一個很普通的類,只有一個Spring的注解@Service,標(biāo)識為一個bean以便于通過Spring IoC容器來管理。我們再來看看ArticleController這個類,其實用過Spring MVC的人應(yīng)該都熟悉這幾個注解,這里簡單解釋一下:

通過這個三個注解,我們就能輕松的實現(xiàn)通過URL給前端返回JSON格式數(shù)據(jù)的功能。不過大家肯定有點疑惑,這不都是Spring MVC的東西嗎?跟Spring boot有什么關(guān)系?其實Spring boot的作用就是為我們省去了配置的過程,其他功能確實都是Spring與Spring MVC來為我們提供的,大家應(yīng)該記得Spring boot通過各種starter來為我們提供自動配置的服務(wù),我們的工程里面之前引入過這個依賴:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>

這個是所有Spring boot的web工程都需要引入的jar包,也就是說只要是Spring boot的web的工程,都默認(rèn)支持上述的功能。這里我們進(jìn)一步發(fā)現(xiàn),通過Spring boot來開發(fā)web工程,確實為我們省了許多配置的工作。

二、Restful API設(shè)計

好了,我們現(xiàn)在再來看看如何實現(xiàn)Restful API。實際上Restful本身不是一項什么高深的技術(shù),而只是一種編程風(fēng)格,或者說是一種設(shè)計風(fēng)格。在傳統(tǒng)的http接口設(shè)計中,我們一般只使用了get和post兩個方法,然后用我們自己定義的詞匯來表示不同的操作,比如上面查詢文章的接口,我們定義了article/list.json來表示查詢文章列表,可以通過get或者post方法來訪問。而Restful API的設(shè)計則通過HTTP的方法來表示CRUD相關(guān)的操作。因此,除了get和post方法外,還會用到其他的HTTP方法,如PUT、DELETE、HEAD等,通過不同的HTTP方法來表示不同含義的操作。下面是我設(shè)計的一組對文章的增刪改查的Restful API:

這里可以看出,URL僅僅是標(biāo)識資源的路勁,而具體的行為由HTTP方法來指定。

三、Restful API實現(xiàn)

現(xiàn)在我們再來看看如何實現(xiàn)上面的接口,其他就不多說,直接看代碼:

@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;
    }

}

我們再來分析一下這段代碼,這段代碼和之前代碼的區(qū)別在于:

所以看來看去,這個代碼還是跟Spring boot沒太多的關(guān)系,Spring boot也僅僅是提供自動配置的功能,這也是Spring boot用起來很舒服的一個很重要的原因,因為它的侵入性非常非常小,你基本感覺不到它的存在。

四、測試

代碼寫完了,怎么測試?除了GET的方法外,都不能直接通過瀏覽器來訪問,當(dāng)然,我們可以直接通過postman來發(fā)送各種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();
    }

}

執(zhí)行結(jié)果這里就不給大家貼了,大家有興趣的話可以自己實驗一下。整個類要說明的點還是很少,主要這些東西都與Spring boot沒關(guān)系,支持這些操作的原因還是上一篇文章中提到的引入對應(yīng)的starter:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
</dependency>

因為要執(zhí)行HTTP請求,所以這里使用了MockMvc,ArticleRestController通過注入的方式實例化,不能直接new,否則ArticleRestController就不能通過Spring IoC容器來管理,因而其依賴的其他類也無法正常注入。通過MockMvc我們就可以輕松的實現(xiàn)HTTP的DELETE/PUT/POST等方法了。

五、總結(jié)

本文講解了如果通過Spring boot來實現(xiàn)Restful的API,其實大部分東西都是Spring和Spring MVC提供的,Spring boot只是提供自動配置的功能。但是,正是這種自動配置,為我們減少了很多的開發(fā)和維護(hù)工作,使我們能更加簡單、高效的實現(xiàn)一個web工程,從而讓我們能夠更加專注于業(yè)務(wù)本身的開發(fā),而不需要去關(guān)心框架的東西。

文章轉(zhuǎn)自微信公眾號@Java技術(shù)棧

上一篇:

根據(jù)Swagger OpenAPI規(guī)范生成TypeScript類型

下一篇:

強力監(jiān)控!Spring Boot 3.3 集成 Zipkin 全面追蹤 RESTful API 性能
#你可能也喜歡這些API文章!

我們有何不同?

API服務(wù)商零注冊

多API并行試用

數(shù)據(jù)驅(qū)動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準(zhǔn)確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費