这里有新鲜出炉的精品教程, 程序狗速度看过来!
Spring Boot
Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务你可通过它来选择不同的 Spring 平台可创建独立的 Java 应用和 web 应用, 同时提供了命令行工具来允许'spring scripts'.
本篇文章主要介绍了 springboot-controller 的使用详解, 小编觉得挺不错的, 现在分享给大家, 也给大家做个参考一起跟随小编过来看看吧
Controller 的使用
一
@Controller: 处理 http 请求
@RestController:Spring4 之后新加的注解, 原来返回 json 需要 @ResponseBody 配合 @Controller
@RequestMapping: 配置 url 映射
1. 对于控制器层, 如果只使用 @Controller 注解, 会报 500, 即 controller 必须配合一个模板来使用:
使用 spring 官方的一个模板:
- <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 相当于 @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 中的数据
@RequestParam: 获取请求参数的值
@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();
- }
- }
结果:
方式二: 也可以把 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();
- }
- }
结果:
方式三: 使用传统方式访问:
- @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; // 方法参数中的 Integer id 这个 id 不需要与前面对应
- // return girlProperties.getCupSize();
- }
- }
结果:
注解简写:@RequestMapping(value = "/say",method = RequestMethod.GET) 等价于:@GetMapping(value = "/say")
- @RestController
- @RequestMapping("/hello")
- public class HelloController {
- @Autowired
- private GirlProperties girlProperties;
- // @RequestMapping(value = "/say",method = RequestMethod.GET)
- //@GetMapping(value = "/say")// 等价于上面的
- @PostMapping(value = "/say")
- public String say(@RequestParam("id") Integer myId){
- return "id:"+myId; // 方法参数中的 Integer id 这个 id 不需要与前面对应
- // return girlProperties.getCupSize();
- }
- }
来源: http://www.phperz.com/article/18/0322/354718.html