Jackson JSONproperty不将字段绑定到变量

时间:2019-04-12 19:20:45

标签: java jackson

我在让Jackson将JSON对象的属性绑定到POJO时遇到问题。它正在返回JSON的字段名称,而不是POJO。

这是我要绑定的JSON对象:

    "Data": {
      "technical_description": "This is a description.",
      "technical_version": "1.0",
      "technical_last_modified": "2019-03-11T18:23:34",
      "is_available_in_source": true,
      "availability_description": null,
      "is_oii": false,
      "is_pii": false,
      "grain_description": null,
      "grain_attributes": [
        "INSTANCE"
      ],
      "source_objects": null
    }

这也是我也想要绑定的POJO:

public class Specification {

    @JsonProperty(value = "technical_description")
    public String description;

    @JsonProperty(value = "technical_version")
    public String version;

    @JsonProperty(value = "technical_last_modified")
    @JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
    public Date lastModified;

    @JsonProperty(value = "is_available_in_source")
    public Boolean availableInSource;

    @JsonProperty(value = "availability_description")
    public String availabilityDescription;

    @JsonProperty(value = "is_oii")
    public Boolean oii;

    @JsonProperty(value = "is_pii")
    public Boolean pii;

    @JsonProperty(value = "grain_description")
    public String grainDescription;

    @JsonProperty(value = "grain_attributes")
    public String[] grainAttributes;

    @JsonProperty(value = "source_objects")
    public String[] sourceObjects;

    public Specification(){};

    public Specification(String description, String version, Date lastModified, Boolean isAvailableInSource,
                         String availabilityDescription, Boolean isOii, Boolean isPii, String grainDescription,
                         String[] grainAttributes, String[] sourceObjects){
        this.description=description;
        this.version=version;
        this.lastModified=lastModified;
        this.availableInSource=isAvailableInSource;
        this.availabilityDescription=availabilityDescription;
        this.oii=isOii;
        this.pii=isPii;
        this.grainDescription=grainDescription;
        this.grainAttributes=grainAttributes;
        this.sourceObjects=sourceObjects;
    };
}

规范对象通过Detail对象浮出水面

@Entity
public class Detail {

    @Id
    @Constraints.Required
    public String id;

    @Column(name = "specifications", updatable = false)
    @Convert(converter = HashMapConverter.class)
    public Map<String,Specification> technicalSpec;

    ...

}

这是我正在使用的转换器:

public class HashMapConverter implements AttributeConverter<Map<String, Specification>, String> {

    private ObjectMapper objectMapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);

    @Override
    public String convertToDatabaseColumn(Map<String, Specification> techSpecs){
        String technicalSpecs = null;
        try {
            technicalSpecs = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(techSpecs);

        } catch (final JsonProcessingException e) {
            System.out.println(e);
        }
        return technicalSpecs;
    }

    @Override
    public Map<String, Specification> convertToEntityAttribute(String techSpecJSON){
        Map<String, Specification> techSpecs = null;
        try {
            techSpecs =  objectMapper.readValue(techSpecJSON, objectMapper.getTypeFactory().constructMapType(HashMap.class, String.class, Specification.class));
        } catch (final IOException e) {
            System.out.println(e);
        }
        return techSpecs;
    }
}

它将绑定通过自定义转换器找到的对象,但是它将从JSON对象返回相同的字段名称,即我希望它返回description而不是technical_description

0 个答案:

没有答案