繁体   English   中英

使用 spring 引导在实体 class 的列上添加 json

[英]adding json on a column in enitity class using spring boot

我正在使用 JPA AttributeConverter 和 Spring Boot 1.2.1 RELEASE,它工作正常。 但是升级到 Spring Boot 1.3.0.RELEASE 后出现以下错误

attributeLabelMap.java:

我想将其添加为 json 格式

metadata: {
          masked: true,
          maxLength: 200,
          formula: "a + b",
          parentCode: "strAttr_1",
          currency: "$",
        },

我在实体 class 中添加了此列

@Column(name = "metadata_json",columnDefinition = "json")
@Convert(converter = HashMapConverter.class)
private Map<String,Object> metaDataAttribute

哈希映射转换器 class:

package in.nobroker.hood.crm.entity;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import javax.persistence.AttributeConverter;
import java.io.IOException;
import java.util.Map;

@Slf4j
public class HashMapConverter implements AttributeConverter<Map<String, Object>, String> {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String convertToDatabaseColumn(Map<String, Object> stringObjectMap) {
        String attributeInfoJson = null;
        try {
            attributeInfoJson = objectMapper.writeValueAsString(stringObjectMap);
        } catch (final JsonProcessingException e) {
            log.error("JSON writing error", e);
        }
        return attributeInfoJson;
    }

    @Override
    public Map<String, Object> convertToEntityAttribute(String s) {
        Map<String, Object> attributeInfo = null;
        try {
            attributeInfo = objectMapper.readValue(s, Map.class);
        } catch (final IOException e) {
            log.error("JSON reading error", e);
        }
        return attributeInfo;
    }
}

在 Postman 中:

{
    "label": "dateAttr_5",
    "id": "f73434ba-bfaa-49f5-9a41-b86715a985f2",
    "filter": true,
    "inUse": true,
    "editable": true,
    "mandatory": true,
    "priority": 0,
    "textOptions": "a,b,c,d",
    "type": "DATETIME_FORM_DATA",
    "filterType": "ABSOLUTE",
    "metaDataAttribute": {
        "masked": true,
        "maxLength": 200,
        "formula": "a + b",
        "parentCode": "strAttr_1",
        "currency": "$"
    }
}

我想在我点击 Postman 时添加 metaDataAttribute,它给出了内部服务器错误:

argument "content" is null
failed to fetch attribute label mappings 
argument "content" is null
    at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4693)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3401)
    at in.nobroker.hood.crm.entity.HashMapConverter.convertToEntityAttribute(HashMapConverter.java:31)
    at in.nobroker.hood.crm.entity.HashMapConverter.convertToEntityAttribute(HashMapConverter.java:10)
    at org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl.toDomainValue(JpaAttributeConverterImpl.java:45)
    at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140)
    ... 186 common frames omitted

在 HashMapConverter 的 convertToEntityAttribute 中添加对 String 的 null 检查

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM