springboot中get post put delete 请求

匿名 (未验证) 提交于 2019-12-03 00:19:01

组合注解(RequestMapping的变形)

  • @GetMapping = @RequestMapping(method = RequestMethod.GET)
  • @PostMapping = @RequestMapping(method = RequestMethod.POST)
  • @PutMapping = @RequestMapping(method = RequestMethod.PUT)
  • @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE

1、原始springMVC的请求

package com.example.springdemo.hello; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class OrginController {     /**      * 原始的写法      * @return      */     @RequestMapping(value = "/orginController",method = RequestMethod.GET)     @ResponseBody     public String orginController(){         return "orginController";     } } 

如图:


2、使用SpringBoot的请求

package com.example.springdemo.hello;  import org.springframework.web.bind.annotation.*;  import java.util.HashMap; import java.util.Map;  @RestController public class RequestDemoController {      /**      * RestController相当于Controller+repsoneBody      */     /**      * 通过@PathVariable接收参数      */     @GetMapping(value="/test1/{id}/{name}")     public Map<String, Object> getMap(@PathVariable("id") String id,@PathVariable("name") String name) {         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",id);         map.put("name",name);         return map;     }      /**      * get请求的用法      * @param id      * @param name      * @return      */     @RequestMapping(value = "/gettest1",method = RequestMethod.GET)     public Map<String,Object> getController(@RequestParam(value="id") String id,@RequestParam(value="name") String name){         Map<String,Object> map = new HashMap<String, Object>();       map.put("id",id);       map.put("name",name);       return map;     }     @GetMapping(value="gettest2")     public Map<String, Object> getController2(@RequestParam(value="id") String id,@RequestParam(value="name") String name) {         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",id);         map.put("name",name);         return map;     }      /**      * post请求的用法      */     @RequestMapping(value = "/posttest1",method = RequestMethod.POST)     public Map<String,Object> postController(@RequestBody User user){         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",user.getId());         map.put("name",user.getName());         return map;     }     @PostMapping(value="posttest2")     public Map<String, Object> postController2(@RequestBody User user) {         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",user.getId());         map.put("name",user.getName());         return map;     }      /**      * put请求的用法      */     @RequestMapping(value = "/puttest1",method = RequestMethod.PUT)     public Map<String,Object> putController(@RequestBody User user){         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",user.getId());         map.put("name",user.getName());         return map;     }     @PutMapping(value="puttest2")     public Map<String, Object> putController2(@RequestBody User user) {         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",user.getId());         map.put("name",user.getName());         return map;     }      /**      * delete请求的用法      */     @RequestMapping(value = "/deletetest1",method = RequestMethod.DELETE)     public Map<String,Object> deleteController(@RequestParam(value="id") String id){         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",id);         return map;     }     @DeleteMapping(value="deletetest2")     public Map<String, Object> deleteController2(@RequestParam(value="id") String id) {         Map<String,Object> map = new HashMap<String, Object>();         map.put("id",id);         return map;     } }

通过POSTMAN测试接口的截图

1)path


2)get请求



3)post请求



4)put请求



5)delete请求



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!