BetaRide
BetaRide

Reputation: 16844

No properties set in POST to spring @RestController

I have spring controller marked with RestController. If I do a POST with a Json object the properties of the model class Company are not populated, e.g. the name property is null.

If I reqeust the request body in the save() method I do get a Json string which has a name property, which means I'm sure that the json body of the POST request gets transmited.

Is there something I have to do to make spring deserialize the Json string into the company argument of the save() method?

Controller:

@RestController
@RequestMapping("/company")
public class CompanyResource {

    @Resource
    private CompanyService companyService;

    @RequestMapping(method = RequestMethod.POST)
    public Company save(Company company) {
        return companyService.save (company);
    }

}

Company model class: @Entity

public class Company {

    @Id
    private long id;

    private String name;

    // public setters and getters
}

Upvotes: 2

Views: 372

Answers (1)

IgorekPotworek
IgorekPotworek

Reputation: 1335

You need @RequestBody annotation:

public Company save(@RequestBody Company company) {
    return companyService.save (company);
}

Upvotes: 2

Related Questions