日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

發(fā)布時(shí)間:2025/3/17 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

SpringBoot2.0 特點(diǎn)

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

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

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

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

1、創(chuàng)建一個(gè)簡(jiǎn)單的Maven項(xiàng)目

2、引入核心依賴

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

3、編寫配置文件

application.yml

# 端口 server:port: 8001

4、啟動(dòng)文件注解

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

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

三、SpringBoot2.0 幾個(gè)入門案例

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

import com.boot.hello.entity.ProjectInfo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /*** SpringBoot 2.0 第一個(gè)程序*/ @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 注解 等價(jià) @Controller + @ResponseBody 返回Json格式數(shù)據(jù)。

2、參數(shù)映射

1)首先看看SpringBoot 如何區(qū)分環(huán)境

這里標(biāo)識(shí)配置加載指定的配置文件。
2)參數(shù)配置
application-pro.yml

user:author: 知了一笑title: SpringBoot 2.0 程序開(kāi)發(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 {@Resourceprivate ParamConfig paramConfig ;@RequestMapping("/getParam")public String getParam (){return "["+paramConfig.getAuthor()+";"+paramConfig.getTitle()+";"+paramConfig.getTime()+"]" ;} }

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

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

/*** Rest 風(fēng)格接口測(cè)試*/ @RestController // 等價(jià) @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)測(cè)試代碼

@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = MockServletContext.class) @WebAppConfiguration public class TestRestApi {private MockMvc mvc;@Beforepublic void setUp() throws Exception {mvc = MockMvcBuilders.standaloneSetup(new RestApiController()).build();}/*** 測(cè)試保存接口*/@Testpublic void testInsert () throws Exception {RequestBuilder request = null;request = post("/rest/insert/").param("id", "1").param("name", "測(cè)試大師").param("age", "20");mvc.perform(request).andExpect(content().string(equalTo("success")));}/*** 測(cè)試查詢接口*/@Testpublic void testSelect () throws Exception {RequestBuilder request = null;request = get("/rest/select/1");mvc.perform(request).andExpect(content().string(equalTo("success")));} }

這樣SpringBoot2.0的入門案例就結(jié)束了,簡(jiǎn)單,優(yōu)雅,有格調(diào)。

四、源代碼

GitHub:知了一笑 https://github.com/cicadasmile/spring-boot-base


總結(jié)

以上是生活随笔為你收集整理的SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。