SpringBoot—Controller注解
Controller 是 Spring 中最基本的组件,主要是处理跟⽤户交互的,⼀般每个业务逻辑都会有⼀个 Controller,提供给⽤户 http 请求接⼝,⽤户请求接⼝进⾏数据访问。
掌握Controller的⽤法,⼀般掌握下⾯⼏个注解就差不多
了,@Controller,@RestController,@RequestMapping,@PathVariable,@RequestParam,@GetMapping。
下⾯详细说下这些注解的⽤法:
⽬录
1. @Controller
标注 Controller 类,处理 http 请求
@Controller
//@ResponseBody
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
如果直接使⽤@Controller这个注解,当运⾏该SpringBoot项⽬后,在浏览器中输⼊:local:8080/hello,会得到如下错误提⽰:
出现这种情况的原因在于:没有使⽤模版。即@Controller ⽤来响应页⾯,@Controller必须配合模版来使⽤。spring-boot ⽀持多种模版引擎包括:
1,FreeMarker
2,Groovy
3,Thymeleaf (Spring 官⽹使⽤这个)
4,Velocity
5,JSP (貌似Spring Boot官⽅不推荐,STS创建的项⽬会在src/main/resources 下有个templates ⽬录,这⾥就是让我们放模版⽂件的,然后并没有⽣成诸如 SpringMVC 中的webapp⽬录)
本⽂以Thymeleaf为例介绍使⽤模版,具体步骤如下:
第⼀步:在l⽂件中添加如下模块依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第⼆步:修改控制器代码,具体为:
@Controller
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
第三步:在resources⽬录的templates⽬录下添加⼀个hello.html⽂件,具体⼯程⽬录结构如下:
hello.html⽂件中的内容为:
<h1>hello wo jiao zhangkeluo</h1>
再次运⾏此项⽬之后,在浏览器中输⼊:localhost:8080/hello
就可以看到hello.html中所呈现的内容了。
现在都是前后端分离,后端主要负责处理Restful请求,然后返回JSON格式的数据,所有⼀般不会使⽤模版了。
2. @RestController
标注 Controller 类,spring 4 新加注解,相当于@Controller + @ResponseBody ,主要是为了使 http 请求返回数据格式为 json 格式,正常情况下都是使⽤这个注解。
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
与下⾯的代码作⽤⼀样:
@Controller
@ResponseBody
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
3. @ResquestMapping
配置 url 映射,可以作⽤在控制器的某个⽅法上,也可以作⽤在此控制器类上。当控制器在类级别上添加@RequestMapping注解时,这个注解会应⽤到控制器的所有处理器⽅法上。处理器⽅法上的@RequestMapping注解会对类级别上的@RequestMapping的声明进⾏补充。
例⼦⼀:@RequestMapping仅作⽤在处理器⽅法上
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
sayHello所响应的url=localhost:8080/hello。
例⼦⼆:@RequestMapping仅作⽤在类级别上
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
}
sayHello所响应的url=localhost:8080/hello,效果与例⼦⼀⼀样,没有改变任何功能。
例⼦三:@RequestMapping作⽤在类级别和处理器⽅法上
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(value="/sayHello",method= RequestMethod.GET)
public String sayHello(){
return "hello";
}
@RequestMapping(value="/sayHi",method= RequestMethod.GET)
public String sayHi(){
return "hi";
springboot结构}
}
sayHello所响应的url=localhost:8080/hello/sayHello。
sayHi所响应的url=localhost:8080/hello/sayHi
4. @PathVariable
获取 url 中的数据,我们在 url 中拼接⼀个字符串 {username},类似于地址占位符,由⽤户请求时添加,请求获取。注意注解中的参数必须与占位符参数⼀致;
@RestController//处理http请求,返回json格式
@RequestMapping(value = "/users")//配置url,让该类下的所有接⼝url都映射在/users下
public class UserController {
@RequestMapping(value = "/myInfo/{username}", method = RequestMethod.GET)
public String say(@PathVariable("username") String username) {
return username;
}
}
5. @RequestParam
获取请求参数值,⽅法随意可以设置,但是通常需求都是使⽤ POST请求处理表单提交。
@RestController//处理http请求,返回json格式
@RequestMapping(value = "/users")//配置url,让该类下的所有接⼝url都映射在/users下
public class UserController {
@RequestMapping(value = "/myInfo", method = RequestMethod.POST)
public String say(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
return username + password;
}
}
@RestController//处理http请求,返回json格式
@RequestMapping(value = "/users")//配置url,让该类下的所有接⼝url都映射在/users下
public class UserController {
@RequestMapping(value = "/myInfo", method = RequestMethod.POST)
public String say(@RequestParam(value = "username",required = false,defaultValue = "张克落") String username,
@RequestParam(value = "password",required = false,defaultValue = "123456") String password) {
return username + password;
}
}
6. 组合注解
@GetMapping,@PostMapping,@PutMapping等 由请求⽅法+Mapping 的组合注解等价于 @RequestMapping 单独指定映射再指定请求⽅法。这种⽅式简化了我们的使⽤,通常都是直接使⽤组合注解,不需要再⾃⼰指定请求⽅法,毕竟程序猿都是⽐较懒的嘛~
例如:
@RequestMapping(value = "/myInfo", method = RequestMethod.POST)
可以简化为:
@PostMapping(value = "/myInfo")
参考:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。