Я достиг этого с помощью следующего.
@RequestMapping(value="/users", method=RequestMethod.POST)
public Object index(@RequestBody SearchUsersViewModel model, HttpServletResponse response) {
model.setList(userService.getUsers(model));
if(true)
{
return new ModelAndView("controls/tables/users", "model", model);
}
else
{
return JsonView.Render(model, response);
}
}
JsonView.java
public class JsonView {
public static ModelAndView Render(Object model, HttpServletResponse response)
{
MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
MediaType jsonMimeType = MediaType.APPLICATION_JSON;
try {
jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
} catch (HttpMessageNotWritableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
JS Функция, которая делает вызов
config.request = $
.ajax({
url : url,
data : $.toJSON(def.data),
type : def.type,
dataType : def.dataType,
processData : true,
contentType : def.contentType,
success : function(data) {
try {
var json = data;
if (typeof data === "String" || typeof data == "string") {
json = (eval('(' + data + ')'));
}
if ('object' === typeof json) {
if (json.Validation && json.Validation.Errors.length > 0) {
$.each(json.Validation.Errors, function() {
// Error Code Check
});
// Error Callback
if (typeof (def.errorCallback) == 'function') {
def.errorCallback(json);
}
} else {
def.callback(data);
}
} else {
def.callback(data);
}
} catch (e) {
def.callback(data);
// Hide Loading
}
},
error : function(data) {
}
});