第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

替代響應(yīng) spring boot api 休息

替代響應(yīng) spring boot api 休息

守候你守候我 2024-01-05 16:13:03
我是 REST API 和 Spring Boot 的初學(xué)者。我對(duì)如何處理請(qǐng)求可能有的不同響應(yīng)有疑問(wèn)。例如,如果我發(fā)布信用卡數(shù)據(jù){    "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 的對(duì)象。如果date_expiration過(guò)期或者identification_card與客戶(hù)端不對(duì)應(yīng)怎么辦?它們是在服務(wù)中解析的邏輯驗(yàn)證,可以觸發(fā)不同的響應(yīng)。我應(yīng)該如何處理它們?
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

在這里,您使用與請(qǐng)求正文和響應(yīng)正文相同的對(duì)象。這不是標(biāo)準(zhǔn)做法。

您應(yīng)該有單獨(dú)的請(qǐng)求/響應(yīng)對(duì)象。在請(qǐng)求對(duì)象中,您應(yīng)該只從用戶(hù)那里獲得您需要的信息。但是在響應(yīng)對(duì)象中,您應(yīng)該包含要在響應(yīng)中發(fā)送的信息以及驗(yàn)證信息,例如錯(cuò)誤詳細(xì)信息,其中包括錯(cuò)誤代碼和錯(cuò)誤描述,您可以使用它們向用戶(hù)顯示驗(yàn)證錯(cuò)誤。

希望這可以幫助。


查看完整回答
反對(duì) 回復(fù) 2024-01-05
?
米脂

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊

好吧,如果date_expiration過(guò)期或identification_card不符合客戶(hù)的要求,這就是商業(yè)失敗。

我喜歡用 來(lái)表示業(yè)務(wù)錯(cuò)誤HTTP 422 - Unprocessable Entity。

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

正如我所說(shuō),這種情況是業(yè)務(wù)失?。ㄐ庞每ㄒ堰^(guò)期或?qū)Ξ?dāng)前用戶(hù)不適用)。

這是一個(gè)例子。會(huì)是這樣的:

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());

? ? }


}

如果信用卡過(guò)期,JSON 將呈現(xiàn)為:


{

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

? "reason": "The card is expired"

}


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 2 回答
  • 0 關(guān)注
  • 162 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)