Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Required request body is missing on a method that accepts request body with proper request

I’m writing a Spring Boot application (using MVC pattern). I have this class:

package com.javatechnologies.springdemo.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Car {
    @Id
    @GeneratedValue
    private Integer id;
    private String brand;
    private String model;
    private double price;
    private String owner;
    private int idOwner;
    
    public Car() {
        //super();
    
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getModel() {
        return model;
    }
    public void setModel(String model) {
        this.model = model;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public int getIdOwner() {
        return idOwner;
    }
    public void setIdOwner(int idOwner) {
        this.idOwner = idOwner;
    }
    
    

}

and I’m trying to sent a POST request from Postman to create a new record of Car and add it to my database.
This is the request:
localhost:8080/addnewcar/?id=120&brand=bmw&model=x5&price=20000&owner=fyodor&idOwner=2
Except I cannot get it to work. Postman says:

{
    "timestamp": "2022-01-16T15:12:43.518+00:00",
    "status": 400,
    "error": "Bad Request",
    "message": "",
    "path": "/addnewcar/"
}

and my Spring Boot application says:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

2022-01-16 17:12:43.516  WARN 11792 --- [io-8080-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public void com.javatechnologies.springdemo.controller.Controller.addNewCar(com.javatechnologies.springdemo.entity.Car)]

even though the method in my Controller accepts a @RequestBody:

@PostMapping("/addnewcar")
public void addNewCar(@RequestBody Car car) {
   discordService.addNewCar(car);
}

I have zero idea what it wants from me. I’ve tried putting breakpoints in both the controller and the service, but IntelliJ never stops at them. Help is greatly appreciated, many thanks in advance.

>Solution :

The issue is coming from your request . You should send the car as a payload in body instead of using query string localhost:8080/addnewcar/? id=120&brand=bmw&model=x5&price=20000&owner=fyodor&idOwner=2

You should use the url localhost:8080/addnewcar using POST method. You don’t need to specify the id since it’s autogenerated. You can try with a json payload

{
'brand': 'bmw',
'model': 'x5',
'price': 200000,
'owner' : 'fyodor',
'idOwner' : 2
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading