springboot注解及GET、POST接口寫法
一、注解
springboot提供了@Contrller和@RestController。
@Controller:返回頁(yè)面和數(shù)據(jù)
@RestController:返回?cái)?shù)據(jù)
@RestMapping注解:主要做路徑映射url
value:請(qǐng)求URL的路徑。
method:HTTP請(qǐng)求方法。
@RestMapping(value="user", method= RequestMethod.GET)
1.1 GET
無(wú)參數(shù)
@RequestMapping (value="/hello", method= RequestMethod.GET) public String hello(String name){ return "123"+name; }
參數(shù)傳遞
@RequestMapping (value="/hello", method= RequestMethod.GET) public String hello(String name){ return "123"+name; }
參數(shù)映射
@RequestParam注解代表參數(shù)映射,將傳入進(jìn)來(lái)的nickname映射到name
@RequestMapping (value="/hello2", method= RequestMethod.GET) public String hello2(@RequestParam(value ="nickname",required = false) String name){ return "123"+name; }
1.2 POST
無(wú)參數(shù)
@RequestMapping(value = "/post1", method = RequestMethod.POST) public String post1(){ return "hello post"; }
帶參數(shù)
@RequestMapping(value = "/post2", method = RequestMethod.POST) public String post2(String username, String password){ return username+"-"+password; }
Bean封裝
@RequestMapping(value = "/post3",method = RequestMethod.POST) public String post3(User user){ System.out.println(user); return "post"; }
json
要在參數(shù)前面加一個(gè)注解@RequestBody,傳入進(jìn)來(lái)的參數(shù)名和類的私有變量要保持一致
@RequestMapping(value = "/post34",method = RequestMethod.POST) public String post4(@RequestBody User user){ System.out.println(user); return "post"; }
1.3錯(cuò)誤
- 404 :路勁不對(duì)
- 405:方法不被允許
到此這篇關(guān)于springboot注解及GET、POST接口寫法的文章就介紹到這了,更多相關(guān)springboot get post接口寫法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何解決Spring in action @valid驗(yàn)證不生效的問(wèn)題
這篇文章主要介紹了如何解決Spring in action @valid驗(yàn)證不生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Mybatis中輸入輸出映射與動(dòng)態(tài)Sql圖文詳解
這篇文章主要給大家介紹了關(guān)于Mybatis中輸入輸出映射與動(dòng)態(tài)Sql的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02SpringBoot通過(guò)yml和xml文件配置日志輸出方法
這篇文章主要介紹了SpringBoot通過(guò)yml和xml文件配置日志輸出方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04java學(xué)習(xí)筆記之eclipse+tomcat 配置
俗話說(shuō):工欲善其事必先利其器,既然要學(xué)習(xí)java,首先把java的開發(fā)環(huán)境搗鼓一下吧,這里我們來(lái)談?wù)別clipse+tomcat的配置方法。2014-11-11springMvc注解之@ResponseBody和@RequestBody詳解
本篇文章主要介紹了springMvc注解之@ResponseBody和@RequestBody詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05java全角、半角字符的關(guān)系以及轉(zhuǎn)換詳解
這篇文章主要介紹了2013-11-11使用注解進(jìn)行Spring開發(fā)的全過(guò)程
使用注解(Annotation)是一種在代碼級(jí)別進(jìn)行說(shuō)明和標(biāo)記的技術(shù),它從JDK 5.0開始引入,并在現(xiàn)代Java開發(fā)中得到了廣泛應(yīng)用,本文將詳細(xì)介紹Spring框架中常用的注解及示例,幫助開發(fā)者快速掌握Spring注解開發(fā)的要點(diǎn)和技巧,需要的朋友可以參考下2023-11-11SpringBoot實(shí)現(xiàn)埋點(diǎn)監(jiān)控
本文主要介紹了SpringBoot實(shí)現(xiàn)埋點(diǎn)監(jiān)控,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01