我想创建一个org.eclipse.persistence.mappings.converters.Converter将POJO转换为JSON。我这样做是将其存储在Postgres jsonb字段中。 使用Spring Boot 1.5.14可以很好地工作。新项目位于Spring Boot 2.0.2上,该代码不再起作用。不再调用Converter,这将导致数据库中出现类型错误。
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.sessions.Session;
import org.postgresql.util.PGobject;
import java.io.IOException;
import java.sql.SQLException;
public class MetadataConverter implements org.eclipse.persistence.mappings.converters.Converter {
private ObjectMapper mapper = new ObjectMapper();
@Override
public Object convertObjectValueToDataValue(Object o, Session session) {
try {
PGobject out = new PGobject();
out.setType("jsonb");
out.setValue(mapper.writeValueAsString(o));
return out;
} catch (SQLException e) {
throw new IllegalArgumentException("Unable to serialize to json field ", e);
} catch (JsonProcessingException e) {
e.printStackTrace();
throw new IllegalArgumentException("Unable to serialize to json field ", e);
}
}
@Override
public Metadata convertDataValueToObjectValue(Object o, Session session) {
if (o == null) {
return null;
}
try {
if (o instanceof PGobject) {
return mapper.readValue(((PGobject) o).getValue(), new TypeReference<Metadata>(){});
} else {
return mapper.readValue((String) o, new TypeReference<Metadata>(){});
}
} catch (IOException | ClassCastException e) {
throw new IllegalArgumentException("Unable to deserialize to json field ");
}
}
@Override
public boolean isMutable() {
return false;
}
@Override
public void initialize(DatabaseMapping databaseMapping, Session session) {
}
}
这是模型:
...
@Data
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(name = "MY_TEST")
public class TestModel implements Serializable{
@Id
String id;
@Column(columnDefinition = "jsonb")
@Converter(converterClass = MetadataConverter.class, name = "metadataJsonConverter")
@org.eclipse.persistence.annotations.Convert("metadataJsonConverter")
Metadata metadata;
}
在两个项目中,eclipslink版本均为2.6.3,Postgres版本位于旧项目9.4.1212.jre7(已计划更新)中,而位于新项目42.2.2中。
有人知道改变了什么吗?
最好的问候
卢卡斯
答案 0 :(得分:0)
JpaConfiguration错误,扩展了JpaBaseConfiguration解决了问题:
@Configuration
public class JpaConfiguration extends JpaBaseConfiguration {
protected JpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
super(dataSource, properties, jtaTransactionManagerProvider, transactionManagerCustomizers);
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
final HashMap<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, detectWeavingMode());
map.put(PersistenceUnitProperties.VALIDATION_MODE, ValidationMode.NONE.toString());
return map;
}
private String detectWeavingMode() {
return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static";
}
}