用Java构建SOAP客户端

时间:2017-05-23 12:56:16

标签: java xml web-services soap jaxb

我希望在Java中使用SOAP API,它接收类似这样的XML,以便对方法进行校验:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <GetReturnAnalysis xmlns="http://www.address.com/integration">
            <entityCode>186D3CAD-0841</entityCode>
        </GetReturnAnalysis>
    </env:Body>
</env:Envelope>

为了做到这一点,我创建了以下课程:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GetReturnAnalysis")
public class GetReturnAnalysisRequest {
  @XmlElement(name = "entityCode")
  protected String entityCode;
  @XmlAttribute(name="xmlns", required = true)
  public final static String xmlns="http://www.address.com/integration";

  public GetReturnAnalysisRequest(String entityCode) {
    this.entityCode = entityCode;
  }

  public GetReturnAnalysisRequest() { }

  public String getEntityCode() {
    return entityCode;
  }

  public void setEntityCode(String entityCode) {
    this.entityCode = entityCode;
  }        
}

并制作以下​​代码以构建要发送的消息:

private  SOAPMessage createSOAPRequest(GetReturnAnalysis request) throws SOAPException, JAXBException, IOException, ParserConfigurationException, SAXException {
    MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
    SOAPMessage message = messageFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    message.getSOAPHeader().detachNode();

    SOAPBody body = envelope.getBody();
    String requestString =  XmlHelper.toOutputString(request);
    Document doc = convertStringToDocument(requestString);
    body.addDocument(doc);
    message.writeTo(System.out);

    message.saveChanges();
    message.writeTo(System.out);


    return message;
  }

(内部调用的方法,在此代码中):

public static <T> String toOutputString(T type) throws IOException {
    Validate.notNull(type, "Java type not defined!");

    try {
      StringWriter os = new StringWriter();
      JAXBContext jaxbContext = JAXBContext.newInstance(type.getClass());
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

      // output pretty printed
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
      jaxbMarshaller.marshal(type, os);

      System.out.println(os.toString());

      return os.toString();
    } catch (JAXBException e) {
      throw new IOException(e);
    }
  }




private Document convertStringToDocument(String xml) throws SAXException, IOException, ParserConfigurationException {
    return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
  }

在我看来,它应该有用。但是,它会生成类似这样的XML:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <GetReturnAnalysis xmlns="">
            <entityCode>186D3CAD-0841</entityCode>
        </GetReturnAnalysis>
    </env:Body>
</env:Envelope>

服务器不接受,因为它没有填充xmlns =“http://www.address.com/integration”属性。我想知道它是否是由于属性的名称(xmlns)...但是,这是服务器期望的名称,我无法更改它,因为它是第三方API。

我也尝试过不同的方式,宣布这样的课程:

  @XmlRootElement(name = "GetReturnAnalysis", namespace = "http://www.address.com/integration")
  public class GetReturnAnalysisRequest { ...

但是当我添加到消息(body.addDocument方法)时,它会检索错误(那里不应该有命名空间)。

在代码中,您可能已经注意到,我已经放了两个message.writeTo(用于调试,在createSOAPRequest方法上)。第一个正确地给了我XML,第二个,在我调用“save”之后,我在GetReturnAnalysis上获得了xmlns属性为空的xml。

我想知道你是否可以帮助我。我是一个使用SOAP的菜鸟,并且在这个问题上遇到了很多麻烦......

更新1

我使用postman成功发送了一条消息,其中包含以下XML:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns="http://www.address.com/integration" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <GetReturnAnalysis >
            <entityCode>186D3CAD-0841</entityCode>
        </GetReturnAnalysis>
    </env:Body>
</env:Envelope>

所以我对代码进行了一些小改动,改变了类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GetReturnAnalysis")
public class GetReturnAnalysisRequest {
  @XmlElement(name = "entityCode")
  protected String entityCode;

  public GetReturnAnalysisRequest(String entityCode) {
    this.entityCode = entityCode;
  }

  public GetReturnAnalysisRequest() { }

  public String getEntityCode() {
    return entityCode;
  }

  public void setEntityCode(String entityCode) {
    this.entityCode = entityCode;
  }        
}

和创建SOAP请求方法:

private  SOAPMessage createSOAPRequest(GetReturnAnalysis request) throws SOAPException, JAXBException, IOException, ParserConfigurationException, SAXException {
    MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
    SOAPMessage message = messageFactory.createMessage();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("", "http://www.address.com/integration");
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    message.getSOAPHeader().detachNode();

    SOAPBody body = envelope.getBody();
    String requestString =  XmlHelper.toOutputString(request);
    Document doc = convertStringToDocument(requestString);
    body.addDocument(doc);
    message.writeTo(System.out);

    message.saveChanges();
    message.writeTo(System.out);    

    return message;
  }

但是,出于一些神秘的原因,当我调用message.saveChanges时,GetReturnAnalysis类最终会这样:

<GetReturnAnalysis xmlns="">
    <EntityCode>186D3CAD-0841</EntityCode>
</GetReturnAnalysis>ityCode>

并且空的xmlns属性会覆盖我提供的整个命名空间。我想知道它为什么这样做?难道它不能简单地保存我想要保存的字符串而不进行更改吗?

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题

命名空间定义:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GetReturnAnalysis", namespace = "http://www.address.com/integration")
public class GetReturnAnalysisRequest {
  @XmlElement(name = "entityCode", namespace = "http://www.address.com/integration")
  protected String entityCode;

  public GetReturnAnalysisRequest(String entityCode) {
    this.entityCode = entityCode;
  }

  public GetReturnAnalysisRequest() { }

  public String getEntityCode() {
    return entityCode;
  }

  public void setEntityCode(String entityCode) {
    this.entityCode = entityCode;
  }   
}

第二 - 将文档构建器设置为名称空间感知

String requestString =  toOutputString(request);

Document doc = convertStringToDocument(requestString);
body.addDocument(doc);
message.writeTo(System.out);

-

private Document convertStringToDocument(String xml) throws SAXException, IOException, ParserConfigurationException {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);
    return docFactory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
  }

使用框架

对于使用Web服务,您应该使用框架。我建议CXF(我的首选)或Axis2。然后,您可以定义服务接口并生成正确的wsdl和客户端类,并获得对其他可选协议和扩展(例如安全性)的支持。

直接处理XML我只在特殊情况下提供建议,例如RPC-literal,自定义安全或其他不再支持的协议。