Java Code Examples for org.springframework.http.HttpStatus#EXPECTATION_FAILED

The following examples show how to use org.springframework.http.HttpStatus#EXPECTATION_FAILED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AccountController.java    From cf-SpringBootTrader with Apache License 2.0 6 votes vote down vote up
/**
 * REST call to decrease the balance in the account. Decreases the balance
 * of the account if the new balance is not lower than zero. Returns HTTP OK
 * and the new balance if the decrease was successful, or HTTP
 * EXPECTATION_FAILED if the new balance would be negative and the
 * old/current balance.
 * 
 * @param userId
 *            The id of the account.
 * @param amount
 *            The amount to decrease the balance by.
 * @return The new balance of the account with HTTP OK.
 */
@RequestMapping(value = "/accounts/{userId}/decreaseBalance/{amount}", method = RequestMethod.GET)
public ResponseEntity<Double> decreaseBalance(@PathVariable("userId") final String userId, @PathVariable("amount") final double amount) {

	logger.debug("AccountController.decreaseBalance: id='" + userId + "', amount='" + amount + "'");

	Account accountResponse = this.service.findAccount(userId);

	BigDecimal currentBalance = accountResponse.getBalance();

	BigDecimal newBalance = currentBalance.subtract(new BigDecimal(amount));

	if (newBalance.compareTo(BigDecimal.ZERO) >= 0) {
		accountResponse.setBalance(newBalance);
		this.service.saveAccount(accountResponse);
		return new ResponseEntity<Double>(accountResponse.getBalance().doubleValue(), getNoCacheHeaders(), HttpStatus.OK);

	} else {
		// no sufficient founds available
		return new ResponseEntity<Double>(accountResponse.getBalance().doubleValue(), getNoCacheHeaders(), HttpStatus.EXPECTATION_FAILED);
	}

}
 
Example 2
Source File: IMExceptionHandler.java    From issue-management with MIT License 4 votes vote down vote up
@ExceptionHandler(Exception.class)
public final ResponseEntity<?> handleExceptions(Exception ex, WebRequest request) {
    log.error("ControllerAdvice -> ExceptionHandler -> " , ex ,request);
    ExceptionResponse  exceptionResponse =new ExceptionResponse(new Date(),ex.getMessage());
    return new ResponseEntity<>(exceptionResponse , HttpStatus.EXPECTATION_FAILED);
}
 
Example 3
Source File: CustomizedResponseEntityExceptionHandler.java    From pacbot with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(RuleJarFileMissingException.class)
public final ResponseEntity<Object> handlerRuleJarFileMissingException(RuleJarFileMissingException exception, WebRequest webRequest) {
	ExceptionResponse exceptionResponse = new ExceptionResponse(exception.getMessage(), AdminConstants.JAR_FILE_MISSING);
	return new ResponseEntity<>(exceptionResponse, HttpStatus.EXPECTATION_FAILED);
}
 
Example 4
Source File: CustomizedResponseEntityExceptionHandler.java    From pacbot with Apache License 2.0 4 votes vote down vote up
@ExceptionHandler(PacManException.class)
public final ResponseEntity<Object> handlerRuleJarFileMissingException(PacManException exception, WebRequest webRequest) {
	ExceptionResponse exceptionResponse = new ExceptionResponse(exception.getMessage(), exception.getMessage());
	return new ResponseEntity<>(exceptionResponse, HttpStatus.EXPECTATION_FAILED);
}
 
Example 5
Source File: ExceptionAdvice.java    From ssm with Apache License 2.0 4 votes vote down vote up
/**
 * 处理自定义异常
 * @param e
 * @return
 */
@ExceptionHandler(IException.class)
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
public Msg handleIException(IException e){
	return Msg.message(417,"自定义异常");
}
 
Example 6
Source File: CustomAdvice.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@ResponseStatus(HttpStatus.EXPECTATION_FAILED)
@ExceptionHandler(HandledPojoException.class)
public Error handlePojoExcepton(HandledPojoException e) {
    return new Error(e.getMessage());
}
 
