【问题标题】:Passing XML data to SOAP service dilemma将 XML 数据传递给 SOAP 服务的困境
【发布时间】:2014-05-03 09:27:48
【问题描述】:

我昨天发布了这个问题:How to go about creating SOAP webservice in Java 并在关注这本书之后:http://www.packtpub.com/java-7-jax-ws-web-services/book 我已经成功地创建了一个 JAX-WS 应用程序:

package hellows;
import javax.jws.*;
@WebService(portName = "HelloWSPort", serviceName = "HelloWSService", targetNamespace = "http://hellows/", endpointInterface = "hellows.HelloWS")
public class HelloWSImpl implements HelloWS {
    public String hello(String name) {
        // replace with your impl here
         return "Hello "+name +" Welcome to Web Services!";

    }
}

一切都很好。但我需要的是服务方法(即“hello”)应该而不是“string”接受描述学生详细信息的 XML 文件(我已经更改了原始文件):

<?STU version="1.0"?>
<stu>    
        <id sequence="1">2354282</id>
        <date>2012-06-17T21:19:15</date>
        <student interest="food" status="newadmission">
            <id></id>
            <birthyear>2012</birthyear>
            <sex>Male</sex>
            <address>Sonata</address>
            <class>3</class>
         </student>


</stu>

然后服务将处理它并返回一个 Java 对象,其中包含基于某种算法的标志“通过”/“失败”。

所以我的问题是:

  • 服务方法应该如何接收这个 XML 数据?作为字符串?还是其他方式?
  • 我需要在 config 文件夹中的 .wsdl 和 .xsd 文件中描述这种 XML 数据格式吗?

【问题讨论】:

    标签: java web-services soap wsdl jax-ws


    【解决方案1】:

    我使用 AXIS2 做过类似的事情。所以,我的回答或许能给你一些希望:)

    services.xml 文件中: 配置hello 操作,使其messageReceiver 类的类型为org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver

    <operation name="hello"> 
    <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/> 
    </operation>
    

    然后,您可以在 hello 方法中处理传入的 XML 元素。

    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.xpath.AXIOMXPath;
    import org.jaxen.JaxenException;
    
    public String hello(OMElement xmlElement) {
            try {
                AXIOMXPath someXPath = new AXIOMXPath("//root/child");
            String str = ((OMElement)someXPath.selectSingleNode(node)).getText();
            } catch (JaxenException e) {
                e.printStackTrace();  
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2019-07-23
      • 2016-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多