How to use Jackson to deserialise an array of objects - java

The Jackson data binding documentation indicates that Jackson supports deserialising "Arrays of all supported types" but I can't figure out the exact syntax for this.
For a single object I would do this:
//json input
{
"id" : "junk",
"stuff" : "things"
}
//Java
MyClass instance = objectMapper.readValue(json, MyClass.class);
Now for an array I want to do this:
//json input
[{
"id" : "junk",
"stuff" : "things"
},
{
"id" : "spam",
"stuff" : "eggs"
}]
//Java
List<MyClass> entries = ?
Anyone know if there is a magic missing command? If not then what is the solution?

First create a mapper :
import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3
ObjectMapper mapper = new ObjectMapper();
As Array:
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
As List:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
Another way to specify the List type:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));

From Eugene Tskhovrebov
List<MyClass> myObjects = Arrays.asList(mapper.readValue(json, MyClass[].class))
This solution seems to be the best for me.

For Generic Implementation:
public static <T> List<T> parseJsonArray(String json,
Class<T> classOnWhichArrayIsDefined)
throws IOException, ClassNotFoundException {
ObjectMapper mapper = new ObjectMapper();
Class<T[]> arrayClass = (Class<T[]>) Class.forName("[L" + classOnWhichArrayIsDefined.getName() + ";");
T[] objects = mapper.readValue(json, arrayClass);
return Arrays.asList(objects);
}

try this
List<MyClass> list = mapper.readerForListOf(MyClass.class).readValue(json)

First create an instance of ObjectReader which is thread-safe.
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader objectReader = objectMapper.reader().forType(new TypeReference<List<MyClass>>(){});
Then use it :
List<MyClass> result = objectReader.readValue(inputStream);

I was unable to use this answer because my linter won't allow unchecked casts.
Here is an alternative you can use. I feel it is actually a cleaner solution.
public <T> List<T> parseJsonArray(String json, Class<T> clazz) throws JsonProcessingException {
var tree = objectMapper.readTree(json);
var list = new ArrayList<T>();
for (JsonNode jsonNode : tree) {
list.add(objectMapper.treeToValue(jsonNode, clazz));
}
return list;
}

