0

This question is about best practice and not about problem.

I am writing a spring boot rest service that will perform update meaning basically i am doing POSToperation. The input parameters are something like userId,name,emailAddress,phoneNo.

So my question is, can i do something like below. Just use POST to tell this is update uri but pass all parameters as request param instead of requestBody

@PostMapping(value="/my-url )
    public ResponseEntity<?> myMethod(
          @RequestParam(value = "userId")  String userId,
          @RequestParam(value = "name")  String name,
          @RequestParam(value = "emailAddress")  String emailAddress,
          @RequestParam(value = "phoneNo")  String phoneNo){

I know this works but, is this acceptable ? If this is okay , what is the purpose of using request type as POST if we are passing parameters in the form of requestparam. Expert view is appreciated.

user9735824
  • 1,196
  • 5
  • 19
  • 36
  • Yes, you can do it. Instead of passing lot of parameters, you can create a request body either in xml or json format to increase the visibility. – Sambit Sep 03 '19 at 20:58
  • isn't it fundametally wrong to use requestparam with `POST` ? – user9735824 Sep 03 '19 at 21:00
  • Technically you can do it but it is always used as request body in case of post request. – Sambit Sep 03 '19 at 21:04
  • Possible duplicate of [Can I use @Requestparam annotation for a Post request?](https://stackoverflow.com/questions/47457382/can-i-use-requestparam-annotation-for-a-post-request) – Sambit Sep 03 '19 at 21:04
  • not a duplicate. I am asking best practice and that question is different. – user9735824 Sep 03 '19 at 21:09
  • There is nothing wrong with using parameters (or form parameters in the body) in a POST request. That has been the way the web has been working since its inception, so no there is nothing wrong with it. Although you probably want to create a model (`User`) and automatically let Spring bind the parameters for you. – M. Deinum Sep 04 '19 at 06:20

1 Answers1

1

Yes. Speaking of the best practice i belive the best way is to create a model that will be a @RequestBody paremeter better then pass a number of parameters in the url. moreover, creating a model you denenitlly guarantee a fixed bunch of possible parameters, plus you don't need care of their mandatoriness anymore. BTW. the best practice for an update action is a PUT, but not POST request

Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
  • Thanks for `PUT` and `POST` clarification. So you are saying don't do it but better try not to. So what is the purpose of setting request type as `POST` if we are passing everything as requestparam ? – user9735824 Sep 03 '19 at 21:12
  • sorry, didn't thoroughly get your question( anyway. answering your own question in comments: yes, it's completely wrong to use `POST` for update - there are international agreements. Speaking of a parameters passing.. well nobody forbids you do as you wan, but it depends on an app requirements. Where is a guarantee that the next week won't appear a requirement to update a more complicated model? in this case the best way is to make a fundament for a scalability – Yuriy Tsarkov Sep 03 '19 at 21:29
  • you are saying use requestbody but you are not saying don't use requestparam. Am i right ? – user9735824 Sep 03 '19 at 21:32
  • yap. generally you shoud separate parameters that are responseble for "addressing" of the request and parameters that will be updated. Imagine you need to update a book's publish date and it's ISBN , so the parameters that will be updated are a "isbn" and "publish_date" and them you shoud add into the input model, and the key for update is an id of the book. So then you can make smth like that: `@PutMapping(value = "api/book/{bookId}" public ResponseEntity> myMethod( @RequestBody UpdateBookModel model, @PathVariable("bookId") Long bookId {...}` – Yuriy Tsarkov Sep 03 '19 at 21:44
  • Additional remarks: You can use `PUT` or `PATCH` for an update action, but `PATCH` is often to make changes to part of the resource. – LHCHIN Sep 04 '19 at 01:06