javascript
SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口
一、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 依賴即可.
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: 80014、啟動(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
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)題。
- 上一篇: ubuntu8.04安装java+ecl
- 下一篇: SpringBoot2.0 整合 Shi