Spring Boot返回模型的JSONArray字段为空
P粉076987386
P粉076987386 2023-09-12 15:56:44
0
1
400

我在对象中有一个JSONArray字段:

@Column(name = "_history", columnDefinition = "JSON")
@Convert(converter = JSONArrayConverter.class)
private JSONArray history;

这是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;
    }
}

问题是当从MySQL数据库请求对象时(history是JSON列并且有数据),Spring Boot将其返回为空:

"history": {}
P粉076987386
P粉076987386

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!