springboot-controller的使用詳解
Controller的使用
一、
- @Controller:處理http請(qǐng)求
- @RestController:Spring4之后新加的注解,原來(lái)返回json需要@ResponseBody配合@Controller
- @RequestMapping:配置url映射
1.對(duì)于控制器層,如果只使用@Controller注解,會(huì)報(bào)500,即controller必須配合一個(gè)模板來(lái)使用:
使用spring官方的一個(gè)模板:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在resources下面的templates文件夾下建立index.html:
<h1>hello Spring Boot!</h1>
HelloController:
@Controller @ResponseBody public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ // return girlProperties.getCupSize(); return "index"; } }
@RestController相當(dāng)于@Controller和@ResponseBody組合使用
如果程序需要通過(guò)hello和hi都能訪問(wèn)到,只需在@RequestMapping的value中添加如下:
@RestController public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/hello", "/hi"},method = RequestMethod.GET) public String say(){ return girlProperties.getCupSize(); } }
二、
- @PathVariable:獲取url中的數(shù)據(jù)
- @RequestParam:獲取請(qǐng)求參數(shù)的值
- @GetMapping:組合注解
@PathVariable:
方式一:
@RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/say/{id}"},method = RequestMethod.GET) public String say(@PathVariable("id") Integer id){ return "id:"+id; // return girlProperties.getCupSize(); } }
結(jié)果:
方式二:也可以把id寫(xiě)在前面:
@RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = {"/{id}/say"},method = RequestMethod.GET) public String say(@PathVariable("id") Integer id){ return "id:"+id; // return girlProperties.getCupSize(); } }
結(jié)果:
方式三:使用傳統(tǒng)方式訪問(wèn):
@RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; @RequestMapping(value = "/say",method = RequestMethod.GET) public String say(@RequestParam("id") Integer myId){ return "id:"+myId; //方法參數(shù)中的Integer id這個(gè)id不需要與前面對(duì)應(yīng) // return girlProperties.getCupSize(); } }
結(jié)果:
注解簡(jiǎn)寫(xiě):@RequestMapping(value = "/say",method = RequestMethod.GET)等價(jià)于:@GetMapping(value = "/say")
@RestController @RequestMapping("/hello") public class HelloController { @Autowired private GirlProperties girlProperties; // @RequestMapping(value = "/say",method = RequestMethod.GET) //@GetMapping(value = "/say")//等價(jià)于上面的 @PostMapping(value = "/say") public String say(@RequestParam("id") Integer myId){ return "id:"+myId; //方法參數(shù)中的Integer id這個(gè)id不需要與前面對(duì)應(yīng) // return girlProperties.getCupSize(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Go?Java算法之為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級(jí)實(shí)例
這篇文章主要為大家介紹了Go?Java算法之為運(yùn)算表達(dá)式設(shè)計(jì)優(yōu)先級(jí)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java結(jié)構(gòu)型設(shè)計(jì)模式中建造者模式示例詳解
建造者模式,是一種對(duì)象構(gòu)建模式 它可以將復(fù)雜對(duì)象的建造過(guò)程抽象出來(lái),使這個(gè)抽象過(guò)程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對(duì)象。本文將通過(guò)示例講解建造者模式,需要的可以參考一下2022-09-09Java 運(yùn)算符 動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 運(yùn)算符 動(dòng)力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-04-04spring boot RestTemplate 發(fā)送get請(qǐng)求的踩坑及解決
這篇文章主要介紹了spring boot RestTemplate 發(fā)送get請(qǐng)求的踩坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08spring security4 添加驗(yàn)證碼的示例代碼
本篇文章主要介紹了spring security4 添加驗(yàn)證碼的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Flutter 通過(guò)Clipper實(shí)現(xiàn)各種自定義形狀的示例代碼
這篇文章主要介紹了Flutter 通過(guò)Clipper實(shí)現(xiàn)各種自定義形狀的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12