Spring+webservice写一个test小实例

用到的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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值