I am trying to do HTTP PUT using org.apache.cxf.jaxrs.client.WebClient. Server expects the payload in format:
{"key":"mainkey","value":{\"intKey1\":\"value1\",\"intKey2":\"value2\"},"ttl":"100"}
however, I am ending up sending as below:
{"key":"mainkey","value":{"intKey1":"value1","intKey2":"value2"},"ttl":"100"}
(Note that internal key value needs an escape quotes)
Here is my code snippet:
private void callClient4(RestClient client) {
KeyValueMessage<String, Map<String, String>> kv = new KeyValueMessage<String, Map<String, String>>();
kv.setKey("mainkey");
kv.setTtl("100");
Map<String, String> map = new HashMap<>();
map.put("intKey1", "value1");
map.put("intKey2", "value2");
kv.setValue(map);
Response ret = client.getClient().accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).sync()
.put(Entity.json(kv));
}
What could be done to change the format as expected by server?
used com.fasterxml.jackson.databind.ObjectMapper to solve the problem
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.writeValueAsString(map);