2016-11-23 99 views
1

我正在使用Spring MVC编写一个API,并且我提出了一个问题,允许使用不同语言编写的应用程序使用我的API。Spring MVC RestController允许在方法中使用不同名称的参数

事实证明,“Ruby用户”喜欢在snake_case中命名参数,我们的“Java用户”喜欢在camel_case中使用它们的参数名。

是否有可能创建我的方法,允许param名称命名多种方式,但映射到相同的方法变量?

例如...如果我有一个方法接受一些变量,其中有映射到邮政编码。我可以用一个接受BOTH“postal_code”和“postalCode”的@RequestParam编写我的方法,并将它映射到相同的变量吗?

+0

你为什么做 “Ruby用户” 的代码更复杂bacause。 Api应该是客户端独立的。 – dit

回答

1

无论JAX-RS @QueryParam也不春天@RequestParam支持您的需求,即将多个请求参数名称映射到同一个变量。 我建议不要这样做,因为它会很难支持,因为混淆如哪个参数来自哪个客户端。如果你真的想处理这个问题((因为你不能改变来自第三方的URL,同意很久以后),那么另一种方法是利用HandlerMethodArgumentResolver这有助于传递我们自己的请求参数(像@MyRequestParam)像如图中下面的代码的控制器的方法:

Controller类:

@Controller 
public class MyController { 
    @RequestMapping(value="/xyz") 
    public void train1(@MyRequestParam String postcode) {//custom method argument injected 
     //Add your code here 
    } 
} 

MyRequestParam:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.PARAMETER) 
public @interface MyRequestParam { 
} 

HandlerMethodArgumentResolver实现类:

public class MyRequestParamWebArgumentResolver implements HandlerMethodArgumentResolver { 

    @Override 
    public Object resolveArgument(MethodParameter parameter, 
      ModelAndViewContainer mavContainer, 
      NativeWebRequest webRequest, 
      WebDataBinderFactory binderFactory) { 

     MyRequestParam myRequestParam = 
       parameter.getParameterAnnotation(MyRequestParam.class); 

     if(myRequestParam != null) { 

      HttpServletRequest request = 
       (HttpServletRequest) webRequest.getNativeRequest(); 

      String myParamValueToBeSentToController = ""; 
      //set the value from request.getParameter("postal_code") 
      //or request.getParameter("postalCode") 

      return myParamValueToBeSentToController; 
     }   
     return null; 
    } 

    @Override 
    public boolean supportsParameter(MethodParameter parameter) { 
     return (parameter.getParameterAnnotation(MyRequestParam.class) != null); 
    }  
} 

WebMvcConfigurerAdapter类:

@Configuration 
class WebMvcContext extends WebMvcConfigurerAdapter { 

    @Override 
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { 
     argumentResolvers.add(new MyRequestParamWebArgumentResolver()); 
    } 
} 
0

我认为你想做的事情是Spring框架不允许使用注释RequestParam。

但是如果你能够改变代码或者说您的第三方修改来电,我会建议你两个选择

选项1:

使用@PathVariable财产

@RequestMapping(value = "/postalcode/{postalCode}", method = RequestMethod.GET) 
public ModelAndView yourMethod(@PathVariable("postalCode") String postalCode) { 
//...your code 

这里没关系,如果你打电话给你的URL:

http://domain/app/postalcode/E1-2ES 
http://domain/app/postalcode/23580 

选项2:

创建您的控制器2种方法,并使用相同的服务

@RequestMapping(value = "/postalcode", method = RequestMethod.GET, params={"postalCode"}) 
public ModelAndView yourMethod(@RequestParam("postalCode") String postalCode) { 
    //...call the service 


@RequestMapping(value = "/postalcode", method = RequestMethod.GET, params={"postal_code"}) 
public ModelAndView yourMethodClient2(@RequestParam("postal_code") String postalCode) { 
    //...call the service 

如果有可能,我会建议你选择1是更具可扩展性

+0

是什么让选项1更具可扩展性?它如何处理5个参数?为什么'yourMethodClient2'不能简单地调用'yourMethod'? – zeroflagL

+0

使用5个,10个或更多的参数,它将工作得很好,第二个选项不是很好,主要是因为重复的代码。 – yt61

+0

总是取决于方法的种类,参数的数量,增加第三方数量的可能性,... – cralfaro

相关问题