2014-05-03 55 views
0

我贴过这个问题昨天:How to go about creating SOAP webservice in Java和下面的这本书后:http://www.packtpub.com/java-7-jax-ws-web-services/book我已成功地创建了JAX-WS应用程序:传递XML数据,SOAP服务两难

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!"; 

    } 
} 

一切都很好。但我需要的是服务方法(即“你好”)应改为“串”接受描述学生信息的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数据格式吗?

回答

0

我已经使用AXIS2做了类似的事情。所以,我的回答也许能够给你一些希望:)

的services.xml文件: 以这样的方式,它的messageReceiver类类型的配置您hello操作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(); 
     } 
}