try {
ObjectMapper mapper = new ObjectMapper();
JsonFactory f = new JsonFactory();
List<User> lstUser = null;
JsonParser jp = f.createJsonParser(new File("C:\\maven\\user.json"));
TypeReference<List<User>> tRef = new TypeReference<List<User>>() {};
lstUser = mapper.readValue(jp, tRef);
for (User user : lstUser) {
System.out.println(user.toString());
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

here is an utility which is up to transform json2object or Object2json,
whatever your pojo (entity T)
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
*
* #author TIAGO.MEDICI
*
*/
public class JsonUtils {
public static boolean isJSONValid(String jsonInString) {
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(jsonInString);
return true;
} catch (IOException e) {
return false;
}
}
public static String serializeAsJsonString(Object object) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objMapper = new ObjectMapper();
objMapper.enable(SerializationFeature.INDENT_OUTPUT);
objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
StringWriter sw = new StringWriter();
objMapper.writeValue(sw, object);
return sw.toString();
}
public static String serializeAsJsonString(Object object, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objMapper = new ObjectMapper();
if (indent == true) {
objMapper.enable(SerializationFeature.INDENT_OUTPUT);
objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
StringWriter stringWriter = new StringWriter();
objMapper.writeValue(stringWriter, object);
return stringWriter.toString();
}
public static <T> T jsonStringToObject(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper objMapper = new ObjectMapper();
obj = objMapper.readValue(content, clazz);
return obj;
}
#SuppressWarnings("rawtypes")
public static <T> T jsonStringToObjectArray(String content) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper mapper = new ObjectMapper();
obj = mapper.readValue(content, new TypeReference<List>() {
});
return obj;
}
public static <T> T jsonStringToObjectArray(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper mapper = new ObjectMapper();
mapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
obj = mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
return obj;
}

you could also create a class which extends ArrayList:
public static class MyList extends ArrayList<Myclass> {}
and then use it like:
List<MyClass> list = objectMapper.readValue(json, MyList.class);

Related

How to avoid string to number conversion for doubles and numbers expressed in Scientific Notation?

I receive JSON payload which is set of key-value pairs. Value may be either string or number. I have to parse the JSON and store key-value pairs into appropriate varchar2 columns. I should save incoming number exactly as it was presented in the input payload.
But for numbers presented like 1.1E4, 0.00000000000003 and similar I get 11000.0, 3.0E-14 instead.
Is it a way to disable/prevent number conversion to have just string representation instead?
I use FasterXML Jackson implementation.
By the way there is no actual doc available - all sources I found point to http://wiki.fasterxml.com/JacksonHome which is down right now.
I found two similar questions here
Jackson JSON converts integers into strings
Disable the Number to String automatic conversion in jackson
but both require exception when encounter number, which is not my case. I have tried suggested solutions but was unsuccessful in modifying them to fit my task.
Also there is no answer in
https://github.com/FasterXML/jackson-databind/issues/796
Right now I have no specification for input string other than key-value pairs. So just an example:
I may receive something like:
{"a":"text", "b":"35", "c":{"d":"another"}, "e":["array",35], "f":1.1E4, "g":0.00000000000003}
I want string pairs
"a" -> "text", "b" -> "35", "c" -> "{\"d\":\"another\"}", "e" -> "[\"array\",35]", "f" -> "1.1E4"
The simplest conversion way is:
public void test() throws IOException {
Map map = new ObjectMapper().readValue(
"{\"a\":\"text\", \"b\":\"35\", \"c\":{\"d\":\"another\"}, \"e\":[\"array\",35], \"f\":1.1E4, \"g\":0.00000000000003}"
, Map.class);
System.out.println(map);
}
results in:
{a=text, b=35, c={d=another}, e=[array, 35], f=11000.0, g=3.0E-14}
The more accurate way:
public class JsonUtil2 {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static Map<String, String> parse(String json) throws IOException {
ObjectNode objectNode = (ObjectNode) OBJECT_MAPPER.readTree(json);
Map<String, String> result = new HashMap<>(objectNode.size());
objectNode.fields().forEachRemaining(entry -> result.put(entry.getKey(), toJson(entry.getValue())));
return result;
}
private static String toJson(JsonNode jsonNode) {
if (jsonNode.isNumber()) {
if (jsonNode instanceof DoubleNode || jsonNode instanceof FloatNode) {
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
dfs.setMinusSign('-');
DecimalFormat df = new DecimalFormat("#.#", dfs);
df.setMaximumFractionDigits(32);
df.setMaximumIntegerDigits(32);
return df.format(jsonNode.doubleValue());
} else {
return jsonNode.asText();
}
} else if (jsonNode.isValueNode()) {
return jsonNode.asText();
} else {
try {
return OBJECT_MAPPER.writeValueAsString(jsonNode);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
}
which results in:
{a=text, b=35, c={"d":"another"}, e=["array",35], f=11000, g=0.00000000000003}
This is much better, but still differ in f=11000 instead of f=1.1E4.
In your case you want to treat everything as a String, so you need a custom deserialiser which reads JSON Object and JSON Array as String. We can also force Jackson to read Map<String, String> by providing this information using TypeFactory.
Assume our JSON payload looks like below:
{
"a": "text",
"b": "35",
"c": {
"d": "another",
"dd":3.44E3
},
"e": [
"array",
35,
2.3E5
],
"f": 1.1E4,
"g": 0.00000000000003
}
Example code:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JsonTreeApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
SimpleModule everythingIsStringModule = new SimpleModule();
everythingIsStringModule.addDeserializer(String.class, new EverythingIsStringDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(everythingIsStringModule);
MapType mapType = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, String.class);
LinkedHashMap<String, String> map = mapper.readValue(jsonFile, mapType);
map.forEach((k, v) -> System.out.println(k + " => " + v));
}
}
class EverythingIsStringDeserializer extends StringDeserializer {
#Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
if (p.currentToken() == JsonToken.START_OBJECT) {
return _deserializeFromObject(p, ctxt);
}
return super.deserialize(p, ctxt);
}
private String _deserializeFromObject(JsonParser p, DeserializationContext ctxt) throws IOException {
MapType mapType = ctxt.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, String.class);
JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(mapType);
Map<String, String> map = (Map<String, String>) deserializer.deserialize(p, ctxt);
return toString(map);
}
#Override
protected String _deserializeFromArray(JsonParser p, DeserializationContext ctxt) throws IOException {
CollectionType collectionType = ctxt.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
JsonDeserializer<Object> deserializer = ctxt.findRootValueDeserializer(collectionType);
List<String> list = (List<String>) deserializer.deserialize(p, ctxt);
return toString(list);
}
private String toString(Map<String, String> map) {
StringBuilder builder = new StringBuilder(128);
builder.append('{');
boolean addComa = false;
for (Map.Entry<String, String> entry : map.entrySet()) {
if (addComa) {
builder.append(',');
}
builder.append('"').append(entry.getKey())
.append("\":");
appendValue(entry.getValue(), builder);
addComa = true;
}
builder.append('}');
return builder.toString();
}
private String toString(List<String> list) {
StringBuilder builder = new StringBuilder(128);
builder.append('[');
boolean addComa = false;
for (String item : list) {
if (addComa) {
builder.append(',');
}
appendValue(item, builder);
addComa = true;
}
builder.append(']');
return builder.toString();
}
private void appendValue(String value, StringBuilder builder) {
if (value == null || value.isEmpty()) {
builder.append("\"\"");
return;
}
if (Character.isAlphabetic(value.charAt(0))) {
builder.append('"').append(value).append('"');
} else {
builder.append(value);
}
}
}
Prints:
a => text
b => 35
c => {d=another, dd=3.44E3}
e => [array, 35, 2.3E5]
f => 1.1E4
g => 0.00000000000003

