Spring &引用;未识别字段(..),未标记为可忽略“;当jaxb解组xml输入时

Spring &引用;未识别字段(..),未标记为可忽略“;当jaxb解组xml输入时,spring,jaxb,unmarshalling,resttemplate,Spring,Jaxb,Unmarshalling,Resttemplate,在一个典型的SpringMVC项目中,我试图访问从外部webservice源获取的对象。到目前为止,这些数据的实际集成并不是我在项目中的一部分。但它坏了,我得把它修好。也就是说:我并不完全熟悉相关代码 背景 数据 从外部web服务接收的XML数据如下所示: <offeredServiceTOes> <OfferedService deleted="false"> <id>0001_01-u001/igd</id> &l

在一个典型的SpringMVC项目中,我试图访问从外部webservice源获取的对象。到目前为止,这些数据的实际集成并不是我在项目中的一部分。但它坏了,我得把它修好。也就是说:我并不完全熟悉相关代码

背景 数据 从外部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>
@Override
public List<OfferedServiceTO> getOfferedServices() {
    return restTemplate.getForObject(
            dataServiceUriTemplate, 
            OfferedServiceTOList.class,
            OFFERED_SERVICES
    );
  • 相关的
    ServiceTO

    @XmlRootElement(name="service")
    public class ServiceTO
    {
        // [...]
        @XmlElement
        public String title;
    
        /[...]
        @XmlElementWrapper(name="mainServices")
        @XmlElement(name="service")
        public List<ServiceTO> mainServices;
    }
    
  • 问题 问题是,关于xml数据包装,第一个清单的注释,
    @xmlementwrapper(name=“services”)@xmlement(name=“service”)
    看起来不错。但我一直在犯错误:

    [...] 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)
    
    通过注释
    @xmlementwrapper(name=“services”)
    解决了类似的相关问题。但这已经存在

    如果有任何建议,我将不胜感激。多谢各位


    --马丁

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

    在OfferedServiceTO.class

    @XmlElementWrapper(name="services")
    @XmlElement(name="service")
    public List<ServiceTO> services;
    
    其中
    ServiceTOList.class
    必须类似于:

    @XmlRootElement(name="service")
    public class ServiceTOList extends ArrayList<ServiceTO> {
    
        public List<ServiceTO> services;
    }
    
    @XmlRootElement(name=“service”)
    公共类服务列表扩展了ArrayList{
    公开名单服务;
    }
    
    [...] 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)
    
    @XmlElementWrapper(name="services")
    @XmlElement(name="service")
    public List<ServiceTO> services;
    
    @XmlElement(name="services")
    public ServiceTOList services;
    
    @XmlRootElement(name="service")
    public class ServiceTOList extends ArrayList<ServiceTO> {
    
        public List<ServiceTO> services;
    }