Spring 让控制器返回对象的糟糕设计?
嗨,这是个糟糕的设计吗?我想返回个人资料,如果一切按照计划进行。如果没有,我想返回我的自定义错误消息 这样行吗Spring 让控制器返回对象的糟糕设计?,spring,spring-mvc,spring-3,Spring,Spring Mvc,Spring 3,嗨,这是个糟糕的设计吗?我想返回个人资料,如果一切按照计划进行。如果没有,我想返回我的自定义错误消息 这样行吗 @RequestMapping(value = "/{userId}", method = RequestMethod.PUT) public @ResponseBody Object saveUser(@PathVariable Long userId, ModelMap data, @Valid Profile profile, BindingResult bindingResul
@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;
}
如果您需要访问profile对象,我建议您将其添加到会话或请求上下文中。然后,您可以从saveUser方法(或者ModelAndView对象,如果愿意的话)返回一个字符串,并且该对象仍可供以后的请求使用。如果您需要访问配置文件对象,我建议您将其添加到会话或请求上下文中。然后,您可以从saveUser方法(或ModelAndView对象,如果您愿意)返回一个字符串,并且该对象仍可供以后的请求使用。您应该使用@ExceptionHandler(Exception.class)和@ResponseStatus注释 范例
@ExceptionHandler (Exception.class)
@RequestMapping(value = "/{userId}", method = RequestMethod.PUT)
public @ResponseBody
Profile saveUser(@PathVariable Long userId, ModelMap data, @Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
throw new Exception("message");
if(ValidationUtil.createValidationErrors(bindingResult)) {
throw new Exception("message");
}
}
profileService.save(profile);
return profile;
}
有关更多详细信息,请参阅
您应该使用@ExceptionHandler(Exception.class)和@ResponseStatus注释 范例
@ExceptionHandler (Exception.class)
@RequestMapping(value = "/{userId}", method = RequestMethod.PUT)
public @ResponseBody
Profile saveUser(@PathVariable Long userId, ModelMap data, @Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {
if (bindingResult.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
throw new Exception("message");
if(ValidationUtil.createValidationErrors(bindingResult)) {
throw new Exception("message");
}
}
profileService.save(profile);
return profile;
}
有关更多详细信息,请参阅
我只需要返回一个json对象。我不需要配置文件。同样,您可以将JSON对象放入请求(或模型)或会话中,然后返回字符串。我只是在实现一个API。如果我使用JSP创建前端,该模型就可用?不,一个模型返回到浏览器,其中包含请求中命名的对象。我认为最简单的事情就是创建一个ModelAndView对象。然后在该对象中设置视图,并向其中添加JSON对象。返回它将把浏览器发送到正确的链接,并将JSON对象放在浏览器对accessI的响应范围中。我只需要返回一个JSON对象。我不需要配置文件。同样,您可以将JSON对象放入请求(或模型)或会话中,然后返回字符串。我只是在实现一个API。如果我使用JSP创建前端,该模型就可用?不,一个模型返回到浏览器,其中包含请求中命名的对象。我认为最简单的事情就是创建一个ModelAndView对象。然后在该对象中设置视图,并向其中添加JSON对象。返回该值会将浏览器发送到正确的链接,并将JSON对象放置在浏览器访问的响应范围中。我没有实现前端。访问我的API的人还能使用这个模型吗?它是一个RESTFUL web服务吗?或其他一些服务API?bindingresult不会通过验证错误验证。控制器方法不应使用@ExceptionHandler(Exception.class)进行注释?如果存在任何绑定错误或验证错误,则代码应抛出自定义的用户定义异常。上面的代码只是一个带有泛型异常的示例,我没有实现前端。访问我的API的人还能使用这个模型吗?它是一个RESTFUL web服务吗?或其他一些服务API?bindingresult不会通过验证错误验证。控制器方法不应使用@ExceptionHandler(Exception.class)进行注释?如果存在任何绑定错误或验证错误,则代码应抛出自定义的用户定义异常。上面的代码只是一个带有泛型异常的示例