我有一个rest调用,它接受任意查询参数。为了捕获这些,我使用了@RequestParam Map queryParams。
我希望映射中的每个条目都绑定到不同的类型,例如一些到日期,一些到双精度,一些到字符串等…
我该怎么做?
任何代码示例都会有所帮助。
通用汽车最后一定要映射到Map吗?您可以创建一个辅助对象并将所有requestemParams映射到它,如下所示:
CustomObjectDTO
public class CustomObjectDTO{
private String prop1;
private Date prop2;
private int prop3;
//Getters and setters
// propably also the default constructor is needed
}
和你的示例控制器:
public @ResponseBody void doSomething(CustomObjectDTO customObjectDTO){
// do something with the object
}
你可以这样做:
@RequestMapping(value= "/xxx")
public @ResponseBody void reqParamSample(ModelMap model,
HttpServletRequest request,
@RequestParam(value="id") int id,
@RequestParam(value="name") String name){
// do sth
}
请求参数将根据参数名转换为类型