将XML数据传递给SOAP服务困境

时间:2014-05-03 09:27:48

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

我昨天发布了这个问题: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!";

    }
}

一切都很好。但我需要的是服务方法(即"你好")应该代替" 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对象&#34;传递&#34; /&#34;失败&#34;基于一些算法。

所以我的问题是:

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

1 个答案:

答案 0 :(得分:0)

我使用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();  
        }
}
相关问题