嗨,这是不好的设计?如果一切按计划进行,我想要返回配置文件。如果没有,我想返回我的自定义错误消息。错误的设计让控制器返回一个对象?
可以吗?
@RequestMapping(value = "/{userId}", method = RequestMethod.PUT)
public @ResponseBody
Object saveUser(@PathVariable Long userId, ModelMap data, @Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return ValidationUtil.createValidationErrors(bindingResult);
}
profileService.save(profile);
return profile;
}
我只需要返回一个json对象。我不需要配置文件。 – pethel
再次,您可以将JSON对象放入请求(或模型)或会话中,然后返回一个字符串。 – TrueDub
我只是在实现一个API。如果我使用jsps创建前端,该模型是否可用? – pethel