@RequestMapping、@PostMapping、@GetMapping区别

介绍

@RequestMapping、@PostMapping、@GetMapping均为映射请求路径,可作用于类或方法上。

@RequestMapping、@PostMapping、@GetMapping区别

@RequestMapping(value = "xxx", method = RequestMehod.POST)等价于@PostMapping("xxx")

@RequestMapping(value = "xxx", method = RequestMehod.GET)等价于@GetMapping("xxx")

以下给出他们@RequestMapping和 @GetMapping等价的例子

@RequestMapping例子

@RequestMapping当作用于类时,是该类中所有响应请求方法地址的父路径。

@RequestMapping当作用于方法时,是该接口响应请求方法地址的子路径,method里可以填写【RequestMethod.GET/RequestMethod.POST/RequestMethod.DELETE】。

@RequestMapping("/father") //test即父路径
public class Demo{

    @RequestMapping(value = "/son", method = RequestMethod.GET)
    @ResponseBody // 将返回的java对象转为json格式的数据
    public String hello() {
        return "hello world !";
    }
}

访问路径:http://localhost:8080/father/son (切记不要忘记“father”哦!)

 @GetMapping例子

@RequestMapping当作用于类时,是该类中所有响应请求方法地址的父路径。

@GetMapping当作用于方法时,是该接口响应请求方法地址的子路径。

@RequestMapping("/father") //test即父路径
public class Demo{

    @GetMapping("/son")
    @ResponseBody // 将返回的java对象转为json格式的数据
    public String hello() {
        return "hello world !";
    }
}

访问路径:http://localhost:8080/father/son (切记不要忘记“father”哦!)

你可能感兴趣的:(基础,java,开发语言)