I have a JSONArray
field in the object:
@Column(name = "_history", columnDefinition = "JSON")
@Convert(converter = JSONArrayConverter.class)
private JSONArray history;
and this is JSONArrayConverter
:
@JsonSerialize
@Converter(autoApply = true)
public class JSONArrayConverter implements AttributeConverter<JSONArray, String> {
public static final Logger LOGGER = LoggerFactory.getLogger(JSONObjectConverter.class);
@Override
public String convertToDatabaseColumn(JSONArray array) {
LOGGER.debug(array.toString());
if (array == null)
return new JSONArray().toString();
String data = null;
try {
data = array.toString();
} catch (final Exception e) {
LOGGER.error("JSON writing error", e);
}
return data;
}
@Override
public JSONArray convertToEntityAttribute(String data) {
if (_EMPTY.equals(data) || data == null || "[]".equals(data))
return new JSONArray();
JSONArray array = null;
try {
array = new JSONArray(data);
} catch (final Exception e) {
LOGGER.error("JSON reading error", e);
}
return array;
}
}
The problem is when requesting the object from MySQL database(history is JSON column and has data), Spring boot returns it as null:
"history": {}