Madhur Ahuja
Madhur Ahuja

Reputation: 22661

Read query string parameters in Spring REST API (POST)

My Spring REST API is decorated as follows:

In below, I am confused weather, parameters such as list, operation need to be part of Url as query string or do they need to be part of Request Body as form data (Url encoded).

There are situations where I am sending these parameters in query string and it works fine. But couple of my api's are not working properly on production and they work only if I send the data in request body as Url encoded. Can anyone help me explain this behaviour ?

@RequestMapping(value = "/bulkupdate/{companyId}", method = RequestMethod.POST)
   @ResponseBody
   public ResponseEntity<String> bulkupdateArticle(@RequestParam("list") String documentIdList,
      @PathVariable("companyId") String companyId, @RequestParam("operation") String operation){
      try{

Upvotes: 0

Views: 1617

Answers (1)

Jonas Geiregat
Jonas Geiregat

Reputation: 5432

Looking at the resource I find that it could be better designed in a more REST-ful fashion. I don't like to see POSTed data in the reside in the url.

Next to becoming more Rest-ful it would also make live for you much easier.

I would create a Data Transfer Object and pass it as the body of the POST request to your resource/spring controller.

Going from your data:

public class ArticleToUpdate {
    private String list; // list of what ? Maybe design it like List<String> somethingMoreMeaningFull
    private String operation;

    // .. getters
}

public ResponseEntity<String> bulkupdateArticle(@RequestBody ArticleToUpdate articleToUpdate) {
    // .. do whatever you need with the posted data

Now you can post a JSON or XML document in the body which will probably life much easier.

Additionally you could also add validation on the posted data through @Valid support now.

Upvotes: 1

Related Questions