Springboot错误处理

时间:2018-05-25 00:47:04

标签: java spring-boot error-handling

我正在开发一个处理登录的示例spring-boot API,但API工作正常,我在错误处理期间遇到了问题。我在教程中看到的所有错误处理都是在控制器中完成的,但是我的逻辑是在服务中,那就是我想要进行错误处理的地方。

如何在服务中实现不同类型错误的错误处理,或者这被认为是不好的做法。我目前的问题是我的错误消息如下所示:

{

 "message": "Please Activate account before attempting to Sign in",
  "token": " ",
  "accountLocked": false,
  "accountEnabled": false,
  "status": false,
  "balance": " ",
  "profileImage": null,
  "roles": null,
  "baraAuthorities": null,
  "customerNumber": 0,
  "userEmail": "",
  "name": ""
}

但是我希望只有消息和状态显示并在响应中隐藏其余部分,我该如何实现:

{
  "message": "Please Activate account before attempting to Sign in",
  "status": false
}

这是我的LoginService代码:

@Service
public class LoginService implements UserDetailsService {

    @Autowired
    private UserLoginRepository loginRepository;
    @Autowired
    private LoginInformationRepository logininfoRepository;

    BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
    String balance, message, pin, token, userEmail, name, profileImage = "";
    Boolean status, successStatus, accounState = false;
    int customerNumber, attempts;
    List<Roles> userRoles = new ArrayList();

    private boolean userNameExist(String username) {
        UserLogin user = loginRepository.findByUsername(username);
        if (user != null) {
            return true;
        }
        return false;
    }

    public LoginResponse verifyLoginDetails(LoginObject user) throws LoginException {

        AuthoritiesKeys userAuth = new AuthoritiesKeys();
        Merchant searchedMerchant = merchantRepository.findByUserlogin_LoginCredentialsID(userdetails.getLoginCredentialsID());
        DateUtility currentDate = new DateUtility();
        Boolean status = false;

        if (userdetails == null) {
            return new LoginResponse("Unable to Login. Please check login details and try again",status);
        } else {

            pin = user.getPassword();
            attempts = logininfoRepository.countByTodayDateAndUserLoginLoginCredentialsIDAndLoginSuccessStatusFalse(currentDate.getCurrentDate(), userdetails.getLoginCredentialsID());
            if (attempts < 3) {
                if (bCryptPasswordEncoder.matches(pin, userdetails.getPassword())) {

                    if (userdetails.isEnabled() == true) {
                        if (searchedMerchant != null) {

                            message = "Logged in Successfully";
                            status = userdetails.isAccountNonLocked();
                            accounState = userdetails.isEnabled();
                            userRoles = (List<Roles>) userdetails.getAuthorities();
                            balance = searchedMerchant.getAccount().getBalance().toString();
                            successStatus = true;
                            customerNumber = searchedMerchant.getMerchantNumber();
                            userEmail = searchedMerchant.getEmail();
                            name = searchedMerchant.getBusinessName();

                            return new LoginResponse(message, token, accounState, status, userRoles, successStatus, balance, userAuth, profileImage, customerNumber, userEmail, name);

                        } else {
                            return new LoginResponse("Error Merchant Information Error . Please contact the team",status);
                        }

                    } else {
                        return new LoginResponse("Please Activate account before attempting to Sign in",status);
                    }
                } else {
                    return new LoginResponse("Wrong Username or Password",status);
                }

            } else {
                userdetails.setAccountNonLocked(false);
                userdetails.setEnabled(false);
                loginRepository.save(userdetails);
                return new LoginResponse("Too many attempts account has been locked",status);
            }


        }

    }

这是我的LoginResponse代码:

 public class LoginResponse {

    private String message;
    private String token;
    private Boolean accountLocked;
    private Boolean accountEnabled;
    private Boolean status;
    private String balance;
    private String profileImage;
    List<Roles> roles = new ArrayList<>();
    AuthoritiesKeys baraAuthorities = new AuthoritiesKeys();
    private int customerNumber;
    private String userEmail;
    private String name;

    public LoginResponse() {
    }

