Ne AS
Ne AS

Reputation: 1550

Test rest service having RequestParam using postman

I want to test my REST service in order to save a product having a certain category (manyToOne) with Postman:

This is the body of my request:

{
    "categoryId": 36,
    "product": {
        "code": "code1",
        "name": "product1",
        "price": 20
    }
}

And this is how the signature of my REST service method looks like:

@RequestMapping(value = "/addProduct", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ProductBean> add(@RequestParam(value ="categoryId") Long id, @RequestParam(value = "product") ProductBean productBean)

I put in Postman my URL with the /addProduct at the end, then I choose POST. In the body tab, I choose raw and select the JSON (application json). When I send the request I got HTTP 400.

How can test this without error in Postman?

Edit

I want to test it using postman to be sure that my REST is working before adding the front part. This is how I will send the data from the front

add: function (product, id, successCallBack, failureCallBack) {
        $http({
            method: 'POST',
            url: "/addProduct",
            params: {
                product: product,
                categoryId: id
            },
            headers: {'Content-Type': 'application/json'}
        }).then(successCallBack, failureCallBack);
    }

Upvotes: 2

Views: 28312

Answers (1)

jny
jny

Reputation: 8057

Your method signature is incorrect. @RequestParam is the parameter in the uri, not the body of the request. It should be:

 public ResponseEntity<ProductBean> add(MyBean myBean)

where MyBean has to properties: id and product or

public ResponseEntity<ProductBean> add(@ModelAttribute(value ="categoryId") Long id, @ModelAttribute(value = "product") ProductBean productBean)

For more on mapping requests see Spring documentation

If you want to stick to your original mapping, then everything should be passed in the query string and nothing in the body. The query string will look something like that:

/addProduction?categoryId=36&product={"code":"code1",...}

Upvotes: 7

Related Questions