Spring MVC @RequestParam - a few key names? or in another way to demand "one or the other,"

What is the best way to resolve multiple names for query parameters? I have a web service that changed parameter names, but needs to take some time to accept the old names.

I do not want to create 2 RequestParams, both are not required, b / c I need this or that present. Something like this would be sweet:

@RequestParam(value = "startTime|start", required = true ) String startTime, 

but not

 @RequestParam(value = "startTime", required = false ) String startTime, @RequestParam(value = "start", required = false ) String start ){ if ( start != null || startTime != null ){ // ... 

Is there any way to do this? Thanks.

+6
source share
1 answer

You can do something like this (create a proxy method that intercepts the "old" calls and transfers them to the new method):

 @RequestMapping(value="/your-mapping", params = {"oldparam1", "oldparam2", ...}) public Whatever yourOldMethod(@RequestParam(value="oldparam1", required=true) String oldParam1, ...){ return yourNewMethod(oldParam1, ...); } @RequestMapping(value="/your-mapping", params = {"newparam1", "newparam1", ...}) public Whatever yourNewMethod(@RequestParam(value="newparam1", required=true) String oldParam1, ...){ //do whatever you need to do here } 

If you do not need to support old calls, just delete yourOldMethod .

The beauty here is using the "params" of @RequestMapping , which allows two methods to listen to the same "URL" (with different parameters for each)

+3
source

All Articles