0

I am using Spring RestTemplate to communicate with an provided REST service that delivers JSON. To Map the response I am using Jaxon, but I will gladly switch to anything else that works.

I would like to create an POJO that contains sub-content of the delivered data but in a different Structure.

It boils down to this:

Source: { "a": "val_a", "b" : {"c" : "val_c", "d": "val_d"}}

@JsonIgnoreProperties(ignoreUnknown = true)
class Foo {
   // should contains the content of `"a": "val_a"`
   // but contains null
   private Baa;

   // getter and setter
}

@JsonIgnoreProperties(ignoreUnknown = true)
class Baa {
  private String a;

  // getter and setter
}


// This should be the operation that is done internally by Spring when calling 
// ResponseEntity<Foo>response = restTemplate.exchange(url, HttpMethod.GET, entity, Foo.class);
// response.getBody();
private Foo read(String s) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    return mapper.readValue(s, Foo.class);
}    

The result of the deserialization is an empty Baa object. The actual JSON and POJO Object structure is more complex but this sums it up.

Any Technology that achieves this would be welcome. The only possibility I came up with is deserializing the JSON in the provided structure and write a Converter class that generates the desired Object but I was hoping to avoid this.

----- Update/clarification ------ The problem is, that the property a should be mapped within class Baa, which lies within Foo but is provided in the root path (is this the right term?) of the provided JSON objekt

Aracos
  • 155
  • 1
  • 3
  • 8

2 Answers2

1
public class Foo {

  private String a;
  private B b;

  // getters setters

}

public class B {

  private String c;
  private String d;

  // getters setters

}

Should map with no additional annotations with your code. If you're having a particular code with a non trivial example then post your actual code in whole.

Update on your clarification: no you can't do that with annotations as I said in my comment. You will have to write the custom deserialiser. Check out this answer: Jackson: is it possible to include property of parent object into nested object?

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • This deserializes the JSON provided, but i would like to map the String a within the class B without the need of a converter class – Aracos Jul 05 '17 at 06:46
  • Jackson will do this automatically if configured correctly. Be sure to assign a variable name to `Baa` and have correct getter and setter. Additionally you can use the ObjectMapper of Jackson to deserialize. – Nico Jul 05 '17 at 07:13
  • @Aracos no, you can't do that with annotations. You will need a custom deserializer: https://stackoverflow.com/questions/19191223/jackson-is-it-possible-to-include-property-of-parent-object-into-nested-object – Strelok Jul 05 '17 at 07:25
0

If you don't want to write the java bean that rappresent the JSON structure, you have to use a different library. Jackson forces you to create a java structure that reflects the JSON structure. In my opinion Jackson works great and i suggest you to use it, but the alternative could be JSON library.

With this one you can select only the element you want from the json, and map it to the bean you want.

Little example:

JSONObject response =  new JSONObject("{\"a\": \"val_a\", \"b\" : {\"c\" : \"val_c\", \"d\": \"val_d\"}}");

JSONObject bObject =  response.getJSONObject("b");
String cElement = (String) elenco.get("c");

The value of bObject is {"d":"val_d","c":"val_c"}, and the value of cElement is val_c

This libray uses JSONObject and JSONArray generic objects, to map the content of the json to a java object.

amicoderozer
  • 2,046
  • 6
  • 28
  • 44