Example 7
Source File: CollectionService.java    From Pixiv-Illustration-Collection-Backend with Apache License 2.0 4 votes vote down vote up
@Transactional(rollbackFor = Exception.class)
public Boolean updateIllustrationOrder(Integer collectionId, UpdateIllustrationOrderDTO updateIllustrationOrderDTO, Integer userId) {
    long start = System.currentTimeMillis();
    System.out.println(start);
    //校验collectionId是否属于用户
    checkCollectionAuth(collectionId, userId);
    //输入三个illust对象,分别是要插入位置的上下两个 以及 插入对象
    //查看要插入的画作是否在画集中
    Integer illustrationOrder = queryIllustrationOrder(collectionId, updateIllustrationOrderDTO.getReOrderIllustrationId());
    if (illustrationOrder == null) {
        throw new BusinessException(HttpStatus.BAD_REQUEST, "画作不在画集中");
    }
    //带有超时的cas
    long now = System.currentTimeMillis();
    while (System.currentTimeMillis() - now < 10 * 1000) {
        //尝试加锁
        if (checkCollectionUpdateStatus(collectionId)) {
            continue;
        }
        Integer upperIndex;
        Integer lowerIndex;
        Integer originalIndex;
        Integer resultIndex;
        //查出上界
        try {
            originalIndex = queryIllustrationOrder(collectionId, updateIllustrationOrderDTO.getReOrderIllustrationId());
            upperIndex = queryIllustrationOrder(collectionId, updateIllustrationOrderDTO.getUpIllustrationId());
            if (upperIndex == null) {
                throw new BusinessException(HttpStatus.BAD_REQUEST, "画作不在画集中");
            }
            //取中值
            if (updateIllustrationOrderDTO.getLowIllustrationId() == null) {
                //无下界,直接上界+1w
                resultIndex = upperIndex + 10000;
            } else {
                //查出下界index
                lowerIndex = queryIllustrationOrder(collectionId, updateIllustrationOrderDTO.getLowIllustrationId());
                if (lowerIndex == null) {
                    throw new BusinessException(HttpStatus.BAD_REQUEST, "画作不在画集中");
                }
                resultIndex = (upperIndex + lowerIndex) / 2;
            }
            if (resultIndex.compareTo(originalIndex) != 0) {
                //更新
                collectionMapper.updateIllustrationOrder(collectionId, updateIllustrationOrderDTO.getReOrderIllustrationId(), resultIndex);
                //并更改上界插入因子
                collectionMapper.incrIllustrationInsertFactor(collectionId, updateIllustrationOrderDTO.getUpIllustrationId());
                Integer insertFactor = collectionMapper.queryIllustrationInsertFactor(collectionId, updateIllustrationOrderDTO.getUpIllustrationId());
                //判断上界是否达到阈值
                if (insertFactor >= 10) {
                    //达到则进行全量更新,并把插入因子都置为0
                    collectionMapper.reOrderIllustration(collectionId);
                }
            }
            System.out.println("耗时:" + (System.currentTimeMillis() - start));
            return true;
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new BusinessException(HttpStatus.EXPECTATION_FAILED, "未知异常");
        } finally {
            stringRedisTemplate.delete(COLLECTION_REORDER_LOCK + collectionId);
        }

    }
    return false;
}
 
Example 8
Source File: AccountController.java    From cf-SpringBootTrader with Apache License 2.0 3 votes vote down vote up
/**
 * REST call to increase the balance in the account. Increases the balance
 * of the account if the amount is not negative. Returns HTTP OK and the new
 * balance if the increase was successful, or HTTP EXPECTATION_FAILED if the
 * amount given is negative.
 * 
 * @param userId
 *            The id of the account.
 * @param amount
 *            The amount to increase the balance by.
 * @return The new balance of the account with HTTP OK.
 */
@RequestMapping(value = "/accounts/{userId}/increaseBalance/{amount}", method = RequestMethod.GET)
public ResponseEntity<Double> increaseBalance(@PathVariable("userId") final String userId, @PathVariable("amount") final double amount) {

	logger.debug("AccountController.increaseBalance: id='" + userId + "', amount='" + amount + "'");

	Account accountResponse = this.service.findAccount(userId);

	BigDecimal currentBalance = accountResponse.getBalance();

	logger.debug("AccountController.increaseBalance: current balance='" + currentBalance + "'.");

	if (amount > 0) {

		BigDecimal newBalance = currentBalance.add(new BigDecimal(amount));
		logger.debug("AccountController.increaseBalance: new balance='" + newBalance + "'.");

		accountResponse.setBalance(newBalance);
		this.service.saveAccount(accountResponse);
		return new ResponseEntity<Double>(accountResponse.getBalance().doubleValue(), getNoCacheHeaders(), HttpStatus.OK);

	} else {
		// amount can not be negative for increaseBalance, please use
		// decreaseBalance
		return new ResponseEntity<Double>(accountResponse.getBalance().doubleValue(), getNoCacheHeaders(), HttpStatus.EXPECTATION_FAILED);
	}

}