Home > Software engineering >  Spring @RequestBody Mapping maps all attributes to null values from clean camelCase POST Postman req
Spring @RequestBody Mapping maps all attributes to null values from clean camelCase POST Postman req

Time:12-20

I have a backend made with Spring. In one of my controllers, i have a POST Request which receive data from a DTO which is implemented. I use @Data annotation with Lombok, and the problem doesn't come from here... Because i try whithout it too and it's doesn't work also. When i send a POST request from Postman with clean Json formated with camelCase, my controller receive "null" data from my DTO... I don't understand why. Can you give advices or help please ? Thanks

MY DTO

import lombok.Data;

@Data
public class TransactionSendPaymentToSomeOneDto {

  private String connectionEmail;
  private String connectionFirstname;
  private String connectionLastname;
  private String connectionIban;
  private String descriptionTransaction;
  private Double amountSendMoneyValue;

}

MY CONTROLLER

@Slf4j
@RestController
@CrossOrigin("http://localhost:4200")
@RequestMapping("/transactions")
public class TransactionController {

  @Autowired
  private TransactionService transactionService;

  @PostMapping("/{idUserSessionEnv}/payment")
  public ResponseEntity<Transaction> sendPaymentToSomeone(@PathVariable("idUserSessionEnv") Long idUserSessionEnv, @RequestBody TransactionSendPaymentToSomeOneDto selectedSendMoneyValue){
    try{
      log.info("RECEIVING DATA FROM FRONT-END: ID USER = " idUserSessionEnv  " IBAN = "   selectedSendMoneyValue.getConnectionIban()   "  AMOUNT TRANSACTION = "  selectedSendMoneyValue.getAmountSendMoneyValue());
      return ResponseEntity.ok(transactionService.sendPaymentToSomeone(idUserSessionEnv, selectedSendMoneyValue));
    }catch (NoSuchElementException nse){
      return ResponseEntity.noContent().build();
    }
  }

POST request from postman

CodePudding user response:

You are sending wrong payload.

The correct payload will look like this:

{
    "connectionEmail":"[email protected]",
    "connectionFirstname":"Axel",
    "connectionLastname":"Vega",
    "connectionIban":"2222",
    "amountSendMoneyValue":999,
    "descriptionTransaction":"Essai de transaction"
}

CodePudding user response:

Did you try to add @ResponseBody bellow the @PostMapping("/{idUserSessionEnv}/payment") ?

  • Related