SpringBoot入门(三)Controller的使用
2017-08-11 11:54
585 查看
浅析
@Controller:处理http请求@RestController:Spring4之后新加的注解,用于代替原来@ResponseBody +@Controller的组合
@RequestMapping:配置url映射(用户通过访问某个url访问到我们某个方法)
@Controller
原来的代码@RestController public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge(); } }
使用@Controller(仅为了解。因为后面一般还是使用@RestController)
1、pom.xml中添加spring官方模板
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、在src/main/resources/template文件夹下创建index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"></meta> <title>Insert title here</title> </head> <body> Hello Controller </body> </html>
3、修改@RestController为@Controller,修改返回值为index
@Controller public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return "name:"+mPersonProperties.getName()+"age:"+mPersonProperties.getAge(); } }
4、运行结果
@RestController=@Controller+@ResponseBody组合
@Controller @ResponseBody public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value="/helloctrl", method=RequestMethod.GET) public String say() { return mPersonProperties.getName(); } }
@RequstMapping配置url
访问路径可以是
http://localhost:8088/controller/helloctrl
或者
http://localhost:8088/controller/hi
如何处理url中的参数
@PathVariable:获取url中的数据@RequestParam:获取请求参数的值
@GetMapping:组合注解(简化@RequestMapping(value= {“/helloctrl”}, method=RequestMethod.GET)=@GetMapping(value= {“/helloctrl”}))
@PathVariable
@RestController @RequestMapping("/controller") public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value= {"/helloctrl/{id}"}, method=RequestMethod.GET) public String say(@PathVariable("id") Integer id) { return "id"+id; } }
还可以如下写:
@RequestMapping(value= {"/{id}/helloctrl"}, method=RequestMethod.GET)
访问地址
http://localhost:8088/controller/100/helloctrl
设置默认值
@RestController @RequestMapping("/controller") public class HelloController { @Autowired private PersonProperties mPersonProperties; @RequestMapping(value= {"/helloctrl"}, method=RequestMethod.GET) //required是否一定要传递值,设置为false,不需要 public String say(@RequestParam(value="id",required=false,defaultValue="10") Integer myid) { return "id"+myid; } }
@GetMapping
@RestController @RequestMapping("/controller") public class HelloController { @Autowired private PersonProperties mPersonProperties; @GetMapping(value="/helloctrl") public String say(@RequestParam(value="id",required=false,defaultValue="20") Integer myid) { return "id"+myid; } }
与@GetMapping相对应的还有@PostMapping、@PutMapping、@DeleteMapping
相关文章推荐
- springboot快速入门(三)——Controller的使用
- Spring Boot 官方文档学习(一)入门及使用
- Spring Boot入门——JDBCTemplate使用及其相关问题解决
- SpringBoot入门学习一(如何使用IDEA创建一个SpringBoot应用)
- Spring Boot入门一:在Eclipse中使用Spring boot
- spring boot入门之使用JdbcTemplate
- spring boot 入门-使用spring boot构建简单应用
- springboot中使用esper入门
- Spring Boot 官方文档学习(一)入门及使用
- Spring Boot使用mybatis 入门
- SpringBoot之Controller的使用详解
- Spring Boot入门级使用教程
- 从入门到入门-Spring Boot-Controller
- Spring Boot入门二:使用ThymeLeaf+表单验证
- Spring Boot 官方文档学习(一)入门及使用
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- 六、Spring Boot Controller使用
- Spring Boot 官方文档入门及使用
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- Spring Boot 学习(一)入门及使用