Trying to post JSON to a method end up with this error - Required String parameter 'name' is not present

1k Views Asked by At

I know that this question mind be asked before couldn't find exact same problem and I'm new to spring technology so be gentle please

I'm trying post an object using postman which is supposed to be added on my DB

object constructor looks like this

public PostedProduct(String name, long price) {
  super(name, price, UUID.randomUUID());
  }

Mapping function is this

static final String path = "/Products";

@RequestMapping(method = RequestMethod.POST , value = path) 
public void setProducts(@RequestParam("name") String name, @RequestParam("price") long price){
  service.setProduct(new PostedProduct(name,price));
}

SetProduct is a function I use to add Object to my database debugger is not reaching to that statement. This is what i did to post my JSON object

Header

JSON

the following is the error message

"message": "Required String parameter 'name' is not present",

I tried to change function to this and tried some other combinations

@PostMapping(path)
public void setProducts(@RequestBody PostedProduct product)
{
   service.setProduct(product);
}

Nothing changed except for the error message

"message": "Required request body is missing: public void  Controllers.MarketController.setProducts(Moldels.ProductModel.PostedProduct)"

I'm not looking for a cheap solution I'm trying to learn why. If anyone is willing to help i can provide more detail

3

There are 3 best solutions below

0
On BEST ANSWER

I found a solution after 2 days of searching for answer

Apparently you need to put a Response entity to make sure Spring to handle POST command correctly

@RequestMapping(value = "/Product" , method = RequestMethod.POST , consumes = MediaType.APPLICATION_JSON_VALUE)
   public ResponseEntity<Product> setPruduct(@RequestBody PostedProduct product )
   {
      service.setProduct(product);
      return new ResponseEntity<Product>(HttpStatus.OK);
   }

Like this or simply annotating HTTP status is ok is enough like this

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/Product" , method = RequestMethod.POST , consumes = MediaType.APPLICATION_JSON_VALUE)
public void setPruduct(@RequestBody PostedProduct product )
{
service.setProduct(product);
}

sorry for the trouble

1
On

the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object

Spring automatically deserializes the JSON into a Java type assuming an appropriate one is specified. By default, the type we annotate with the @RequestBody annotation must correspond to the JSON sent from our client-side controller.

make sure that your class respect the java bean specification

3
On

If you use @RequestParam your URI should look like this (it means you are not using a HTTP body) :

http://localhost:8080/Products?name=book&price=12000

If you use @RequestBody with your HTTP body example your PostedProduct class should look like this (URI is now http://localhost:8080/Products) :

public class PostedProduct {

    private String name;
    private long price;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public long getPrice() {
        return price;
    }

    public void setPrice(long price) {
        this.price = price;
    }
}

Notice that a constructor without arguments and setter methods are mandatory if you want Spring to deserialize your JSON correctly