java抛出运行期异常_java-在哪里抛出HTTP运行时异常

假设我有以下运行时异常:

@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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值