我试图从现有的SOAP Web服务生成一些列表。但是问题是,在尝试生成空值时,总是会得到空值。 也许有人可以评论为什么这个问题发生在我身上? 谢谢!
我正在做的步骤:
我从WSDL_link生成了wsdl
带有插件的生成的wsdl文件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
<!-- Following configuration will pass ${project.basedir}/src/remote-xjc/bindings.xjc
and ${project.basedir}/src/jaxws/bindings-default.xjc to wsimport. -->
<configuration>
<wsdlUrls>
<wsdlUrl>${project.basedir}/src/main/resources/wsdl/FxRates_12.wsdl</wsdlUrl>
</wsdlUrls>
</configuration>
</execution>
</executions>
</plugin>
我使用了这个插件,因为其他插件总是给我错误。 并尝试通过以下方式调用它:
@RestController
public class GetRatesController {
private static final String soapEndpointUrl = "http://old.lb.lt/webservices/FxRates/FxRates.asmx";
private static final String soapAction = "http://www.lb.lt/WebServices/FxRates/getCurrencyList";
@RequestMapping(value = "/get", method = RequestMethod.GET, produces = "application/xml")
public ResponseEntity<GetCurrencyListResponse> getIndexView() {
GetCurrencyListResponse response = new GetCurrencyListResponse();
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
GetCurrencyList getCurrencyList = new GetCurrencyList();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction, getCurrencyList), soapEndpointUrl);
JAXBContext jaxbContext = JAXBContext.newInstance(GetCurrencyListResponse.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
response = (GetCurrencyListResponse) unmarshaller.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument());
} catch (Exception e) {
System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");
e.printStackTrace();
}
System.out.println(response.getGetCurrencyListResult().getContent());
return new ResponseEntity<>(response, HttpStatus.OK);
}
private static <T> SOAPMessage createSOAPRequest(String soapAction, T requestClass) throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
createSoapEnvelope(soapMessage, requestClass);
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", soapAction);
soapMessage.saveChanges();
return soapMessage;
}
private static <T> void createSoapEnvelope(SOAPMessage soapMessage, T requestClass) throws SOAPException, JAXBException {
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
JAXBContext jaxbContext = JAXBContext.newInstance(requestClass.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter sw = new StringWriter();
// SOAP Body
SOAPBody soapBody = envelope.getBody();
jaxbMarshaller.marshal(requestClass, sw);
System.out.println(sw.toString());
soapBody.addTextNode(sw.toString());
}
}
但是我得到的结果为空。 哪一部分错了?如何获取货币清单,应返回哪个服务? 货币清单服务的XML:XML