gpt4 book ai didi

java - 我应该在模型中使用 HttpStatus 抛出异常吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:43 26 4
gpt4 key购买 nike

假设我开发了一些 rest api。我有网络层( Controller )和服务层(模型)。在服务层使用 HttpStatus 代码抛出异常是好习惯吗?

有人会说,那个模型应该对web层一无所知,不应该依赖于web层。

但是,让我们考虑一下这种情况:

在 Controller 中使用 HttpStatus 抛出异常

--- 服务 ---

 class userService {

public void updateUser(int userId, String username, String email) {

if ( //userid not found) {
throw new UserNotFoundException("User not found");
}

if ( // bad email format) {
throw new BadArgumentException("Bad email format");
}

if ( // user is not active) {
throw new AccessDeniedException("User is not active");
}

... here save user ...

}
}

--- Controller ---

class UserController {

@RequestMapping ....
public void updateUser(int id, String username, String email) {
try{
userService.updateUser(id, username, email);
}
catch (UserNotFoundException e) {
throw new HttpException(e.getMessage(), HttpStatus.NOT_FOUND);
}
catch (BadArgumentExceptione e) {
throw new HttpException(e.getMessage(), HttpStatus.BAD_REQUEST);
}
catch (AccessDeniedException e) {
throw new HttpException(e.getMessage(), HttpStatus.FORBIDEN);
}
}
}

你看到了吗?我应该编写多少额外的代码才能向 Api 客户端返回正确的响应?此外,我可能会忘记捕获一些可以正确报告的异常,它将作为默认的 Internal Server Exception 返回。在 Controller 中,我总是应该查看服务层并检查哪些异常可以抛出服务以正确处理它们。 (请不要在 java 中建议检查异常)。

现在让我们看看另一个解决方案:

在服务层(模型)中使用 HttpStatus 抛出异常

--- 服务 ---

类用户服务{

   public void updateUser(int userId, String username, String email) {

if ( //userid not found) {
throw new UserNotFoundException("User not found", HttpStatus.NOT_FOUND);
}

if ( // bad email format) {
throw new BadArgumentException("Bad email format", HttpStatus.BAD_REQUEST);
}

if ( // user is not active) {
throw new AccessDeniedException("User is not active", HTTP_STATUS.FORBIDEN);
}

... here save user ...

}
}

-- Controller --

class UserController {

@RequestMapping ....
public void updateUser(int id, String username, String email) {
userService.updateUser(id, username, email);
}
}

就是这样。更少的代码。现在我不必处理每个可能抛出服务层的异常,我不必每次编写 Controller 时都手动检查服务异常并且我不会忘记处理一些异常(因为它们在服务层中正确形成)这不太容易出错。

再说一次,在服务层处理 Http 相关数据是一种不好的做法吗?

如果不好,你将如何处理我描述的问题。

谢谢。

P.S.:在这两种解决方案中,一些通用的 ErrorHandler 会捕获异常并使用适当的状态代码形成响应。

最佳答案

您可以在 Controller 内部的方法中使用@ExceptionHandler 来管理同一 Controller 中的异常

 @ExceptionHandler({MyException.class,OtherException.class})
public String myMethodExceptionHandler() {...}

或者您可以使用@ControllerAdvice 创建一个类来管理您所有 Controller 的错误

@ControllerAdvice
class GlobalControllerExceptionHandler {
@ResponseStatus(HttpStatus.CONFLICT) // 409
@ExceptionHandler(MyException.class)
public void handleConflict() {
// Nothing to do
}
}

这里有教程。 https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

关于java - 我应该在模型中使用 HttpStatus 抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com