Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use @Requestparam annotation for a Post request?

I have this controller method:

@PostMapping(         value = "/createleave",         params = {"start","end","hours","username"}) public void createLeave(@RequestParam(value = "start") String start,                         @RequestParam(value = "end") String end,                         @RequestParam(value = "hours") String hours,                         @RequestParam(value = "username") String username){     System.out.println("Entering createLeave " + start + " " + end + " " + hours + " " + username);     LeaveQuery newLeaveQuery = new LeaveQuery();     Account account = accountRepository.findByUsername(username);     newLeaveQuery.setAccount(account);     newLeaveQuery.setStartDate(new Date(Long.parseLong(start)));     newLeaveQuery.setEndDate(new Date(Long.parseLong(end)));     newLeaveQuery.setTotalHours(Integer.parseInt(hours));     leaveQueryRepository.save(newLeaveQuery); } 

However when I send a post request to this endpoint I get the following

"{"timestamp":1511444885321,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.UnsatisfiedServletRequestParameterException","message":"Parameter conditions \"start, end, hours, username\" not met for actual request parameters: ","path":"/api/createleave"}" 

When I remove the params argument from the @PostMapping annotation I get a more general error, it will say that it cannot find the first required parameter (start), while it really is being send together with the parameters end, hours and username.

how to get param in method post spring mvc?

I've read in this post that @RequestParam can only be used for get methods, but if I remove @RequestParam and stick with the params argument of the @PostMapping annotation it still doesn't work. I know I can use @RequestBody but I do not want to make a class just for those 4 parameters. Can anyone tell me how I can make this work?

Thank you

EDIT: I'm reading here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#params-- that the argument params isn't exactly what I thought it was. It seems to be used as a condition. If a set of parameters match a value then the endpoint controller method will be activated.

like image 770
Maurice Avatar asked Nov 23 '17 14:11

Maurice


People also ask

Can we use @RequestParam in GET request?

Similarly, if the request has more than one query string parameter, we can use @RequestParam annotation individually on the respective method arguments. Our controller reads id and name parameters from the request. Executing the respective GET request, we see that both request parameters are mapped correctly.

What does the @RequestParam annotation do?

@RequestParam annotation enables spring to extract input data that may be passed as a query, form data, or any arbitrary custom data.

What is @RequestParam used for?

The @RequestParam is used to read the HTML form data provided by a user and bind it to the request parameter. The Model contains the request data and provides it to view page.


1 Answers

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.

like image 140
Giorgi Tsiklauri Avatar answered Sep 22 '22 22:09

Giorgi Tsiklauri