SpringBoot 中@GetMapping和@PostMapping测试同一方法,为什么用@Get类型的注解可以访问成功,而@PostMapping用浏览器访问失败

@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。

  Difference between @GetMapping & @RequestMapping:

@GetMapping does not support  the consumes attribute of @RequestMapping.

SpringBoot 中@GetMapping和@PostMapping测试同一方法,为什么用@Get类型的注解可以访问成功,而@PostMapping用浏览器访问失败

@Controller
public class JspController {
    @PostMapping("/index")
    public String index(Model model){
        model.addAttribute("msg","springboot访问JSP页面");
        return "index";
    }

}

用@PostMapping注解,在浏览器输入访问地址时,会出现

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Feb 20 09:50:06 CST 2019

There was an unexpected error (type=Method Not Allowed, status=405).

Request method 'GET' not supported

而用@GetMapping注解,可以访问成功

通过浏览器的地址栏输入地址,所访问的URL都是get请求,因此如果以post定义方法,那么由于请求与实现的不一致,会返回405错误。

浏览器发送Get请求有

1.直接在地址栏中输入地址

2.点击链接

3.表单默认提交方式

浏览器发送post请求有:

1.将表单的method设置为post

你可能感兴趣的:(SpringBoot学习笔记)