Spring MVC将很多@RequestParam包装到Object中

时间:2016-02-17 19:28:40

标签: java spring spring-mvc spring-restcontroller

我有一个RESTful控制器的方法,如:

 @RequestMapping(value = "", method = RequestMethod.GET,
        produces = {"application/json", "application/xml"})
@ResponseStatus(HttpStatus.OK)
public
@ResponseBody
Object getAll(@RequestParam(value = "1", required = false) Integer a,
              @RequestParam(value = "2", required = false) Integer b,
              @RequestParam(value = "2", required = false) String c,
              @RequestParam(value = "3", required = false) String d,
              @RequestParam(value = "4", required = false) String e,
              @RequestParam(value = "5", required = false) String f,
              @RequestParam(value = "6", required = false) String g,
              @RequestParam(value = "7", required = false) String h,
              HttpServletRequest request, HttpServletResponse response) {

    //some logic
    return service.getAll();
}

是否可以将所有参数包装到某些数据传输对象?对于像这样的课程:

     public class ParamsDTO {

public String a;

public String b;

public String c;

//list of all params
}

因此我可以重构我的控制器的方法,如:

@RequestMapping(value = "", method = RequestMethod.GET,
        produces = {"application/json", "application/xml"})
@ResponseStatus(HttpStatus.OK)
public
@ResponseBody
Object getAll(@RequestBody ParamsDTO params,
              HttpServletRequest request, HttpServletResponse response) {

    //some logic
    return service.getAll();
}

我问,因为我有大约10个不同的URL映射与所有这些参数,如果我要添加新的或删除旧的,我必须在10个地方更改它。有没有更好的方法来实现这一目标?提前谢谢。

0 个答案:

没有答案