springboot-controller的使用詳解
Controller的使用
一、
- @Controller:處理http請(qǐng)求
- @RestController:Spring4之后新加的注解,原來返回json需要@ResponseBody配合@Controller
- @RequestMapping:配置url映射
1.對(duì)于控制器層,如果只使用@Controller注解,會(huì)報(bào)500,即controller必須配合一個(gè)模板來使用:
使用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組合使用
如果程序需要通過hello和hi都能訪問到,只需在@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寫在前面:
@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)方式訪問:
@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)寫:@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)文章
關(guān)于Lombok @Data注解:簡(jiǎn)化Java代碼的魔法棒
Lombok庫通過@Data注解自動(dòng)生成常見的樣板代碼如getter、setter、toString等,極大減少代碼量,提高開發(fā)效率,@Data注解集成了@ToString、@EqualsAndHashCode、@Getter、@Setter、@RequiredArgsConstructor等注解的功能2024-10-10
LambdaQueryWrapper的實(shí)現(xiàn)原理分析和lambda的序列化問題
這篇文章主要介紹了LambdaQueryWrapper的實(shí)現(xiàn)原理分析和lambda的序列化問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
淺談SpringBoot中的Bean初始化方法?@PostConstruct
這篇文章主要介紹了SpringBoot中的Bean初始化方法?@PostConstruct,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例
這篇文章主要介紹了SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Spring中的@EnableWebSecurity注解詳解
這篇文章主要介紹了Spring中的@EnableWebSecurity注解詳解,EnableWebSecurity注解是個(gè)組合注解,它的注解中,又使用了@EnableGlobalAuthentication注解,需要的朋友可以參考下2023-12-12
Spring init-method與destroy-method屬性的用法解析
這篇文章主要介紹了Spring init-method與destroy-method屬性的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot的ConfigurationProperties或Value注解無效問題及解決
在SpringBoot項(xiàng)目開發(fā)中,全局靜態(tài)配置類讀取application.yml或application.properties文件時(shí),可能會(huì)遇到配置值始終為null的問題,這通常是因?yàn)樵趧?chuàng)建靜態(tài)屬性后,IDE自動(dòng)生成的Get/Set方法包含了static關(guān)鍵字2024-11-11

