我有一个 HTTP 表单,它返回以下 HttpRequest 请求正文,如下所示
propertyName0=name0&propertyValue0=value0&propertyDescription0=description0&
propertyName1=name1&propertyValue1=value1&propertyDescription1=description1&propertyName2=name2&propertyValue2=value2&propertyDescription1=description2
我有一个具有以下属性的模型
public class Property {
private String propertyName;
private String propertyValue;
private String propertyDescription;
// Getters and setters
}
我希望能够将请求正文变量转换为 Property 对象列表。有办法做到这一点吗?
我的 Controller 代码(建议)
@RequestMapping(value="update", method=RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void update(List<Property> properties) {
// Do something with properties.
}
@RequestMapping(value="/fetch/}", method=RequestMethod.GET)
@ResponseBody
public String searchConfigProperties() throws Exception {
List<Property> properties = // Some code that generates a list. model.
Gson gson = new Gson();
return gson.toJson(properties);
}
更新 Controller 代表服务架构后端代码,不一定要绑定(bind)到任何 View 。
最佳答案
恕我直言,如果你有:Spring会自动执行此操作:
props[0].propertyName=name0&props[0].propertyValue=value0&props[0].propertyDescription=description0&
props[1].propertyName=name1&props[1].propertyValue=value1&props[1].propertyDescription=description1&props[2].propertyName=name2&props[2].propertyValue=value2&props[1].propertyDescription=description2
如果您可以修改表单以获得这样的字符串,那么一切都会正常工作。
关于java - 将 spring 请求主体转换为对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25152812/