Can I use @Requestparam annotation for a Post request?

前端 未结 5 666
名媛妹妹
名媛妹妹 2020-12-02 19:10

I have this controller method:

@PostMapping(
        value = \"/createleave\",
        params = {\"start\",\"end\",\"hours\",\"username\"})
public void creat         


        
相关标签:
5条回答
  • 2020-12-02 19:29

    You should use @RequestBody instead of using @RequestParam And you should provide whole object as a body of request @RequestParam is to get data from URL

    you can do something like public saveUser(@RequestBody User user) { do something with user }

    and it will be mapped as User object for example

    0 讨论(0)
  • 2020-12-02 19:31
    public void createLeave(@RequestParam Map<String, String> requestParams)
    

    Above code did not work.

    Correct syntax is:

    public void createLeave(@RequestBodyMap<String, String> requestParams)
    
    0 讨论(0)
  • 2020-12-02 19:33

    What you are asking for is fundamentally wrong. POST requests sends data in a body payload, which is mapped via @RequestBody. @RequestParam is used to map data through the URL parameters such as /url?start=foo. What you are trying to do is use @RequestParam to do the job of @RequestBody.

    Alternative solutions for REST controllers

    • Introduce a DTO class. It is the most preferred and clean method.
    • If you really want to avoid creating a class, you can use @RequestBody Map<String, String> payload. Be sure to include 'Content-Type': 'application/json' in your request header.
    • If you really want to use @RequestParam, use a GET request instead and send your data via URL parameters.

    Alternative solutions for MVC controllers

    • Introduce a DTO class and use it with annotation @ModelAttribute.
    • If you transform the form data into JSON, you can use @RequestBody Map<String, String> payload. To do this, please see this answer.

    It is not possible to map form data encoded data directly to a Map<String, String>.

    0 讨论(0)
  • 2020-12-02 19:40

    Well, I think the answer by @Synch is fundamentally wrong, and not the question being asked.

    1. First of all, I use @RequestParam in a lot of scenarios expecting either GET or POST HTTP messages and I'd like to say, that it works perfectly fine;
    2. POST Message's data payload (body), which is referred to the most voted answer (again, by @Synch) is actually the text data, which can perfectly legally be paramname=paramvalue key-value mapping(s) alike (see POST Message Body types here);
    3. docs.spring.io, an official source for Spring Documentation, clearly states, that:

      In Spring MVC, "request parameters" map to query parameters, form data, and parts in multipart requests.

    So, I think the answer is YES, you can use @RequestParam annotation with @Controller class's method's parameter, as long as that method is request-mapped by @RequestMapping and you don't expect Object, this is perfectly legal and there's nothing wrong with it.

    0 讨论(0)
  • 2020-12-02 19:44
    @PostMapping("/createleave")
    public void createLeave(@RequestParam Map<String, String> requestParams){
    
        String start = requestParams.get("start");
        String end= requestParams.get("end");
        String hours= requestParams.get("hours");
        String username = requestParams.get("username");
    
        System.out.println("Entering createLeave " + start + " " + end + " " + hours + " " + username);
    }
    

    This is for multipart/form-data enctype post request.

    0 讨论(0)
提交回复
热议问题