SpringBoot: dates are null in PUT petition to insert an object in a database

22 Views Asked by At

Trying to do a backend with Springboot. Now I'm trying to do petitions with postman to see if code works. For now, GET & POST are working. The problem comes whenever I'm doing a PUT petition with dates. The dates are null when data arrives to the Controller. This is the method save in the Controller:

@Operation(summary = "Save", description = "Method that saves a Loan")
    @RequestMapping(path = { "" }, method = RequestMethod.PUT)
    public void save(@RequestBody LoanDto dto) {
        //loan_start and loan_return are null at this point
        this.loanService.save(dto);
    }

The others atributes of the class are received fine when asking the petition. This is the body of the petition I'm working with in Postman:

{
  "id": 35,
  "game": {
    "id": 25,
    "title": "Aventureros al tren",
    "age": 8,
    "category": {
      "id": 3,
      "name": "Familiar"
    },
    "author": {
      "id": 1,
      "name": "Alan R. Moon",
      "nationality": "US"
    }
  },
  "client": {
    "id": 1,
    "name": "John Doe"
  },
  "loan_start": "2023-01-04",
  "loan_returned": "2023-01-15"
}

In Loan class, I have the next lines of code:

@Entity
@Table(name = "loan")
public class Loan {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "game_id", nullable = false)
    private Game game;

    @ManyToOne
    @JoinColumn(name = "client_id", nullable = false)
    private Client client;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Column(name = "loan_start", nullable = false)
    private LocalDate loan_start;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @Column(name = "loan_returned", nullable = false)
    private LocalDate loan_returned;

//getters and setters for all attributes down here
// ...
}

And this is the service implementation:

@Override
    public void save(LoanDto data) {
        Loan loan = new Loan();

        BeanUtils.copyProperties(data, loan, "id", "game", "client"); 
        this.loanRepository.save(loan);
    }

The class LoanDto is exactly the same as this one The error I get on Eclipse when I ask the PUT petition is the next one:

NULL not allowed for column "LOAN_RETURNED"; SQL statement: insert into loan (id, client_id, game_id, loan_returned, loan_start) values (default, ?, ?, ?, ?) [23502-214]

In Postman, I get a error 500.

I have tried to change the dates, but it still doesn't work.

I'm working with JAVA 21, using CrudRepository class to deal with SQL operations and BeanUtils to copy properties from objects to deal with.

I have tried PUT petitions using Postman. Whenever I'm doing PUT petitions without dates, it works, but it stops working when I try to add dates, as they may be wrong, but I don't know where the error is.

0

There are 0 best solutions below