使用spring时不会调用@JsonDeserialize

时间:2017-09-14 11:12:51

标签: json spring jackson cxf

我正在使用Jackson 2.x和spring-ws 4.0.6。 我已使用CXF配置了休息服务并配置了映射器。

<jaxrs:providers>         
        <ref bean="jsonReader"/>
</jaxrs:providers>

然后像这样配置这个映射器。

<bean id="jsonBodyReader" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider">
    <property name="mapper" ref="mapper"/>
</bean>

<bean id="mapper"
      class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="annotationIntrospector">
        <bean class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector"/>
    </property>
    <property name="featuresToEnable">
        <array>
            <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_WITH_ZONE_ID"/>
            <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES"/>
        </array>
    </property>
    <property name="featuresToDisable">
        <array>
            <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS"/>
            <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_EMPTY_JSON_ARRAYS"/>
            <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
        </array>
    </property>
    <property name="serializationInclusion" value="NON_NULL"/>
</bean>

现在我想把这个json映射到List<HashMap<String,String>>

{
"identities": [{
    "id1": "12345",
    "id2": "16777"
},{
    "id3": "12345"
}]

}

我可以获得尽可能多的ids - id1, id2, id3..,因此我无法使用字段名id1, id2创建一个pojo,等等。

我在我的pojo中注释了这个字段

@JsonDeserialize(using = CustDeserializer.class)
private List<HashMap<String,String>> identities;

CustDeserializer看起来像这样

public class CustDeserializer extends JsonDeserializer>> {
  @Override
  public List> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectCodec oc = jp.getCodec();
    JsonNode node = oc.readTree(jp);
    List> identities = new ArrayList>();
    for(int i=0;i> elem = node1.fields();
        HashMap map= new HashMap();           
        while(elem.hasNext()){
            Entry el = elem.next();
            String name = el.getKey();
            String value = el.getValue().asText();
            map.put(name, value);
        }
        identities.add(map);
    }
    return identities;
  }

现在,当我调用其余服务时,列表中会填充包含键/值对的哈希映射。但是没有调用CustDeserializer类,所以我无法自定义列表。它默认由杰克逊的CollectionDeserializer转换。

当我在独立的java程序中尝试相同时,它可以工作,我的反序列化器被称为

ObjectMapper mapper = new ObjectMapper();

    try {
        JsonRequest h = mapper.readValue(new File("request.json"), JsonRequest.class);
        System.out.println(h);
    }

输出:JsonRequest [name=XYZ, identities=[{id2=16777, id1=12345}, {id3=12345}]]

但是当我在cxf和spring的上下文中使用它时,某种程度上它不起作用。

0 个答案:

没有答案
相关问题