    public LoginResponse(String message, Boolean status) {
        this.message = message;
        this.status = status;
    }

    public LoginResponse(String message, String token, Boolean accountLocked, Boolean accountEnabled, List<Roles> myroles, Boolean status, String balance, AuthoritiesKeys userBaraAuthorities, String profileImage, int customerNumber, String userEmail, String name) {
        this.message = message;
        this.token = token;
        this.accountLocked = accountLocked;
        this.accountEnabled = accountEnabled;
        this.status = status;
        this.balance = balance;
        this.roles = myroles;
        this.baraAuthorities = userBaraAuthorities;
        this.profileImage = profileImage;
        this.customerNumber = customerNumber;
        this.userEmail = userEmail;
        this.name = name;
    }

… // left getter and setters for brevity

}

这是我的LoginController代码:

@RestController
@Api(value = "Login API", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserLoginController {

    @Autowired
    private LoginService loginService;
    @RequestMapping(method = RequestMethod.POST, value = "/api/usermanagement/user/login")
    @ApiOperation("login registered user")
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = ResponseMessage.class)})
    public LoginResponse Login(@Valid @RequestBody LoginObject credentails) throws LoginException {
        return loginService.verifyLoginDetails(credentails);

    }

}

1 个答案:

答案 0 :(得分:1)

使用时应使用<div class="container-fluid"> <div class="row"> <div class="col-md-4"> <div class="srch"> <form method="post" class="col-md-7"> <input type="text" name="search" placeholder=" SEARCH" id="srch"> </form> </div> </div> <div class="col-md-4"> <a href="#top" class="navbar-brand"><img src="image/logo.jpg" width="250" height="74"></a> </div> <div class="col-md-4"> <button type="button" class="navbar-toggle x collapsed line" data-toggle="collapse" data-target="#navbar-collapse-x"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> </div> </div> 类来处理如下所示的异常

创建课程ResponseEntityExceptionHandler

GlobalControllerExceptionHandler

创建课程import javax.validation.ConstraintViolationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation .ResponseEntityExceptionHandler; @ControllerAdvice public class GlobalControllerExceptionHandler extends ResponseEntityExceptionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(GlobalControllerExceptionHandler.class); //////////// Globle exception handler /////////// @ExceptionHandler(Exception.class) public ResponseEntity handleAnyException(final Exception e,final WebRequest request) { LOGGER.error("Error occurred while processing request: {}", e.getMessage()); e.printStackTrace(); ResponseDTO responseDTO = new ResponseDTO(); responseDTO.setSuccess(false); responseDTO.setCode(500); responseDTO.setMessage(e.getMessage()); return new ResponseEntity<>(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR); } ////////////Self define exception class handler /////////// @ExceptionHandler(InvalidCredentialException.class) public ResponseEntity handleInvalidCredentialException(final InvalidCredentialException e,final WebRequest request) { LOGGER.error("Invalid Credential: "+ e.getCause()); ResponseDTO responseDTO = new ResponseDTO(); responseDTO.setSuccess(false); responseDTO.setCode(e.getErrorCode()); responseDTO.setMessage(e.getMessage()); return new ResponseEntity<>(responseDTO, HttpStatus.UNAUTHORIZED); }}

InvalidCredentialException

现在抛出如下错误

public class InvalidCredentialException extends RuntimeException {

private static final long serialVersionUID = -3338166707608147652L;
private int errorCode;

public InvalidCredentialException() {

}

public InvalidCredentialException(final Throwable cause) {
    super(cause);
}

public InvalidCredentialException(final String message) {
    super(message);
}

public InvalidCredentialException(final int errorCode, final String message) {
    super(message);
    this.setErrorCode(errorCode);
}

public InvalidCredentialException(final String message, final Throwable cause) {
    super(message, cause);
}

public int getErrorCode() {
    return errorCode;
}}

我建议你不要在你的代码中使用try-catch块(除非你需要)因为如果发生任何异常并且没有被catch块处理,那么它将进入全局异常类throw new InvalidCredentialException(401, "No project associated with this id"); 处理程序。这里Exception也是一个自定义类,它有一些变量,如ResponseDTO等。