Mitchel Abrahams
Mitchel Abrahams

Reputation: 42

Spring boot post rest call "Required request body is missing"

I have a simple angular school project with a spring boot backend. Whenever I try to send something from the front to the backend via a rest call I'm getting a "Required request body is missing"

Front-end code

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 class in front-end

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

Back-end code

@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 in back-end

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

the exact output is

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)]

I have no idea why the request body is not found. Maybe I'm forgetting something, some kind of setting or configuration somewhere, or maybe the serialization isn't working like I'm expecting... Hopefully, someone can help.

Upvotes: 0

Views: 1462

Answers (1)

Mitchel Abrahams
Mitchel Abrahams

Reputation: 42

Solved:

Module was missing a WebConfig to verify cors. Added it to the project and works as expected.

Upvotes: 1

Related Questions