0

I have the following example object:

{
  "manufacturer": "bla",
  "model": "5901",
  "metadata": { 
    "CommercialName" : "bla bla",
    "Intername Name" : "bla bla"
   },
  "features": [
    "a"
  ],
  "profiles": 1
}

I wish to store the below metadata part without parsing it directly into Postgres SQL's "jsob" type.

  "metadata": { 
    "CommercialName" : "bla bla",
    "Intername Name" : "bla bla"
   },

The DTO class looks like below now.

JsonInclude(JsonInclude.Include.NON_NULL)
public class device {

    private Long            id;
    private String          manufacturer;
    private String          model;
    private Integer         profiles;
    private String          metadata;
    private List<String>    feature;

+all the gettors/settors
}

However, I am getting an error. I do not know how to represent "metadata" (which in theory can contain any client specific JSON object) without having a separate object for it.

julianfperez
  • 1,726
  • 5
  • 38
  • 69

2 Answers2

0

You can use org.json.JSONObject to represent metadata instead of using string. Then use toString() on the JSONObject to get JSON text.

https://stleary.github.io/JSON-java/org/json/JSONObject.html

I am adding code snippet of controller.

@RestController
public class DeviceController {
    @RequestMapping(value="/add/device", method=RequestMethod.POST, produces={MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<String> postDeviceData(@RequestBody Device device) {
        // The device object will be filled with data sent from Http request
        return new ResponseEntity<>("Done", HttpStatus.OK);
    }
}
Vijay k
  • 5
  • 7
  • I tried the below, but when I test it the object is empty (the map is 0 objects) private JSONObject metadata; – user1920407 May 19 '18 at 13:31
  • Is the **device** object filled by controller in spring? – Vijay k May 19 '18 at 15:56
  • yes it is. and I pass the entire DTO object for the request body @RequestMapping(value = "/device", method = RequestMethod.POST) public Long createDevice(@RequestBody DeviceMasterDTO deviceMasterDTO) { – user1920407 May 20 '18 at 02:24
  • @user1920407 You can try using Map instead of Map, ?>. When saving to DB as json string you might have to construct json string from Map, where as JSONObject gives you that as built in method. – Vijay k May 20 '18 at 02:58
0

So the solution to my problem was from an hint form "Hitobat" who suggested using Map<String,String>, I changed it slightly to Map<?,?> now I see that I get the data, even when I pass a slightly more elaborate jSON object.

THANKS!!

{
  "manufacturer": "dada",
  "model": "kaka",
  "metadata": { 
    "CommercialName" : "a3 sf",
    "Internalname Name" : "asdf",
    "timbacktu" : {
        "bumdarabu" : 345,
        "aadfsq" : 2.3
    }
   },
  "features": [
    "asdfsd"
  ],
  "profiles": 1
}

The DTO class

@JsonInclude(JsonInclude.Include.NON_NULL)
public class device {

    private Long            id;
    private String          manufacturer;
    private String          model;
    private Integer         profiles;
    private Map<?,?>        metadata;
    private List<String>    features;

}