Spring+webservice写一个test小实例

来源:互联网 发布:淘宝卖家提现收费吗 编辑:程序博客网 时间:2024/04/30 06:57

用到的jar包如图,其中有些不用的,在这里啊懒得整理了


第一步

先写一个OfService.java接口方法吧。


import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface OfService {
  public String doSomeThing(@WebParam(name="content") String content);
}

doSomeThing就是被调用的接口方法了

第二步:

再写一个OfService.java接口的实现类吧:OfServiceImpl


public String doSomeThing(String content) {
//     String res="";
//     //模拟调用接口传递的sml参数为string格式的
//     String strxml="<?xml version='1.0' encoding='UTF-8' standalone='yes'?><treatyroot>节点和值自己写</treatyroot>";
//     //将string装换为xml文件
//     try {
// Document document = DocumentHelper.parseText(strxml);
// Element root = document.getRootElement();
//
// System.out.println(root.getName());//得到根节点
//
// Iterator iter = root.elementIterator("ordercont"); // 获取根节点下的子节点ordercont
//            // 遍历ordercont节点
//                while (iter.hasNext()) {
//
//                    Element itemEle = (Element) iter.next();
//
//                    String weborder = itemEle.elementTextTrim("weborder"); // 拿到ordercont子节点weborder的值
//                    String orderstatus = itemEle.elementTextTrim("orderstatus");
//                    String logistictime = itemEle.elementTextTrim("logistictime");
//
//                    System.out.println("weborder:" + weborder);
//                    System.out.println("orderstatus:" + orderstatus);
//                    System.out.println("logistictime:" + logistictime);
//            }
//     } catch (DocumentException e) {
// e.printStackTrace();
// }
   
   
//返回拼接xml远程调用参数
String inString ="<?xml version='1.0' encoding='UTF-8'?>";
inString +="<interfaceRoot>";
inString +="<returnCode>0</returnCode>";
inString +="<returnMessage>成功</returnMessage>";
inString +="</interfaceRoot>";


        return inString;
    }


在这个类里面主要是解析别人传进来得xml,所以在这里模拟写了一个string格式的xml字符串,然后将字符串通过Document 转换成xml在进行解析,其中解析的代码我都注释了,可以自己看,最后当然要返回一个信息给调用者反馈哦


第三步:

配置spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"  
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://cxf.apache.org/jaxws  
            http://cxf.apache.org/schemas/jaxws.xsd"
           >
           
            

<!-- 使用annotation注解方式配置事务
<tx:annotation-driven transaction-manager="transactionManager"/> -->

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint  id="ofService" implementor="webservice.serviceimpl.OfServiceImpl" address="/ofService" />
    
</beans>



好了,做到这些,当然要测试下自己的webservice通了没有,下面是我的调用test实例数据,main方法我就不写啦


JaxWsDynamicClientFactory factory =JaxWsDynamicClientFactory.newInstance();
Client client =factory.createClient("http://localhost:8080/test/webservice/ofService?wsdl"); 
Object[] res =client.invoke("doSomeThing", new Object[]{"入参的值"});
System.out.println("返回的数据实是:"+res[0]);


0 0
原创粉丝点击