1. @Controller

作用 : 标注 Controller 类,处理 http 请求。

注意 : @Controller必须配合模版来使用。

代码 :

package com.hui.apidemo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class IndexController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "Hello Word";
}
}

输出 :
如果直接使用@Controller这个注解,当运行该SpringBoot项目后,在浏览器中输入:local:8080/index,会得到如下错误提示:

image-20200901135743988

出现这种情况的原因在于:没有使用模版。即@Controller 用来响应页面,@Controller必须配合模版来使用。

2. @RestController

作用 : 标注 Controller 类,spring 4 新加注解,相当于@Controller + @ResponseBody ,主要是为了使 http 请求返回数据格式为json格式

注意 : 正常情况下都是使用这个注解。

代码 :

package com.hui.apidemo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "Hello Word";
}
}

输出 :
image-20200901140057799

3. @ResquestMapping

作用 : 配置 url 映射,可以作用在控制器的某个方法上,也可以作用在此控制器类上。

注意 :

  • 在类级别上添加@RequestMapping注解时,这个注解会应用到控制器的所有处理器方法上。
  • 在方法上添加@RequestMapping注解时,会对类级别上的@RequestMapping的声明进行补充。

代码 :

  • 仅作用在类上时

    package com.hui.apidemo.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    // 只作用在类上
    @RestController
    @RequestMapping("/hello")
    public class IndexController {
    @RequestMapping(method = RequestMethod.GET)
    public String hello() {
    return "Hello Word";
    }
    }

    访问: http://127.0.0.1:8080/hello

  • 仅作用在方法上

    package com.hui.apidemo.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    // 只作用在方法上
    @RestController
    public class DemoController {
    @RequestMapping(value = "/demo",method = RequestMethod.GET)
    public String hello(){
    return "Hello Word -1";
    }
    }

    访问: http://127.0.0.1:8080/demo

  • 同时作用在类和方法上

    package com.hui.apidemo.controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    // 同时作用在方法上和类上
    @RestController
    @RequestMapping("/test")
    public class TestController {
    // 访问: /test/say
    @RequestMapping(value = "say",method = RequestMethod.GET)
    public String sayHello() {
    return "say Hello";
    }
    // 访问: /test/bye
    @RequestMapping(value = "bye",method = RequestMethod.GET)
    public String sayBye() {
    return "say Bye";
    }
    }

    访问:

4. @PathVariable

作用 : 获取 url 中的数据。

注意 : 注解中的参数必须与占位符参数一致

代码 :

package com.hui.apidemo.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")
public class IndexController {
// 获取url(http://127.0.0.1:8080/index/hello/小明) 中的小明
@RequestMapping(value = "/hello/{msg}" ,method = RequestMethod.GET)
public String hello(@PathVariable("msg") String msg) {
return "Hello " + msg;
}
}

输出 :
image-20200901142604624

5. @RequestParam

作用 : 获取请求参数值,方法随意可以设置。

注意 :

  • 通常要求使用POST请求处理表单提交。
  • @RequestParam属性required默认都是必填字段

代码 :

package com.hui.apidemo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/index")
public class IndexController {
@RequestMapping(value = "/hello/{msg}", method = RequestMethod.GET)
public String hello(@PathVariable("msg") String msg) {
return "Hello " + msg;
}

/**
* 测试@RequestParam使用
*/
@RequestMapping(value = "/param", method = RequestMethod.POST)
public String getParam(
@RequestParam(value = "phone") String phone,
@RequestParam(value = "name") String name,
@RequestParam(value = "home", required = false, defaultValue = "未填写") String home
)
{
String str = "param => phone:" + phone + "name:" + name + "home: " + home;
return str;
}
}

输出:

image-20200901150606850

6. 组合注解

作用 : 由请求方法+Mapping的组合注解(@GetMapping@PostMapping@PutMapping…)等价于 @RequestMapping 单独指定映射再指定请求方法。

注意 :

代码 :

package com.hui.apidemo.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/index")
public class IndexController {
// @RequestMapping(value = "/get", method = RequestMethod.GET) 简写
@GetMapping(value = "/get")
public String getSomething(){
return "getSomething";
}
// @RequestMapping(value = "/post", method = RequestMethod.POST) 简写
@PostMapping(value = "/post")
public String postSomething(){
return "postSomething";
}
}