简体   繁体   English

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

[英]Passing XML data to SOAP service dilemma

I had posted this question yesterday: How to go about creating SOAP webservice in Java and after following this book: http://www.packtpub.com/java-7-jax-ws-web-services/book I have managed to created a JAX-WS application: 我昨天发布了这个问题:在遵循本书之后, 如何用Java创建SOAP Web服务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!";

    }
}

All is fine. 一切都很好。 But what I need is that the service method(ie "hello") should instead of "string" accept an XML file describing student details(I've changed the original file): 但是我需要的是服务方法(即“ 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>

Then the service will process it and return a Java Object containing a flag "passed"/"failed" based on some algo. 然后,该服务将对其进行处理,并根据某种算法返回一个包含标志“通过” /“失败”的Java对象。

So my question is: 所以我的问题是:

  • How should service method receive this XML data? 服务方法应如何接收此XML数据? As a String? 作为字符串? or some other way? 或其他方式?
  • Will I need to describe this XML data format in .wsdl and .xsd files within the config folder? 我是否需要在config文件夹中的.wsdl和.xsd文件中描述此XML数据格式?

I have done a similar thing using AXIS2. 我已经使用AXIS2做过类似的事情。 So, my answer might be able to give you some hope :) 因此,我的回答可能会给您一些希望:)

In services.xml file: Configure your hello operation in such a way that its messageReceiver class is of type org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver services.xml文件中:以这种方式配置您的hello操作,使其messageReceiver类的类型为org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver

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

Then, you can process the incoming XML element inside your hello method. 然后,您可以在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();  
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM