我正在尝试在Springboot应用程序中使用Soap Web服务。我的代码中出现[javax.xml.bind.JAXBException:“ com.mta.example”不包含ObjectFactory.class或jaxb.index]错误。我不确定此处的配置错误。 contextPath的值应该是什么?它引用任何软件包还是应该与WSDL中的某些元素匹配?抱歉,我对SOAP Web服务不太熟悉。
Spring boot主类如下。
package com.mta;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.mta"})
public class MTApplication {
public static void main(String[] args) {
SpringApplication.run(MTApplication.class, args);
LoginClient logClient = new LoginClient();
logClient.getLoginDetails();
}
}
肥皂配置类如下
package com.mta;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class SoapConfiguration {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.mta.example");
return marshaller;
}
@Bean
public SoapConnector soapConnector(Jaxb2Marshaller marshaller) {
SoapConnector client = new SoapConnector();
client.setDefaultUri("https://test.platform.ws");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
SoapConnector类以调用Web服务
package com.mta;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class SoapConnector extends WebServiceGatewaySupport {
public Object callWebService(String url, Object request) {
// CREDENTIALS and REQUEST SETTINGS...
return getWebServiceTemplate().marshalSendAndReceive(url, request);
}
}
客户端类
public class LoginClient extends WebServiceGatewaySupport{
@Autowired
SoapConnector soapConnector;
private static final Logger log = LoggerFactory.getLogger(LoginClient.class);
public LoginResponse getLoginDetails() {
LoginRequest request = new LoginRequest();
request.setUserId("mtatest");
request.setPassword("test");
LoginResponse response = (LoginResponse) soapConnector.callWebService("http://www.mta.com/gp/Login", request);
System.out.println(response.getOpCode());
return response;
}