Piotr Żak
Piotr Żak

Reputation: 2403

SpringBoot JACKSON configuration for JSON nested objects received by Spring MVC

I have created Spring Boot app, in this app I have

@RestController
public class OfferController {

    @RequestMapping(value = "/saveOffer", method = RequestMethod.POST)
    public void saveOffer(@RequestBody Offer offer) {
    //...
    }
}

Offer class contain nested property of Address type

public class Offer {

   private String title;
   private Address address;

   //... getters setters etc
}

When I'm sending JSON from UI

{
  "offer": {
    "title":"TheBestOffer",
    "address": {
      "city": "Warsaw"
    }
  }
}

My REST controller receives Offer, Address property is null but title property contains value "TheBestOffer" (as it was sended).

As I assume JACKSON delivered with Spring boot require some extra configuration for nested objects? I have tried to do this but it didn't work :/

Upvotes: 2

Views: 1954

Answers (1)

reos
reos

Reputation: 8324

Spring does this automatically, i think your problem is with the json.

You need to remove offer tag.

{
    "title":"TheBestOffer",
    "address": {
      "city": "Warsaw"
    }
}

Upvotes: 3

Related Questions