Home > Net >  POST request endpoint using Java/Spring-boot
POST request endpoint using Java/Spring-boot

Time:04-08

I'm trying to make a post request endpoint to send some form values to my database. When I test the request with postman, I get a 400 bad request error and this message in my console

JSON parse error: Cannot deserialize value of type abcd.Model.Quote from Array value (token JsonToken.START_ARRAY);

this is my controller:

    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping("/createQuote")
    public boolean newQuote(@RequestBody Quote newQuote) {
    return quoteDB.saveQuote(newQuote);
};

my model:

public class Quote {
public int id;
public String type_building;
public int numElevator;
public String typeService;
public double total;
public double totalElevatorPrice;

public Quote(
        int _id,
        String _type_building,
        int _numElevator,
        String _typeService,
        double _total,
        double _totalElevatorPrice
){
    this.id = _id;
    this.type_building = _type_building;
    this.numElevator = _numElevator;
    this.typeService = _typeService;
    this.total = _total;
    this.totalElevatorPrice = _totalElevatorPrice;

}
public String getType_building() { return type_building; }
public void setType_building(String type_building) { this.type_building = type_building; }

public int getNumElevator() { return numElevator; }
public void setNumElevator(int numElevator) { this.numElevator = numElevator; }

public String getTypeService() { return typeService; }
public void setTypeService(String typeService) { this.typeService = typeService; }

public double getTotal() { return total; }
public void setTotal(int total) { this.total = total; }

public double getTotalElevatorPrice() { return totalElevatorPrice; }
public void setTotalElevatorPrice(int totalElevatorPrice) { this.totalElevatorPrice = totalElevatorPrice; }

}

and this is the function saveQuote where I try to post to the database:

    public boolean saveQuote(Quote newQuote){
    PreparedStatement getData;
    try {
        Connection c;
        c  = this.db.connectDB();
        getData = c.prepareStatement("INSERT INTO Quote VALUES ("  "'"  newQuote.getType_building()   "'"  ","   "'"  newQuote.getNumElevator()   "'"   ","   newQuote.getTypeService()   ","   newQuote.getTotal()   ","   newQuote.getTotalElevatorPrice()   ")");
        getData.executeUpdate();
        return true;
    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}

Thank you for your help.

UPDATE: this is the POST request im sending in postman:

[
{
    "id": 2,
    "type_building": "commercial",
    "numElevator": 10,
    "typeService": "standard",
    "total": 100000.0,
    "totalElevatorPrice": 1.0E7
}
]

CodePudding user response:

@PostMapping("/createQuote")
public boolean newQuote(@RequestBody Quote newQuote) {

Your controller expects as input a single object { ... } representing a Quote object as you have defined it in your code.

Cannot deserialize value of type abcd.Model.Quote from Array value (token JsonToken.START_ARRAY);

But you provide as input with the message that you send an array of objects [ {...}, {...}].

Your request must be without [ ] because those should be used if your controller expected an array of objects.

so it should be

{
    "id": 2,
    "type_building": "commercial",
    "numElevator": 10,
    "typeService": "standard",
    "total": 100000.0,
    "totalElevatorPrice": 1.0E7
}
  • Related