Heim  >  Fragen und Antworten  >  Hauptteil

Das JSONArray-Feld des Spring Boot-Rückgabemodells ist leer

Ich habe ein JSONArray-Feld im Objekt:

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

Dies ist der Code für 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;
    }
}

Das Problem besteht darin, dass Spring Boot beim Anfordern des Objekts aus der MySQL-Datenbank (der Verlauf ist eine JSON-Spalte und enthält Daten) es als leer zurückgibt:

"history": {}
P粉076987386P粉076987386402 Tage vor586

Antworte allen(1)Ich werde antworten

  • P粉496886646

    P粉4968866462023-09-13 10:53:10

    最后,我解决了这个问题。

    <dependency>
        <groupId>io.hypersistence</groupId>
        <artifactId>hypersistence-utils-hibernate-60</artifactId>
        <version>3.4.3</version>
    </dependency>

    首先,将上述存储库添加到pom.xml中。然后将代码更改为以下内容:

    @Column(name = "_history", columnDefinition = "json")
    @Type(JsonType.class)
    private List<Map<String, Object>> history = new ArrayList<>();

    然后一切都正常工作。

    Antwort
    0
  • StornierenAntwort