gpt4 book ai didi

spring - "Unrecognized field (..), not marked as ignorable"当 jaxb 解码 xml 输入时

转载 作者:行者123 更新时间:2023-12-02 06:07:37 25 4
gpt4 key购买 nike

在一个典型的 Spring MVC 项目中,我尝试访问从外部 Web 服务源获取的对象。直到现在,这些数据的实际集成实际上并不是我在项目中的部分。但它坏了,我必须修理它。也就是说:我对相关代码并不完全熟悉。

背景

数据

从外部 Web 服务接收的 XML 数据如下所示:

<offeredServiceTOes>
<OfferedService deleted="false">
<id>0001_01-u001/igd</id>
<title>Umschlagleistung (001)</title>
<mainType>turnover</mainType>
<services>
<service id="tos5yyeivg">
<title>Umschlag Bahn - Binnenschiff</title>
<mainType>turnover</mainType>
<systemId>RailRiver</systemId>
<meansOfTransport id="motRail">
<title>Bahn</title>
<description>Bahn</description>
<systemId>Rail</systemId>
</meansOfTransport>
<meansOfTransportRel id="motRiver">
<title>Binnenschiff</title>
<description>Binnenschiff</description>
<systemId>River</systemId>
</meansOfTransportRel>
</service>
<service id="tos5yyeiw0">
[...]
</service>
[...]
</services>
[...]
</OfferedService>
[...]
<offeredServiceTOes>

解码

  • 使用 Spring Rest 模板的方法如下所示:

    @Override
    public List<OfferedServiceTO> getOfferedServices() {
    return restTemplate.getForObject(
    dataServiceUriTemplate,
    OfferedServiceTOList.class,
    OFFERED_SERVICES
    );
  • 相关的OfferedServiceTOList类:

    @XmlRootElement(name="OfferedService")
    public class OfferedServiceTO
    {

    @XmlElement
    @XmlID
    public String id;

    // [...]

    @XmlElementWrapper(name="services")
    @XmlElement(name="service")
    public List<ServiceTO> services;

    // [...]
    }
  • 相关的ServiceTO

    @XmlRootElement(name="service")
    public class ServiceTO
    {
    // [...]
    @XmlElement
    public String title;

    /[...]
    @XmlElementWrapper(name="mainServices")
    @XmlElement(name="service")
    public List<ServiceTO> mainServices;
    }
  • 编码器/解码器 xml bean 配置

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
    <list>
    <value>a.b.beans.ServiceTO</value>
    <value>a.b.OfferedServiceTO</value>
    [...]
    </list>
    </property>
    </bean>

    <bean id="xmlMessageConverter"
    class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
    <constructor-arg ref="jaxbMarshaller" />
    </bean>

    <bean id="jsonHttpMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="objectMapper" ref="jaxbJacksonObjectMapper"/>
    </bean>

    <bean id="jaxbJacksonObjectMapper"
    class="a.b.path.to.extended.jaxb.JaxbJacksonObjectMapper">
    </bean>

    <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="objectMapper" ref="jaxbJacksonObjectMapper" />
    </bean>
  • 最后,上面提到的path.to.extended.jaxb.JaxbJacksonObjectMapper是:

        public class JaxbJacksonObjectMapper extends ObjectMapper {

    public JaxbJacksonObjectMapper() {
    final AnnotationIntrospector primary = new JaxbAnnotationIntrospector();
    final AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector introspector = new AnnotationIntrospector.Pair(primary, secondary);
    DeserializationConfig deserializationConfig = super.getDeserializationConfig().withAnnotationIntrospector(introspector);
    DeserializationProblemHandler errorHandler = new DeserializationProblemHandler() {
    @Override
    public boolean handleUnknownProperty(DeserializationContext ctxt, JsonDeserializer<?> deserializer, Object beanOrClass,
    String propertyName) throws IOException, JsonProcessingException {
    //TODO Logging (unbekanntes Input-JSON)
    ctxt.getParser().skipChildren();
    return true;
    }
    };
    deserializationConfig.addHandler(errorHandler );
    super.setDeserializationConfig(deserializationConfig);
    SerializationConfig serializationConfig = super.getSerializationConfig().withAnnotationIntrospector(introspector);
    serializationConfig.set(Feature.WRAP_ROOT_VALUE, true);
    super.setSerializationConfig(serializationConfig);
    }

    }

问题

问题是,第一个列表的注释 @XmlElementWrapper(name="services") @XmlElement(name="service") 在 xml 数据包装方面对我来说看起来很好。但我不断收到错误:

[...] nested exception is org.springframework.web.client.ResourceAccessException: I/O error: Unrecognized field "service" (Class a.b.ServiceTO), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@76d697d9; line: 7, column: 18] (through reference chain: a.b.OfferedServiceTO["services"]->a.b.ServiceTO["service"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "service" (Class a.b.ServiceTO), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@76d697d9; line: 7, column: 18] (through reference chain: a.b.OfferedServiceTO["services"]->a.b.ServiceTO["service"])
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)

类似的相关问题如this one通过注释 @XmlElementWrapper(name="services") 修复。但这已经存在了。

如有任何建议,我将不胜感激。谢谢。

-- 马丁

最佳答案

好的,这比预期的要容易。 List 字段需要一个包装层。仔细查看json文档发现了解决方案:

OfferedServiceTO.class

@XmlElementWrapper(name="services")
@XmlElement(name="service")
public List<ServiceTO> services;

必须更改为

@XmlElement(name="services")
public ServiceTOList services;

其中 ServiceTOList.class 必须类似于:

@XmlRootElement(name="service")
public class ServiceTOList extends ArrayList<ServiceTO> {

public List<ServiceTO> services;
}

关于spring - "Unrecognized field (..), not marked as ignorable"当 jaxb 解码 xml 输入时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com