Inner object(ArrayList) is not converting to JSON String?

I am facing issue in converting the nested list object to JSON.I am using object mapper and it is only converting the starting values and after that there is one arraylist inside it and it is not going through that list.
I have tried some basic iteration using JsonNode root = mapper.valueToTree(obj)so that i can iterate through the inner arraylist but i am not getting the result.I am new to this parsing conversion.
code snippet--
public class JsonUtils {
public static <T> String toJsonString(final T obj) throws IOException {
final ObjectMapper mapper = new ObjectMapper();
String jsonString = null;
try {
//JsonNode root = mapper.valueToTree(obj);
jsonString = mapper.writeValueAsString(obj);
} catch (final JsonProcessingException e) {
throw e;
} catch (IOException e) {
throw e;
}
return jsonString;
}
public static <T> String toJsonString(final List<T> lstObject) throws JSONException, IOException {
final JSONArray jsonArray = new JSONArray();
for (final T object : lstObject) {
final String json = JsonUtils.toJsonString(object);
final JSONObject jsonObj = new JSONObject(json);
jsonArray.put(jsonObj);
}
return jsonArray.toString();
}
}
So here is the result which i am getting -
[2, [{"geoMarketId":1,"geoname":"AP","geoId":1,"checked":false},
{"geoMarketId":7,"geoname":"EP","geoId":2,"checked":false},
{"geoMarketId":16,"geoname":"Japan","geoId":3,"checked":true},
{"geoMarketId":18,"geoname":"LA","geoId":4,"checked":true},
{"geoMarketId":22,"geoname":"MEA","geoId":5,"checked":true},
{"geoMarketId":24,"geoname":"NA","geoId":6,"checked":false}]]
Actual Result which should come-
{"geoMarketId":1,"geoname":"AP","geoId":1,"checked":false,
marketName:{"marketname":JP,"marketname":"AP","marketname":MP}}
My json conversion is ignoring this inner list in the same index.
Is there any way my json class can iterate and also convert that innerlist to JSON?

Iterate through a json file in java [duplicate]

