-1

In a controller class,

I have one method

@RequestMapping(value="test", method={RequestMethod.POST,RequestMethod.PUT})
@ResponseBody
public String testApp(@RequestParam String priceA, @RequestBody String valueOfProduct) throws Exception {  

}

My client is sending prices to my App as POST requests for processing.

Now client (which is not under my control) is planning to send the price value as a request parameter and another client is planning to send price in requestheader.

The trick is: If it is present in requestheader it wont be present in requestparameter.

So I have to design it such that my server code works fine in both cases.

Kindly let me know which design would be the best.

Will it be

@RequestMapping(value="test", method={RequestMethod.POST,RequestMethod.PUT})
@ResponseBody
public String testApp(@RequestParam String priceA, @RequestHeader("PRICE") String priceAFromAnother,  @RequestBody String valueOfProduct) throws Exception {  

}

But above logic wont work as @RequestParam wont be available all the time.

José Ricardo Pla
  • 1,043
  • 10
  • 16
user2604052
  • 51
  • 1
  • 3
  • 9

2 Answers2

0

Have you tried searching by your own first? Take a look that this answer.

Community
  • 1
  • 1
Nakul91
  • 1,245
  • 13
  • 30
  • Yes, I had checked. But I have been asked to keep the same signature itself. I have permissions to add a argument. That is why I cannot take the httpservletrequest approach. Else I know I can easily get the values using httpservletrequest.getParam() and httpservletrequest.getHeader(). The challenge is making the changes with minimum changes to design and unit testcases. – user2604052 May 25 '15 at 16:59
0

If you know the header to search for, you could add a request filter (HttpServletRequestWrapper) that pre-process the request. Here is simple example Modify request parameter with servlet filter

NOTE: I would caution you that this methodology is not maintainable if your parameter set/api is going to grow over time.

Community
  • 1
  • 1
Grady G Cooper
  • 1,044
  • 8
  • 19
  • Mine is a spring controller class. So If I follow this approach, it would force me to make drastic changes in design of public String testApp(@RequestParam String priceA, @RequestBody String valueOfProduct) throws Exception { } This will create problems for my client sending POST request right. I have no control over client code. Kindly advice. – user2604052 May 26 '15 at 02:40