1

In my spring boot application, I get a request with flat json like this.

{
   "firstname" : "a",
   "lastname" : "b",
   "street": "non main st",
   "city": "NY"
}

I want to map to Java object like this.

class Person {
   String firstname;
   String lastname;
   Address address;
}

class Address {
    String street;
    String city;
}

Is this possible?


Do note that this is for my spring-boot application which automatically deserialize this. I do not use Object Mapper directly.


Controller:

My REST controller is nothing special.

@RestController
public class TestController{
    @GetMapping("/")
    public void justPrint(@RequestBody Person person){
        System.out.println(person);
    }
}
RamPrakash
  • 2,218
  • 3
  • 25
  • 54
  • Does this answer your question? [Wrapping Json fields into instance variable of a pojo](https://stackoverflow.com/questions/54370637/wrapping-json-fields-into-instance-variable-of-a-pojo) – Michał Ziober Nov 25 '19 at 23:11
  • @MichałZiober, I am afraid it is not. I have updated – RamPrakash Nov 25 '19 at 23:15
  • Behind the scene `ObjectMapper` is used anyway. `SpringBoot` to deserialise `JSON` payload to `Java` `POJO` model uses by default `Jackson` and it's classes set. Related link contains just an example with `ObjectMapper` to check it out with copy paste. – Michał Ziober Nov 25 '19 at 23:23

1 Answers1

0

You could try to use @JsonUnwrapped on your Address field:

class Person {
   String firstname;
   String lastname;
   @JsonUnwrapped
   Address address;
}

UPDATE:

Although GET can work as well, you should really use POST or PUT HTTP method mapping in your controller e.g. here I'm gonna use @PostMapping:

@RestController
public class TestController{
    @PostMapping("/")
    public void justPrint(@RequestBody Person person){
        System.out.println(person);
    }
}

And you should send the data when running curl command:

curl -X POST -H "Content-Type: application/json" -d '{"firstname": "John", "lastname": "Smith", "street": "1st Str.", "city": "London"}' http://localhost:8080/

Here I am assuming that your service is running on port 8080. And one more thing, if you do not have toString() implemented for the Person class, do write one, since only class name would be displayed with System.out.println.

Igor
  • 1,406
  • 2
  • 23
  • 45
  • Does it work in Spring boot without using object mapper? I do not think so – RamPrakash Nov 25 '19 at 23:12
  • You don't need to use `ObjectMapper` directly. For each request, Spring will try to find the appropriate `HttpMessageConverter` to use for read and write of JSON content. In this case it will use the [MappingJackson2HttpMessageConverter](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/json/MappingJackson2HttpMessageConverter.html), which will do all serialization/deserialization for you. – Igor Nov 25 '19 at 23:37
  • Hm, strange since this should work. How is your controller and method for recieving this JSON payload defined? Can you update your question with controller code – Igor Nov 26 '19 at 00:30
  • I added the controller. – RamPrakash Nov 26 '19 at 15:05