Spring Boot—Controller 注解

Controller 是 Spring 中最基本的组件,主要是处理跟用户交互的,一般每个业务逻辑都会有一个 Controller,提供给用户 http 请求接口,用户请求接口进行数据访问。

掌握Controller的用法,一般掌握下面几个注解就差不多了,@Controller@RestController@RequestMapping@PathVariable@RequestParam@GetMapping。

下面详细说下这些注解的用法:

目录

1. @Controller

2. @RestController

3. @ResquestMapping

4. @PathVariable

5. @RequestParam

6. 组合注解


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为例介绍使用模版,具体步骤如下:

第一步:在pom.xml文件中添加如下模块依赖:

<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";
    }
}

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;
    }
}

此时接口 url 为:http://127.0.0.1:8080/users/myInfo/张克落,请求方法 GET 结果:浏览器上显示 张克落

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;
    }
}

此时 接口 url 为:http://127.0.0.1:8080/users/myInfo?username=张克落&password=123456 请求方法:POST注意拼接地址参数必须与注解参数一致
使用 postman 来测试接口,结果为,输出 张克落123456
这时候假如用户没有传参数,就会报404错误。再添加两个设置 required = false:是否必穿。defaultValue:默认值

@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")

参考:

https://www.jianshu.com/p/ea512f634fd6

https://blog.csdn.net/u010412719/article/details/69788227

https://blog.csdn.net/u010412719/article/details/69710480

猜你喜欢

转载自blog.csdn.net/zhangpower1993/article/details/81878773