Spring MVC RestController允许在方法中使用不同名称的params

时间:2016-11-23 11:56:35

标签: spring spring-mvc

我正在使用Spring MVC编写API,我想出了一个问题,允许用不同语言编写的应用程序使用我的API。

事实证明,“Ruby用户”喜欢将他们的参数命名为snake_case,而我们的“Java用户”喜欢将他们的参数命名为camel_case。

是否可以创建允许param名称以多种方式命名但映射到同一方法变量的方法?

例如......如果我有一个接受许多变量的方法,那么就会映射到邮政编码。我可以使用接受“postal_code”和“postalCode”的@RequestParam编写我的方法并将其映射到同一个变量吗?

2 个答案:

答案 0 :(得分:1)

JAX-RS @QueryParam和Spring @RequestParam都不支持您的要求,即将多个请求参数名称映射到同一个变量。 我建议不要这样做,因为它很难支持,因为混淆了哪个参数来自哪个客户端。

但是如果你真的想要处理这个问题((因为你不能改变来自第三方的URL,同意很长时间回来),那么另一种选择就是利用HandlerMethodArgumentResolver来帮助传递我们自己的请求控制器方法的参数(如@MyRequestParam),如下面的代码所示:

控制器类:

@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 Impl类:

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());
    }
}

答案 1 :(得分:0)

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

但是如果您可以更改代码或者告诉您的第三方修改通话我会建议您2个选项

选项1:

使用@PathVariable属性

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

如果将您的网址称为:

,则无关紧要
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可扩展性更高