小編給大家分享一下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è)計(jì)、河?xùn)|網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、河?xùn)|企業(yè)網(wǎng)站模板建站服務(wù),十年河?xùn)|做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
1)SpringBoot繼承了Spring優(yōu)秀的基因,上手難度小
2)簡化配置,提供各種默認(rèn)配置來簡化項(xiàng)目配置
3)內(nèi)嵌式容器簡化Web項(xiàng)目,簡化編碼
Spring Boot 則會(huì)幫助開發(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ù)是未來發(fā)展的趨勢(shì),項(xiàng)目會(huì)從傳統(tǒng)架構(gòu)慢慢轉(zhuǎn)向微服務(wù)架構(gòu),因?yàn)槲⒎?wù)可以使不同的團(tuán)隊(duì)專注于更小范圍的工作職責(zé)、使用獨(dú)立的技術(shù)、更安全更頻繁地部署。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
application.yml
# 端口 server: port: 8001
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) ;
}
}絲毫沒有問題,就這樣吧啟動(dòng)上面這個(gè)類,springboot的基礎(chǔ)環(huán)境就搭建好了。
想想之前的Spring框架的環(huán)境搭建,是不是就是這個(gè)感覺:意會(huì)一下吧。
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ù)。
1)首先看看SpringBoot 如何區(qū)分環(huán)境
這里標(biāo)識(shí)配置加載指定的配置文件。
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()+"]" ;
}
}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(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();
}
/**
* 測(cè)試保存接口
*/
@Test
public 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è)試查詢接口
*/
@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)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:SpringBoot2.0中環(huán)境搭建和RestFul風(fēng)格接口的示例分析
網(wǎng)站網(wǎng)址:http://chinadenli.net/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è)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)