我使用eclipse,我有一个wsdl。我从此wsdl生成了Java类。但是我没有每个wsdl元素(请求元素)
wsdl是:
<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:DefaultNameSpace" xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="urn:DefaultNameSpace" xmlns:intf="urn:DefaultNameSpace" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:DefaultNameSpace">
<element name="LINEAS" type="xsd:string"/>
<element name="TRAMOS" type="xsd:string"/>
<element name="CODUSUARIO" type="xsd:string"/>
<element name="NOMUSUARIO" type="xsd:string"/>
<complexType name="LISTADO">
<sequence>
<element name="RESULTADO" type="xsd:string"/>
<element name="MSG" type="xsd:string"/>
</sequence>
</complexType>
<element name="LISTADODOCUMENTOSReturn" type="impl:LISTADO"/>
<complexType name="Elemento1">
<sequence>
<element name="LINEAS1" type="xsd:string"/>
<element name="TRAMOS1" type="xsd:string"/>
<element name="CODUSUARIO1" type="xsd:string"/>
<element name="NOMUSUARIO1" type="xsd:string"/>
</sequence>
</complexType>
<element name="Elemento2" type="impl:Elemento1"/>
</schema>
</wsdl:types>
<message name="LISTADODOCUMENTOSRequest">
<part name="elementorequest" element="impl:Elemento2"/>
</message>
<message name="LISTADODOCUMENTOSResponse">
<part element="impl:LISTADODOCUMENTOSReturn" name="LISTADODOCUMENTOSReturn"/>
</message>
<portType name="DegradadoMercanciasNotes">
<operation name="LISTADODOCUMENTOS">
<input message="impl:LISTADODOCUMENTOSRequest" name="LISTADODOCUMENTOSRequest"/>
<output message="impl:LISTADODOCUMENTOSResponse" name="LISTADODOCUMENTOSResponse"/>
</operation>
</portType>
<binding name="DominoSoapBinding" type="impl:DegradadoMercanciasNotes">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="LISTADODOCUMENTOS">
<wsdlsoap:operation soapAction=""/>
<input name="LISTADODOCUMENTOSRequest">
<wsdlsoap:body use="literal"/>
</input>
<output name="LISTADODOCUMENTOSResponse">
<wsdlsoap:body use="literal"/>
</output>
</operation>
</binding>
<service name="DegradadoMercanciasNotesService">
<port binding="impl:DominoSoapBinding" name="Domino">
<wsdlsoap:address location="http://seguriteca.sir.renfe.es:80/u35/GDN/Seguriteca2017/Normativa.nsf/DegradadoMercancias?OpenWebService"/>
</port>
</service>
</definitions>
我从eclipse中生成了Java类。我想问一个问题。哪些wsdl'元素转换为Java类?因为在我的Java类中,我都看不到
<message name="LISTADODOCUMENTOSRequest">
<part name="elementorequest" element="impl:Elemento2"/>
</message>
也不
<element name="Elemento2" type="impl:Elemento1"/>
编辑
我的问题在于WSDL'元素
<message name="LISTADODOCUMENTOSRequest">
<part element="impl:LINEAS" name="LINEAS" />
<part element="impl:TRAMOS" name="TRAMOS" />
<part element="impl:CODUSUARIO" name="CODUSUARIO" />
<part element="impl:NOMUSUARIO" name="NOMUSUARIO" />
</message>
当我尝试生成Java类时,此元素发生错误。
答案 0 :(得分:0)
几个月前,我遇到了这个确切的问题。我花了大约一个星期左右的时间来尝试使其工作。
我尝试了大多数可用的IDE ,但它们全部未能生成所有有效的类,但 NetBeans 是一个例外; NetBeans的工作方式不同于Eclipse和IntelliJ,因此我向您推荐的是转移所有与 SOAP请求创建或通用WSDL操作有关的wsdl相关代码。 >转到NetBeans。
然后,您将能够解析响应并发送请求,就像您希望在Eclipse中(但在NetBeans中)所做的那样;但是,这意味着将其作为独立的胖子,其中包含与XML解析等相关的所有依赖关系,您可以将用作Eclipse项目的依赖关系 。
在处理National Rail的开源数据源时,这对我来说是一个非常延迟的过程,但希望以下内容能帮助您更快地解决问题!
使用JAX WS生成类:
1. https://netbeans.org/kb/docs/websvc/client.html
在NetBeans中创建远端jar :
1.导航到“文件资源管理器”等
2.找到build.xml并右键单击
3.将鼠标悬停在“运行目标”上
4.将鼠标悬停在“其他目标”上
5.单击“干净并添加脂肪”
这样做将为您提供很好的生成的类,您可以使用以下类将请求发送至(请注意,我尚未更改项目中的默认标头等,因此您可以引用它们,而只需将其替换为所需的内容即可) !)
请注意:为了方便起见,我已经反映过, Jax WS 所生成的大量请求类中的函数名称被命名为“ create { bla bla}请求”等。
public class SoapRequest {
private final SOAPMessage request;
private final String actionString;
public SoapRequest(Action action, Document generatedRequestAsDoc) throws SOAPException {
this.actionString = action.getActionString();
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("ldb", "http://thalesgroup.com/RTTI/2017-10-01/ldb/");
envelope.addNamespaceDeclaration("typ", "http://thalesgroup.com/RTTI/2013-11-28/Token/types");
envelope.getHeader().
addHeaderElement(new QName("http://thalesgroup.com/RTTI/2013-11-28/Token/types", "AccessToken")).
addChildElement("typ:TokenValue").
addTextNode(getTokenValue());
soapMessage.getMimeHeaders().addHeader("Content-type", "application/soap+xml;charset=UTF-8;action=" + createSoapAction(getActionString(), false));
soapMessage.getMimeHeaders().addHeader("Accept-encoding", "gzip, x-gzip, deflate, x-bzip2");
soapMessage.getMimeHeaders().addHeader("SOAPAction", "http://thalesgroup.com/RTTI/2017-10-01/ldb/ " + createSoapAction(getActionString(), true));
soapMessage.getSOAPBody().addDocument(generatedRequestAsDoc); //adding the generated request by jax client to the body
soapMessage.saveChanges();
this.request = soapMessage;
}
private static String createSoapAction(String action, boolean appendRequest) {
return action + (appendRequest ? "Request" : "");
}
private static Document getMarshalledDocument(JAXBElement<?> element, JAXBContext jaxbContext) {
Document document = getEmptyDoc();
try {
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(element, document);
} catch (JAXBException e) {
Logger.getLogger(IdealTrains.class.getName()).log(Level.SEVERE, null, e);
document = null;
}
return document;
}
@Nullable
public static Document generateDoc(JAXBElement<?> element) {
Class elementClass = element.getDeclaredType();
Document generatedDoc = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(elementClass);
generatedDoc = getMarshalledDocument(element, jaxbContext);
if (generatedDoc == null) {
System.out.println("Could not marshall, terminating...");
return null;
}
return generatedDoc;
} catch (JAXBException ex) {
Logger.getLogger(SoapRequest.class.getName()).log(Level.SEVERE, null, ex);
}
return generatedDoc;
}
@Nullable
public SoapResponse sendRequestForResponse() {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(getRequest(), IdealTrains.STAFF_WEBSERVICE_ENDPOINT);
soapConnection.close();
return new SoapResponse(soapResponse);
} catch (UnsupportedOperationException | SOAPException 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();
return null;
}
}
private static Document getEmptyDoc() {
Document document = null;
try {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException ex) {
Logger.getLogger(IdealTrains.class.getName()).log(Level.SEVERE, null, ex);
}
return document;
}
private static String getTokenValue() {
return IdealTrains.AUTHENTICATION_TOKEN;
}
private SOAPMessage getRequest() {
return this.request;
}
public String getActionString() {
return actionString;
}
public String getResponseString() {
return getActionString() + "Response";
}
}
public class Action {
private final Object objectFactory;
private final String response;
private String actionString;
private Service.ServiceType serviceType;
public Action(Object objectFactory, String getString, Service.ServiceType serviceType) {
this.actionString = getString;
this.objectFactory = objectFactory;
this.response = getString + "Response";
this.serviceType = serviceType;
}
public JAXBElement<?> doParams(Object genericType) {
String functionNameInFacClass = "create" + getActionString() + "Request";
Method func = null;
Class gTypeClass = genericType.getClass();
Object invokedResponse = null;
try {
func = getObjectFactory().getClass().getMethod(functionNameInFacClass, gTypeClass);
invokedResponse = func.invoke(getObjectFactory(), genericType); //Invoke createACTIONRequest in suitable object factory
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
ex.printStackTrace();
}
JAXBElement<?> element = (JAXBElement<?>) invokedResponse;
return element;
}
public Object getObjectFactory() {
return objectFactory;
}
public String getActionString() {
return actionString;
}
public String getResponse() {
return response;
}
public Service.ServiceType getServiceType() {
return this.serviceType;
}
public void setServiceType(Service.ServiceType serviceType) {
this.serviceType = serviceType;
}
public void setAction(String actionString) {
this.actionString = actionString;
}
}