The Jackson data binding documentation indicates that Jackson supports deserialising "Arrays of all supported types" but I can't figure out the exact syntax for this.
For a single object I would do this:
//json input
{
"id" : "junk",
"stuff" : "things"
}
//Java
MyClass instance = objectMapper.readValue(json, MyClass.class);
Now for an array I want to do this:
//json input
[{
"id" : "junk",
"stuff" : "things"
},
{
"id" : "spam",
"stuff" : "eggs"
}]
//Java
List<MyClass> entries = ?
Anyone know if there is a magic missing command? If not then what is the solution?
First create a mapper :
import com.fasterxml.jackson.databind.ObjectMapper;// in play 2.3
ObjectMapper mapper = new ObjectMapper();
As Array:
MyClass[] myObjects = mapper.readValue(json, MyClass[].class);
As List:
List<MyClass> myObjects = mapper.readValue(jsonInput, new TypeReference<List<MyClass>>(){});
Another way to specify the List type:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
From Eugene Tskhovrebov
List<MyClass> myObjects = Arrays.asList(mapper.readValue(json, MyClass[].class))
This solution seems to be the best for me.
For Generic Implementation:
public static <T> List<T> parseJsonArray(String json,
Class<T> classOnWhichArrayIsDefined)
throws IOException, ClassNotFoundException {
ObjectMapper mapper = new ObjectMapper();
Class<T[]> arrayClass = (Class<T[]>) Class.forName("[L" + classOnWhichArrayIsDefined.getName() + ";");
T[] objects = mapper.readValue(json, arrayClass);
return Arrays.asList(objects);
}
try this
List<MyClass> list = mapper.readerForListOf(MyClass.class).readValue(json)
First create an instance of ObjectReader which is thread-safe.
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader objectReader = objectMapper.reader().forType(new TypeReference<List<MyClass>>(){});
Then use it :
List<MyClass> result = objectReader.readValue(inputStream);
I was unable to use this answer because my linter won't allow unchecked casts.
Here is an alternative you can use. I feel it is actually a cleaner solution.
public <T> List<T> parseJsonArray(String json, Class<T> clazz) throws JsonProcessingException {
var tree = objectMapper.readTree(json);
var list = new ArrayList<T>();
for (JsonNode jsonNode : tree) {
list.add(objectMapper.treeToValue(jsonNode, clazz));
}
return list;
}
try {
ObjectMapper mapper = new ObjectMapper();
JsonFactory f = new JsonFactory();
List<User> lstUser = null;
JsonParser jp = f.createJsonParser(new File("C:\\maven\\user.json"));
TypeReference<List<User>> tRef = new TypeReference<List<User>>() {};
lstUser = mapper.readValue(jp, tRef);
for (User user : lstUser) {
System.out.println(user.toString());
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
here is an utility which is up to transform json2object or Object2json,
whatever your pojo (entity T)
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
*
* #author TIAGO.MEDICI
*
*/
public class JsonUtils {
public static boolean isJSONValid(String jsonInString) {
try {
final ObjectMapper mapper = new ObjectMapper();
mapper.readTree(jsonInString);
return true;
} catch (IOException e) {
return false;
}
}
public static String serializeAsJsonString(Object object) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objMapper = new ObjectMapper();
objMapper.enable(SerializationFeature.INDENT_OUTPUT);
objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
StringWriter sw = new StringWriter();
objMapper.writeValue(sw, object);
return sw.toString();
}
public static String serializeAsJsonString(Object object, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper objMapper = new ObjectMapper();
if (indent == true) {
objMapper.enable(SerializationFeature.INDENT_OUTPUT);
objMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}
StringWriter stringWriter = new StringWriter();
objMapper.writeValue(stringWriter, object);
return stringWriter.toString();
}
public static <T> T jsonStringToObject(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper objMapper = new ObjectMapper();
obj = objMapper.readValue(content, clazz);
return obj;
}
#SuppressWarnings("rawtypes")
public static <T> T jsonStringToObjectArray(String content) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper mapper = new ObjectMapper();
obj = mapper.readValue(content, new TypeReference<List>() {
});
return obj;
}
public static <T> T jsonStringToObjectArray(String content, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
T obj = null;
ObjectMapper mapper = new ObjectMapper();
mapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
obj = mapper.readValue(content, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
return obj;
}
you could also create a class which extends ArrayList:
public static class MyList extends ArrayList<Myclass> {}
and then use it like:
List<MyClass> list = objectMapper.readValue(json, MyList.class);

json (which contains few objects) to TreeMap on java

can somebody show code which deserialize json to TreeMap? Some simple example which includes example of json
I'll show what I've tried
First of all I am newbie so forgive me
That's my json(probably i have mistakes even here):
{"Car":[{"mark":"AUDI_A3", "colour": "black"},
{"mark":"BMW_m3", "colour": "white"}]}
There is my code which does not work
public static void main(String[] args) {
open();
}
private static void open(){
try {
BufferedReader buff = new BufferedReader(new FileReader("C:\Users\\t1.json"));
String bf=null;
String json= null;
while((bf=buff.readLine())!=null){
json+=bf;
}
Gson gson = new Gson();
Type type = new TypeToken<TreeMap<String, SmallGuys>>(){}.getType();
TreeMap<String, SmallGuys> Platoon = gson.fromJson(json, type);
System.out.print(Platoon.keySet());
}
catch (Exception e){
System.out.println("There is a mistake");
}
}
With Jackson, I put some methods:
public static Object convertJsonRequestToObject(HttpServletRequest pRequest, Class<?> pObject) throws JsonParseException, JsonMappingException, IOException {
return new ObjectMapper().readValue(IOUtils.toString(pRequest.getInputStream(), StandardCharsets.UTF_8), pObject);
}
public static Map<String,Object> parseJsonToMap(String json) throws JsonParseException, JsonMappingException, IOException{
return new ObjectMapper().readValue(json, HashMap.class);
}
public static Map<String,Object> parseObjectToMap(Object object){
return (Map<String, Object>) new ObjectMapper().convertValue(object, Map.class);
}
//inverse
public static Object parseMapToObject(Map<?, ?> pMap, Class<?> pClass){
return new ObjectMapper().convertValue(pMap, pClass);
}

RestTemplate & Jackson - Custom JSON deserializing?

The webservice returns an empty string instead of NULL which causes Jackson to crash.
So I created a custom parser, and I'm trying to parse it manually? Any idea How I could achieve this?
What Am I doing wrong here? All I'm trying to do is to parse JSON to object as I normally would. The field names are added to my properties using #JsonProperty so the parser should know how to convert it.
public class InsertReplyDeserializer extends JsonDeserializer<ListingReply> {
#Override
public ListingReply deserialize(JsonParser jsonParser, DeserializationContext arg1)
throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
// If service returns "" instead of null return a NULL object and don't try to parse
if (node.getValueAsText() == "")
return null;
ObjectMapper objectMapper = new ObjectMapper();
ListingReply listingReply = objectMapper.readValue(node, ListingReply.class);
return listingReply;
}
}
Here is how I resolved it
#Override
public MyObject deserialize(JsonParser jsonParser, DeserializationContext arg1)
throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
if (node.getValueAsText() == "")
return null;
MyObject myObject = new MyObject();
myObject.setMyStirng(node.get("myString").getTextValue());
JsonNode childNode = node.get("childObject");
ObjectMapper objectMapper = new ObjectMapper();
ChildObject childObject = objectMapper.readValue(childNode,
ChildObject.class);
myObject.setChildObject(childObject);
return myObject;
}
I am not sure you need to manually parse response. You solution would work but seems sub-optimal in my opinion. Since it looks like that you are using RestTemplate, you should rather write (or move your parser code to) your own message converter. Then add this converter to your rest template object which will internally deserialize the value for you. Something along the lines,
public class CustomHttpmsgConverter extends AbstractHttpMessageConverter<Object> {
private ObjectMapper objectMapper = new ObjectMapper();
#Override
protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
InputStream istream = inputMessage.getBody();
String responseString = IOUtils.toString(istream);
if(responseString.isEmpty()) //if your response is empty
return null;
JavaType javaType = getJavaType(clazz);
try {
return this.objectMapper.readValue(responseString, javaType);
} catch (Exception ex) {
throw new HttpMessageNotReadableException(responseString);
}
}
//add this converter to your resttemplate
RestTemplate template = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new CustomHttpmsgConverter());
template.setMessageConverters(converters);

Categories

Resources