2

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 :/

1 Answer 1

3

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

You need to remove offer tag.

{
    "title":"TheBestOffer",
    "address": {
      "city": "Warsaw"
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

But why it fill title property?
I'm not sure why is setting the title property (I did some test and not set the value), but this is the correct way of send a request body.
What if a use case wants us to have the offer wrapper in request json as well. How can we handle that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.