无法为 XML 输入对象添加 XML 映射器:原因:java.lang.ClassNotFoundException:com.fasterxml.jackson.dataformat.xml.XmlMapper

Not able to add XML mapper for the XML input Object : Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.xml.XmlMapper

提问人:MANOHAR SINGH RAGHUWANSHI 提问时间:9/3/2022 最后编辑:MANOHAR SINGH RAGHUWANSHI 更新时间:2/1/2023 访问量:327

问:

搜索了一段时间后,我无法获得正确版本的杰克逊罐子以及我需要使用的杰克逊罐子。我无法尝试添加所有适用的 jar 以获得确切的解决方案,但我的应用程序尚未启动。 使用以下 jars..

<spring.version>5.3.23</spring.version>
    <jackson.version>2.13.2</jackson.version

我正在尝试在消息转换器的帮助下为两种类型的输入和输出、JSON 和 XML 添加对象映射器,这是 *-servlet 中的代码.xml

<mvc:annotation-driven >
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="serializationInclusion" value="NON_EMPTY"/>
                    </bean>
                </property>
            </bean>
            <!-- Added this bean configuration for XML serialization  -->
            <bean class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.dataformat.xml.XmlMapper">
                        <property name="serializationInclusion" value="NON_EMPTY"/>
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    
    

这是我的父 pom 条目: 杰克逊罐子版本是2.13.2

<dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <!-- upgraded due to vulnerabilities in 2.13.2 -->
                <version>2.13.2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.xml.bind</groupId> 
                <artifactId>jaxb-api</artifactId> 
                <version>${jackson.version}</version> 
            </dependency>
            <dependency>
                  <groupId>com.fasterxml.jackson.dataformat</groupId>
                  <artifactId>jackson-dataformat-xml</artifactId>
                  <version>${jackson.version}</version>
                </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-jaxrs-provider</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-module-jaxb-annotations</artifactId>
                <version>${jackson.version}</version>
                <!-- <exclusions>
                    <exclusion>
                        <groupId>jakarta.xml.bind</groupId>
                        <artifactId>jakarta.xml.bind-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>jakarta.activation</groupId>
                        <artifactId>jakarta.activation-api</artifactId>
                    </exclusion>
                </exclusions> -->
            </dependency>

成功清理和构建后,我在启动 tomee 时收到 ClassNotFoundException,即使在我在各自的 pom.xml 中添加了适用的 depencies 之后也是如此。

这是我从中得到的例外。

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter#0' defined in ServletContext resource [/WEB-INF/api-dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/xml/XmlMapper
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
        ... 26 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/xml/XmlMapper
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
        ... 30 more
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/xml/XmlMapper
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:584)
        at org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter.<init>(MappingJackson2XmlHttpMessageConverter.java:50)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
        ... 32 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.xml.XmlMapper
        at org.apache.openejb.util.classloader.URLClassLoaderFirst.loadClass(URLClassLoaderFirst.java:136)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 39 more

        

需要了解查看此处问题的方式或路径,我需要改进以解决此 ClassNotFoundException

xml 解析 jaxb objectmapper jackson-dataformat-xml xmlmapper

评论

0赞 Mads Hansen 9/3/2022
stackoverflow.com/a/60421717/14419
0赞 MANOHAR SINGH RAGHUWANSHI 9/3/2022
按照上述链接完成配置。它对我不起作用。

答: 暂无答案