更改@JsonProperty中@XmlValue的映射字段

时间:2019-03-26 06:37:15

标签: java json xml jackson swagger

我有一个元素“ SubElement2 ”,该元素具有值和属性。

工作中:

我的注释是

    @XmlValue    
    @JsonProperty(value="content")
    private String value;

XML请求:<SubElement2 parameterName = "tested">ELEMENT_2_TAG_VAL</SubElement2>

到JSON:

{
  "SubElement2" : {
    "content" : "ELEMENT_2_TAG_VAL",
    "parameterName" : "tested"
  }
}

在这里,我想将属性名称 content 更改为 myOwnContent

不起作用:

修改后的注释:

    @XmlValue    
    @JsonProperty(value="myOwnContent")  // Modified
    private String value;

XML请求:<SubElement2 parameterName = "tested">ELEMENT_2_TAG_VAL</SubElement2>

  

对于上述注释更改,我面临以下异常:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "content" (class SubElement2), not marked as ignorable (2 known properties: "parameterName", "myOwnContent"])
 at [Source: (String)"{"SubElement2":{"parameterName":"tested","content":"ELEMENT_2_TAG_VAL"}}"; line: 1, column: 53] (through reference chain: Element1["SubElement2"]->SubElement2["content"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)

下面是我的代码: ObjectFactory.java

@XmlRegistry
public class ObjectFactory {

    public SubElement2 createSubElement2() {
        return new SubElement2();
    }
}

MainProgram.java

public class MainProgram {

    public static void main(String[] args) throws ClassNotFoundException, IOException {
        String request = "<SubElement2  parameterName = \"tested\">ELEMENT_2_TAG_VAL</SubElement2>";
        System.out.println("Request in XML : "+request);
        JSONObject jObject = XML.toJSONObject(request);

        ObjectMapper mapper = createCombinedObjectMapper();
        Object json = mapper.readValue(jObject.toString(), Class.forName("Element1"));
        String output = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
        System.out.println(output);
    }

    private static ObjectMapper createCombinedObjectMapper() {
        return new ObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, false)
                .configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false)
                .configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
                .setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
    }

    private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {
        final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
        return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
    }

}

SubElement2.java

@XmlRootElement(name = "SubElement2")
public class SubElement2 {
    @XmlValue    
    @JsonProperty(value="myOwnContent")
    private String value;
    @JsonProperty(value="parameterName",required=true)
    @XmlAttribute(required = true)
    protected String parameterName;
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    public String getParameterName() {
        return parameterName;
    }
    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }
}

Element1.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "element1", propOrder = {
    "element1"
})
@XmlRootElement(name = "Element1")
public class Element1 {

    @XmlElement(name="SubElement2")
    protected SubElement2 subElement2;

    public SubElement2 getSubElement2() {
        return subElement2;
    }

    public void setSubElement2(SubElement2 subElement2) {
        this.subElement2 = subElement2;
    }
}

我正在使用Jackson版本:2.9.7

  

请帮助我解决此异常,谢谢!

     

我想做什么:

我想将jsonproperty用作myOwnContent,因为在我现有的实现中content中的另一个属性已经使用了XML。 (现有的实现没有JSON suuport,我们正在添加JSON支持)

0 个答案:

没有答案