@RequestMapping与@GetMapping和@PostMapping等新注释

栏目: Java · 发布时间: 7年前

内容简介:Spring的复杂性不是来自于它处理的对象,而是来自于自身,不断演进发展的Spring会带来时间维度上复杂性,比如SpringMVC以前版本的从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型,即如果我们想使用传统的

Spring的复杂性不是来自于它处理的对象,而是来自于自身,不断演进发展的Spring会带来时间维度上复杂性,比如SpringMVC以前版本的 @RequestMapping ,到了新版本被下面新注释替代,相当于增加的选项:

  • @GetMapping
  • @PostMapping
  • @PutMapping
  • @DeleteMapping
  • @PatchMapping

从命名约定我们可以看到每个注释都是为了处理各自的传入请求方法类型,即 @GetMapping 用于处理请求方法的 GET 类型, @ PostMapping 用于处理请求方法的 POST 类型等。

如果我们想使用传统的 @RequestMapping 注释实现URL处理程序,那么它应该是这样的:

@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)

新方法可以简化为:

@GetMapping("/get/{id}")

如何工作?

所有上述注释都已在内部注释了 @RequestMapping 以及 方法 元素中的相应值。

例如,如果我们查看 @GetMapping 注释的源代码,我们可以看到它已经通过以下方式使用 RequestMethod.GET 进行了注释:

@Target({ java.lang.annotation.ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = { RequestMethod.GET })
public @interface GetMapping {
   // abstract codes
}

所有其他注释都以相同的方式创建,即 @PostMapping 使用 RequestMethod.POST进行 注释, @ PutMapping 使用 RequestMethod.PUT进行 注释等。

使用方式

下面是结合RestController的简单使用:

@RestController
@RequestMapping("users")
public class UserController {
   @Autowired
   UserService userService;

   @GetMapping("/status/check")
   public String status()
   {
      return "working";
   }

   @GetMapping("/{id}")
   public String getUser(@PathVariable String id)
   {

      return "HTTP Get was called";
   }


   @PostMapping
   public String createUser(@RequestBody UserDetailsRequestModel requestUserDetails)
   {
      return "HTTP POST was called";
   }

   @DeleteMapping("/{userId}")
   public String deleteUser(@PathVariable String userId)
   {


      return "HTTP DELETE was called";
   }

   @PutMapping("/{userId}")
   public String updateUser(@PathVariable String userId, @RequestBody UserDetailsRequestModel requestUserDetails)
   {


      return "HTTP PUT was called";
   }

}

下面是使用@Controller的代码:

@Controller
public class HomeController
{
   @GetMapping("/")
   public String homeInit(Model model) {
      return "home";
   }
}

在上面的代码中,HomeController类充当请求控制器。它的homeInit()方法将处理所有传入的URI请求"/"。它接受a Model并返回视图名称home。使用配置的 视图解析器解析 视图名称”home“的页面。

写法对比

@RequestMapping:

@RequestMapping(value = "/workflow",
      produces = {"application/json"},
      consumes = {"application/json"},
      method = RequestMethod.POST)

@PostMapping如下:

@PostMapping(path = "/members", consumes = "application/json", produces = "application/json")
public void addMember(@RequestBody Member member) {
      //code
      }

Spring Boot


以上所述就是小编给大家介绍的《@RequestMapping与@GetMapping和@PostMapping等新注释》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 2

The Art of Computer Programming, Volume 2

Knuth, Donald E. / Addison-Wesley Professional / 1997-11-04 / USD 79.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 2》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具