假设我有以下运行时异常:
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
public class ExpectationsFailedException extends RuntimeException {
public ExpectationsFailedException(String message) {
super(message);
}
}
我的问题是在服务层中抛出先前的HTTP异常是否可以,还是应该从控制器中抛出该异常:
@Service
public class UserService {
@Autowired
...
public void addUser(final String email, final String username, final String password){
if(parameters_are_not_valid){
throw new ExpectationsFailedException("Invalid input");
}
}
}
控制器异常抛出解决方案如下:
@Service
public class UserService {
@Autowired
...
public void addUser(final String email, final String username, final String password) throws InvalidInputParameters {
if(parameters_are_not_valid){
throw new InvalidInputParameters("Invalid input");
}
}
}
在我的控制器中
@RestController
public class XController{
@Autowired
private UserService userService;
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public void addUser(@Valid @RequestBody SignUpForm form, BindingResult bindingResult){
if(bindingResult.hasErrors()){
throw new ExpectationsFailedException("Input parameters conditions were not fulfilled");
}
try {
userService.addUser(...);
}
catch(InvalidInputParameters ex){
throw new ExpectationsFailedException("Invalid service input parameters");
}
}
}
这些解决方案中的哪一个是首选?为什么?我有一种不应该在服务中引发HTTP异常的感觉,因为我可能会在与HTTP不相关的其他上下文中使用该服务.
我会选择第二个.
你怎么看?
解决方法:
我同意你的最后声明.您的服务层应该独立于HTTP或前端框架(@ResponseStatus是Spring MVC注释,因此在服务层中使用它不是最佳实践).
但是,您不必在服务层中引发一个异常,不必在控制器中捕获该异常,也不必重新引发另一个以@ResponseStatus注释的异常.只需为服务异常添加异常处理程序并从中返回适当的响应状态即可.您有很多选择,例如@ExceptionHandler:
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
@ExceptionHandler(InvalidInputParameters.class)
public void handle() {
// Do nothing, just return the status
}
您可以将此代码放到带注释的@ControllerAdvice类中,以对所有控制器启用它,或者如果在其他地方不需要它,则仅在您的控制器中启用它.
标签:spring,java
来源: https://codeday.me/bug/20191119/2036562.html