问题描述
我有一个rest应用程序,它返回json/xml.我使用杰克逊和jaxb进行转换.一些方法需要接受query_string.我已经使用@ModelAttribute将query_string映射到一个对象中,但这将对象强制进入了我的视图.我不希望该对象出现在视图中.
I have a rest application that returns json/xml. I use jackson and jaxb for conversion. Some methods need to accept a query_string. I've used @ModelAttribute to map the query_string into an object, but this forces the object into my view. I do not want the object to appear in the view.
我想我需要使用@ModelAttribute以外的其他东西,但是我无法弄清楚如何进行绑定,但是不能修改视图.如果我省略@ModelAttribute批注,则该对象将在视图中显示为变量名称(例如:"sourceBundleRequest").
I think I need to use something other than @ModelAttribute, but I can't figure out how to do the binding, but not modify the view. If I omit @ModelAttribute annotation, the object appears in the view as the variable name (eg: "sourceBundleRequest").
示例网址:
http://localhost:8080/rest/sourcebundles/?updateDate=20100501&label=urgent
控制器方法:
@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public String getAll(@ModelAttribute("form") SourceBundleRequest sourceBundleRequest, BindingResult result, ModelMap model) throws ApiException {
// Detect and report errors.
if (result.hasErrors()) {
// (omitted for clarity)
}
// Fetch matching data.
PaginatedResponse<SourceBundle> sourceBundleResponse = null;
try {
int clientId = getRequestClientId();
sourceBundleResponse = sourceBundleService.get(clientId, sourceBundleRequest);
} catch (ApiServiceException e) {
throw new ApiException(ApiErrorType.INTERNAL_ERROR, "sourceBundle fetch failed");
}
// Return the response.
RestResponse<PaginatedResponse> restResponse = new RestResponse<PaginatedResponse>(200, "OK");
restResponse.setData(sourceBundleResponse);
model.addAttribute("resp", restResponse);
// XXX - how do I prevent "form" from appearing in the view?
return "restResponse";
}
示例输出:
"form": {
"label": "urgent",
"updateDate": 1272697200000,
"sort": null,
"results": 5,
"skip": 0
},
"resp": {
"code": 200,
"status": "OK",
"data": {
"totalAvailable": 0,
"resultList": [ ]
}
}
所需的输出:
"resp": {
"code": 200,
"status": "OK",
"data": {
"totalAvailable": 0,
"resultList": [ ]
}
}
省略@ModelAttribute("form")
如果我只是省略了@ModelAttribute("form"),我仍然会收到不期望的响应,但是传入的表单是由对象名称命名的.响应如下所示:
If I simply omit @ModelAttribute("form"), I still get the undesirable response but the incoming form is named by the object name. The response looks like this:
"resp": {
"code": 200,
"status": "OK",
"data": {
"totalAvailable": 0,
"resultList": [ ]
}
},
"sourceBundleRequest": {
"label": "urgent",
"updateDate": 1272697200000,
"sort": null,
"results": 5,
"skip": 0
}
推荐答案
我不知何故错过了解决此问题的最明显方法.我专注于属性,却忘记了我只能修改基础Map.
Somehow I missed the most obvious way to fix this. I was focused on the Attributes and forgot that I can just modify the underlying Map.
// Remove the form object from the model map.
model.remove("form");
按照Biju的建议省略@ModelAttribute,然后删除sourceBundleRequest对象,可能会更有效率.我怀疑@ModelAttribute有一些额外的开销.
It might be a little more efficient to omit @ModelAttribute as Biju suggested and then remove the sourceBundleRequest object. I suspect @ModelAttribute has some additional overhead.
这篇关于从视图中省略ModelAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!