Spring Boot发布休息呼叫“缺少所需的请求正文”

时间:2019-12-30 14:33:33

标签: java angular spring rest spring-boot

我有一个带有Spring Boot后端的简单的斜角学校项目。每当我尝试通过rest调用从前端向后端发送某些消息时,都会收到“缺少所需的请求正文”

前端代码

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

后端的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)]

我不知道为什么找不到请求正文。也许我忘记了某些地方的某种设置或配置,或者序列化无法按我期望的那样工作。希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:0)

已解决:

模块缺少用于验证cors的WebConfig。将其添加到项目中并按预期工作。