当你在使用Java调用.net实现的webservice出现如标题的exception时,恰好你正在使用https://spring.io/guides/gs/consuming-web-service/ 进行soap对接,不要慌。我刚踩过这个坑。
经过查找之后发现JAX-WS规范不需要SoapAction,但是.NET需要,所以产生了这个错误。
解决办法:
首先获取wsdl:
然后引入dependency
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
</dependency>
引入plugin
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generatePackage>com.xxx.givenchy.email</generatePackage>
<schemas>
<schema>
<fileset>
<!-- Defaults to schemaDirectory. -->
<directory>${basedir}/src/main/resources/schemas</directory>
<!-- Defaults to schemaIncludes. -->
<includes>
<include>*.wsdl</include>
</includes>
<!-- Defaults to schemaIncludes -->
<!--<excludes>-->
<!--<exclude>*.xs</exclude>-->
<!--</excludes>-->
</fileset>
<!--<url>http://localhost:8080/ws/countries.wsdl</url>-->
</schema>
</schemas>
</configuration>
</plugin>
配置web service
@Configuration
public class EmailClientConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
// this package must match the package in the <generatePackage> specified in
// pom.xml
marshaller.setContextPath("com.xxx.givenchy.email");
return marshaller;
}
@Bean
public EmailClient emailClient(Jaxb2Marshaller marshaller) {
EmailClient client = new EmailClient();
client.setDefaultUri("http://w01app39.xxx.com/ServiceCenter/MailCenter.asmx");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
封装调用方法:
public class EmailClient extends WebServiceGatewaySupport {
private static final Logger log = LoggerFactory.getLogger(EmailClient.class);
public SendMailResponse sendEmail(SendMail request) {
log.info("Requesting location for " + request);
WebServiceTemplate template = getWebServiceTemplate();
SendMailResponse response = (SendMailResponse) template
.marshalSendAndReceive(request,
new SoapActionCallback(
"http://service.xxx.sh.cn/sendMail"));
return response;
}
}
注意加红部分,是导致异常的原因。这个action url从哪里来的呢?
看生成wsdl的链接。
保证action 和红框一致即可。