无法转换为类型spring.oxm.marshaller:找不到匹配的编辑器或转换策略

Sky*_*zer 1 java spring spring-ws jaxb

我长期以来一直在努力解决这个错误,谷歌搜索,看看网络上的例子,仍然没有开始工作.说实话,我不明白为什么我的项目会抛出这个错误.

Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.oxm.Marshaller] for property 'marshaller': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    ... 52 more)
    at junit.framework.Assert.fail(Assert.java:57)
    at junit.framework.TestCase.fail(TestCase.java:227)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:100)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)

所以我的设置是Spring Web Services,Jaxb2,maven.我已经预定义了xsd文件并通过jaxb2 maven插件从它们生成了java类.

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>schema1-xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>src/main/resources/</schemaDirectory>
                            <schemaFiles>ApplicationResponse.xsd</schemaFiles>
                            <packageName>com.package.response</packageName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

我的pom还有这些依赖:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>${jaxb.api.version}</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.7</version>
    <!-- <exclusions>
        <exclusion>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
        </exclusion>
    </exclusions> -->
</dependency>
<dependency>
    <groupId>org.apache.xmlbeans</groupId>
    <artifactId>xmlbeans</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.stream</groupId>
    <artifactId>sjsxp</artifactId>
    <version>1.0.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的appContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sws="http://www.springframework.org/schema/web-services"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/web-services
                        http://www.springframework.org/schema/web-services/web-services-2.0.xsd
                        http://www.springframework.org/schema/util
                        http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <context:component-scan base-package="com.package" />

    <sws:annotation-driven />

    <bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
        <property name="soapVersion">
            <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_11" />
        </property>
    </bean>

    <bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
        <constructor-arg ref="messageFactory"/>
        <property name="defaultUri" value="https://example/address/hidden"/>
        <property name="marshaller" value="marshaller" />
        <property name="unmarshaller" value="marshaller" />
    </bean>

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
          <property name="contextPath" value="com.package.request:com.package.request" />
          </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

我的服务客户端类

@Component
public class NorServiceClient implements NorService {

    @Autowired
    private WebServiceTemplate wsTemplate;

    public void setDefaultUri(String defaultUri) {
        wsTemplate.setDefaultUri(defaultUri);
    }

    public ApplicationResponse downloadFileList(ApplicationRequest request) {
        // Command: DownloadFileList
        return (ApplicationResponse) wsTemplate.marshalSendAndReceive(request);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的测试用例:

public class AppTest extends TestCase {

    private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/appContext.xml");

    @Test
    public void testApplicationRequest() {
        assertNotNull(new Object());
        System.out.println("Context: " + context);
        NorService norService = (NorService) context.getBean("norServiceClient");
        ApplicationRequest request = new ApplicationRequest();
        nordeaService.downloadFileList(request);
    }
}
Run Code Online (Sandbox Code Playgroud)

启动我的应用程序时,它甚至没有到达service.downloadFileList,它在初始化上下文时抛出异常.所以我不认为这可能是我已经实例化空ApplicationRequest对象的问题.

问题出在哪里?通过互联网上的所有示例,我已经完成了相同的设置,但在我的项目中,它抛出了异常no matching editors or conversion strategy found

Sot*_*lis 6

我假设你的错误是指这个

<property name="marshaller" value="marshaller" />
Run Code Online (Sandbox Code Playgroud)

属性marshallerorg.springframework.oxm.Marshaller的是类的类型的字段org.springframework.ws.client.core.WebServiceTemplate.你不能给它一个String值"marshaller".

您想要在id的上下文中引用另一个bean marshaller.

<property name="marshaller" ref="marshaller" />
Run Code Online (Sandbox Code Playgroud)

同样的事情unmarshaller.