SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析

小編給大家分享一下SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供河?xùn)|網(wǎng)站建設(shè)、河?xùn)|做網(wǎng)站、河?xùn)|網(wǎng)站設(shè)計、河?xùn)|網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、河?xùn)|企業(yè)網(wǎng)站模板建站服務(wù),十年河?xùn)|做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、SpringBoot 框架的特點(diǎn)

1、SpringBoot2.0 特點(diǎn)

1)SpringBoot繼承了Spring優(yōu)秀的基因,上手難度小
2)簡化配置,提供各種默認(rèn)配置來簡化項(xiàng)目配置
3)內(nèi)嵌式容器簡化Web項(xiàng)目,簡化編碼
Spring Boot 則會幫助開發(fā)著快速啟動一個 web 容器,在 Spring Boot 中,只需要在 pom 文件中添加如下一個 starter-web 依賴即可.

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

4)發(fā)展趨勢看
微服務(wù)是未來發(fā)展的趨勢,項(xiàng)目會從傳統(tǒng)架構(gòu)慢慢轉(zhuǎn)向微服務(wù)架構(gòu),因?yàn)槲⒎?wù)可以使不同的團(tuán)隊(duì)專注于更小范圍的工作職責(zé)、使用獨(dú)立的技術(shù)、更安全更頻繁地部署。

二、搭建SpringBoot的環(huán)境

1、創(chuàng)建一個Maven項(xiàng)目

SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析

2、引入核心依賴

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

3、編寫配置文件

application.yml

# 端口
server:
  port: 8001

4、啟動文件注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class,args) ;
    }
}

絲毫沒有問題,就這樣吧啟動上面這個類,springboot的基礎(chǔ)環(huán)境就搭建好了。
想想之前的Spring框架的環(huán)境搭建,是不是就是這個感覺:意會一下吧。

三、SpringBoot2.0 幾個入門案例

1、創(chuàng)建一個Web接口

import com.boot.hello.entity.ProjectInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * SpringBoot 2.0 第一個程序
 */
@RestController
public class HelloController {
    @RequestMapping("/getInfo")
    public ProjectInfo getInfo (){
        ProjectInfo info = new ProjectInfo() ;
        info.setTitle("SpringBoot 2.0 基礎(chǔ)教程");
        info.setDate("2019-06-05");
        info.setAuthor("知了一笑");
        return info ;
    }
}

@RestController 注解 等價 @Controller + @ResponseBody 返回Json格式數(shù)據(jù)。

2、參數(shù)映射

1)首先看看SpringBoot 如何區(qū)分環(huán)境
SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析

這里標(biāo)識配置加載指定的配置文件。

2)參數(shù)配置
application-pro.yml

user:
  author: 知了一笑
  title: SpringBoot 2.0 程序開發(fā)
  time: 2019-07-05

3)參數(shù)內(nèi)容讀取

@Component
public class ParamConfig {
    @Value("${user.author}")
    private String author ;
    @Value("${user.title}")
    private String title ;
    @Value("${user.time}")
    private String time ;
    // 省略 get 和 set 方法
}

4)調(diào)用方式

/**
 * 環(huán)境配置,參數(shù)綁定
 */
@RestController
public class ParamController {

    @Resource
    private ParamConfig paramConfig ;

    @RequestMapping("/getParam")
    public String getParam (){
        return "["+paramConfig.getAuthor()+";"+
                paramConfig.getTitle()+";"+
                paramConfig.getTime()+"]" ;
    }
}

3、RestFul 風(fēng)格接口和測試

1)Rest風(fēng)格接口

/**
 * Rest 風(fēng)格接口測試
 */
@RestController // 等價 @Controller + @ResponseBody 返回Json格式數(shù)據(jù)
@RequestMapping("rest")
public class RestApiController {
    private static final Logger LOG = LoggerFactory.getLogger(RestApiController.class) ;
    /**
     * 保存
     */
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insert (UserInfo userInfo){
        LOG.info("===>>"+userInfo);
        return "success" ;
    }
    /**
     * 查詢
     */
    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    public String select (@PathVariable Integer id){
        LOG.info("===>>"+id);
        return "success" ;
    }
}

2)測試代碼

@RunWith(SpringJUnit4Cla***unner.class)
@SpringBootTest(classes = MockServletContext.class)
@WebAppConfiguration
public class TestRestApi {

    private MockMvc mvc;
    @Before
    public void setUp() throws Exception {
        mvc = MockMvcBuilders.standaloneSetup(new RestApiController()).build();
    }

    /**
     * 測試保存接口
     */
    @Test
    public void testInsert () throws Exception {
        RequestBuilder request = null;
        request = post("/rest/insert/")
                .param("id", "1")
                .param("name", "測試大師")
                .param("age", "20");
        mvc.perform(request)
                .andExpect(content().string(equalTo("success")));
    }

    /**
     * 測試查詢接口
     */
    @Test
    public void testSelect () throws Exception {
        RequestBuilder request = null;
        request = get("/rest/select/1");
        mvc.perform(request)
                .andExpect(content().string(equalTo("success")));
    }
}

以上是“SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析
網(wǎng)站網(wǎng)址:http://bm7419.com/article20/goepco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、ChatGPT、服務(wù)器托管、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管