简体   繁体   English

ObjectMapper 不考虑注释

[英]ObjectMapper not considering annotations

I have the below piece of code which would convert an annotated Java object into Json String.我有下面的一段代码,它将带注释的 Java 对象转换为 Json 字符串。

 try {
            String jsonString = mapper.writeValueAsString(obj);
            LOGGER.debug("logAsJson", jsonString);
        } catch (Exception e) {
            LOGGER.warn("logAsJson", "Exception in logging only. Nothing critical! ", e);
        }

And my class would look something like this我的课看起来像这样

public class Car {公共类汽车{

@JsonProperty("capabilities")
private List<Capability> capability = new ArrayList<Capability>();

@JsonProperty("periodicity")
@NotNull
private Periodicity periodicity;

@JsonProperty("car_driver")
private List<carDriver> carDriver = new ArrayList<carDriver>();

} }

Problem is that while creating the json string from the object, object mapper is not considering the annotations for field names.问题是在从对象创建 json 字符串时,对象映射器没有考虑字段名称的注释。

Thanks in advance.提前致谢。

You might have imported the json annotation and the object mapper from different library versions.您可能已经从不同的库版本中导入了 json 注释和对象映射器。 Make sure they are from the same library.确保它们来自同一个库。

Example,例子,

import com.fasterxml.jackson.annotation.JsonProperty;

and

import com.fasterxml.jackson.databind.ObjectMapper;

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

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