Spring Boot 学习——Controller的使用
2017-08-28 00:04
435 查看
本文主要记录几个注释的使用方法。
1. @Controller : 处理http请求
2. @RequestMapping : 配置URL映射
3. @RestController : 组合注解,spring 4之后新加的注解,相当于@Controller和@ResponseBody配合使用
4. @PathVariable : 获取URL中的数据
5. @RequestParam : 获取请求的参数的值
6. @GetMapping/@PostMapping : 组合注解
使用方法:
1. @Controller
需要与模板配合使用。
pom.xml 中添加模板依赖,代码如下:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在resources下创建文件夹templates,在templates下创建index.html,index.html内容随便写。
编辑Java类,代码如下:@Controller
public class HelloController {
@RequestMapping(value="/say", method=RequestMethod.GET)
public String sayHello(@PathVariable("id") int myId){
return "index";
}
}
在浏览器中访问http://127.0.0.1:8080/say即可看到index.html页面内容
2. @RequestMapping
上面代码已经使用了该注解。
3. @RestController
组合注解,相当于@Controller和@ResponseBody配合使用
4. @PathVariable
获取URL中的数据,代码如下:
@RestController
public class HelloController {
@RequestMapping(value="/say/{id}",method=RequestMethod.GET)
public String sayHello(@PathVariable("id") int myId){
return "myId is " + myId;
}
}
在浏览器中访问http://127.0.0.1:8080/say/111即可查看结果
5. @RequestParam
获取请求的参数的值,代码如下:
@RestController
public class HelloController {
@RequestMapping(value ="/say", method=RequestMethod.GET)
public String sayHello( @RequestParam("id") int myId){
return "myId is " + myId;
}
}
在浏览器中访问http://127.0.0.1:8080/say?id=111即可查看结果
也可以设置参数的默认值,代码如下:
@RestController
public class HelloController {
@RequestMapping(value ="/say", method=RequestMethod.GET)
public String sayHello( @RequestParam( value = "id", required = false, defaultValue = "0") int myId){
return "myId is " + myId;
}
}
这样可以不传参数id。在浏览器中访问http://127.0.0.1:8080/say即可查看结果
6. @GetMapping/@PostMapping
组合注解
@GetMapping(value = "/say") 相当于 @RequestMapping(value = "/say", method = RequestMethod.GET)
@PostMapping(value = "/say") 相当于 @RequestMapping(value = "/say", method = RequestMethod.POST)
除此之外,还有组合注解@PutMapping、@DeleteMapping等等。
1. @Controller : 处理http请求
2. @RequestMapping : 配置URL映射
3. @RestController : 组合注解,spring 4之后新加的注解,相当于@Controller和@ResponseBody配合使用
4. @PathVariable : 获取URL中的数据
5. @RequestParam : 获取请求的参数的值
6. @GetMapping/@PostMapping : 组合注解
使用方法:
1. @Controller
需要与模板配合使用。
pom.xml 中添加模板依赖,代码如下:<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在resources下创建文件夹templates,在templates下创建index.html,index.html内容随便写。
编辑Java类,代码如下:@Controller
public class HelloController {
@RequestMapping(value="/say", method=RequestMethod.GET)
public String sayHello(@PathVariable("id") int myId){
return "index";
}
}
在浏览器中访问http://127.0.0.1:8080/say即可看到index.html页面内容
2. @RequestMapping
上面代码已经使用了该注解。
3. @RestController
组合注解,相当于@Controller和@ResponseBody配合使用
4. @PathVariable
获取URL中的数据,代码如下:
@RestController
public class HelloController {
@RequestMapping(value="/say/{id}",method=RequestMethod.GET)
public String sayHello(@PathVariable("id") int myId){
return "myId is " + myId;
}
}
在浏览器中访问http://127.0.0.1:8080/say/111即可查看结果
5. @RequestParam
获取请求的参数的值,代码如下:
@RestController
public class HelloController {
@RequestMapping(value ="/say", method=RequestMethod.GET)
public String sayHello( @RequestParam("id") int myId){
return "myId is " + myId;
}
}
在浏览器中访问http://127.0.0.1:8080/say?id=111即可查看结果
也可以设置参数的默认值,代码如下:
@RestController
public class HelloController {
@RequestMapping(value ="/say", method=RequestMethod.GET)
public String sayHello( @RequestParam( value = "id", required = false, defaultValue = "0") int myId){
return "myId is " + myId;
}
}
这样可以不传参数id。在浏览器中访问http://127.0.0.1:8080/say即可查看结果
6. @GetMapping/@PostMapping
组合注解
@GetMapping(value = "/say") 相当于 @RequestMapping(value = "/say", method = RequestMethod.GET)
@PostMapping(value = "/say") 相当于 @RequestMapping(value = "/say", method = RequestMethod.POST)
除此之外,还有组合注解@PutMapping、@DeleteMapping等等。
相关文章推荐
- Spring Boot学习总结(4)——使用Springloaded进行热部署
- Spring Boot学习笔记-SQL数据库使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot学习总结(4)——使用Springloaded进行热部署
- REDIS学习(3.2)spring boot 使用redis作为缓存
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring4.0MVC学习资料,Controller中的方法详解和使用(四)
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot 官方文档学习(一)入门及使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot 官方文档学习(一)入门及使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa