亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

替代響應 spring boot api 休息

替代響應 spring boot api 休息

守候你守候我 2024-01-05 16:13:03
我是 REST API 和 Spring Boot 的初學者。我對如何處理請求可能有的不同響應有疑問。例如,如果我發布信用卡數據{    "number": "3434 3434 3434 3434",    "date_expiration": "09-19",    "identification_card": 23232323}然后在@RestController中@PostMapping("/card")public ResponseEntity<CreditCard> payCard(@Valid @RequestBody CreditCard creditCard){      CreditCard creC = cardService.registerCard(creditCard);      return new ResponseEntity<>(creC, HttpStatus.OK);    }在本例中,我返回 ResponseEntity 的對象。如果date_expiration過期或者identification_card與客戶端不對應怎么辦?它們是在服務中解析的邏輯驗證,可以觸發不同的響應。我應該如何處理它們?
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

在這里,您使用與請求正文和響應正文相同的對象。這不是標準做法。

您應該有單獨的請求/響應對象。在請求對象中,您應該只從用戶那里獲得您需要的信息。但是在響應對象中,您應該包含要在響應中發送的信息以及驗證信息,例如錯誤詳細信息,其中包括錯誤代碼和錯誤描述,您可以使用它們向用戶顯示驗證錯誤。

希望這可以幫助。


查看完整回答
反對 回復 2024-01-05
?
米脂

TA貢獻1836條經驗 獲得超3個贊

好吧,如果date_expiration過期或identification_card不符合客戶的要求,這就是商業失敗。

我喜歡用 來表示業務錯誤HTTP 422 - Unprocessable Entity

如果您想在控制器中返回不同的對象,您可以將返回對象從 更改ResponseEntity<CreditCard>為,但如果目的是返回錯誤,我更喜歡在帶注釋的方法中使用 a 。ResponseEntity<Object>ExceptionHandlerControllerAdvice

正如我所說,這種情況是業務失敗(信用卡已過期或對當前用戶不適用)。

這是一個例子。會是這樣的:

CardService.java

@Service

public class CardService {


? ? // ..


? ? public CreditCard registerCard(CreditCard card) throws BusinessException {

? ? ? ? if(cardDoesntBehaveToUser(card, currentUser()))) // you have to get the current user

? ? ? ? ? ? throw new BusinessException("This card doesn't behave to the current user");


? ? ? ? if(isExpired(card)) // you have to do this logic. this is just an example

? ? ? ? ? ? throw new BusinessException("The card is expired");


? ? ? ? return cardRepository.save(card);

? ? }


}

CardController.java


@PostMapping("/card")

public ResponseEntity<Object> payCard(@Valid@RequestBody CreditCard creditCard) throws BusinessException {

? ? CreditCard creC = cardService.registerCard(creditCard);

? ? return ResponseEntity.ok(creC);

}

BusinessException.java


public class BusinessException extends Exception {


? ? private BusinessError error;


? ? public BusinessError(String reason) {

? ? ? ? this.error = new BusinessError(reason, new Date());

? ? }


? ? // getters and setters..

}

BusinessError.java


public class BusinessError {


? ? private Date timestamp

? ? private String reason;


? ? public BusinessError(String Reason, Date timestamp) {

? ? ? ? this.timestamp = timestamp;

? ? ? ? this.reason = reason;

? ? }


? ? // getters and setters..

}

MyExceptionHandler.java


@ControllerAdvice

public class MyExceptionHandler extends ResponseEntityExceptionHandler {


? ? // .. other handlers..


? ? @ExceptionHandler({ BusinessException.class })

? ? public ResponseEntity<Object> handleBusinessException(BusinessException ex) {

? ? ? ? return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(ex.getError());

? ? }


}

如果信用卡過期,JSON 將呈現為:


{

? "timestamp": "2019-10-29T00:00:00+00:00",

? "reason": "The card is expired"

}


查看完整回答
反對 回復 2024-01-05
  • 2 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號