[英]Spring boot post rest call "Required request body is missing"
我有一個帶有 Spring Boot 后端的簡單的有角度的學校項目。 每當我嘗試通過休息調用從前端向后端發送內容時,我都會收到“缺少所需的請求正文”
前端代碼
export class TestController {
private usersUrl: string;
private authorizationMessage: AuthorizationMessage;
constructor(
private httpClient: HttpClient
) {
this.usersUrl = 'http://localhost:8080/fraud/';
}
public testDatabase() {
this.authorizationMessage = new AuthorizationMessage();
this.authorizationMessage.bitMap = 1;
this.authorizationMessage.carAcceptorName = 'mitchel';
this.authorizationMessage.cardAcceptorId = 'cardAcceptorId';
this.authorizationMessage.cardAcceptorTerminalId = 8762344;
this.authorizationMessage.cardSequenceNumber = 12;
this.authorizationMessage.checkInformation = 'check info test';
this.authorizationMessage.messageTypeIdentifier = 3;
this.authorizationMessage.posDataCode = 'posDataCode';
this.authorizationMessage.primaryAccountNumber = 29485819;
this.authorizationMessage.processingCode = 90;
this.authorizationMessage.responseCode = 0;
this.authorizationMessage.systemTraceAuditNumber = 65;
this.authorizationMessage.transactionAmount = 45;
this.authorizationMessage.transmissionDateAndTime = new Date();
console.log(this.authorizationMessage);
return this.httpClient.post(this.usersUrl + 'authorizeTransaction', this.authorizationMessage).subscribe();
}
}
前端的 AuthorizationMessage 類
export class AuthorizationMessage {
messageTypeIdentifier: number;
bitMap: number;
processingCode: number;
transactionAmount: number;
transmissionDateAndTime: Date;
systemTraceAuditNumber: number;
responseCode: number;
cardAcceptorId: string;
posDataCode: string;
cardAcceptorTerminalId: number;
carAcceptorName: string;
primaryAccountNumber: number;
checkInformation: string;
cardSequenceNumber: number;
}
后端代碼
@RestController
@RequestMapping(value= "/fraud", produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j
public class FraudController {
private RuleEngine ruleEngine;
@Autowired
public FraudController(RuleEngine ruleEngine){
this.ruleEngine = ruleEngine;
}
@PostMapping(value = "/authorizeTransaction", consumes = MediaType.APPLICATION_JSON_VALUE)
public void authorizeTransaction(@RequestBody @Valid AuthorizationMessage authorizationMessage){
TransactionContext transactionContext = getTransactionContext(authorizationMessage);
transactionContext = ruleEngine.evaluateTransaction(transactionContext);
updateTransactionHistory(authorizationMessage);
}
}
后台授權消息
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AuthorizationMessage implements Serializable {
private Integer messageTypeIdentifier;
private Integer bitMap;
private Integer processingCode;
private Long transactionAmount;
private LocalDateTime transmissionDateAndTime;
private Integer systemTraceAuditNumber;
private Integer responseCode;
private String cardAcceptorId;
private String posDataCode;
private Integer cardAcceptorTerminalId;
private String carAcceptorName;
private Long primaryAccountNumber;
private String checkInformation;
private Integer cardSequenceNumber;
}
確切的輸出是
2019-12-30 15:09:18.341 WARN 89353 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void nl.han.ica.oose.s1920.g9.frauddetection.controllers.FraudController.authorizeTransaction(nl.han.ica.oose.s1920.g9.frauddetection.core.authorizationmessage.AuthorizationMessage)]
我不知道為什么找不到請求正文。 也許我忘記了某些東西,某處的某種設置或配置,或者序列化沒有像我期望的那樣工作......希望有人可以提供幫助。
解決了:
模塊缺少用於驗證 cors 的 WebConfig。 將其添加到項目中並按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.