CXF之简单实例

下载CXF

http://cxf.apache.org/download.html

以apache-cxf-3.0.12为例

 

新建一个web项目

 

引入cxf  lib下所有的包(图个方便)自带了spring相关包

 

编写一个UserService接口

package com.zns.ws;

import javax.jws.WebService;

@WebService
public interface UserService {
    public String func1(String name);
}

 

编写一个UserServiceImpl实现类

package com.zns.ws.impl;

import javax.jws.WebService;
import com.zns.ws.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public String func1(String name) {
        return "hello: "+name;
    }
}

 

增加一个spring-cxf.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:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
    
      <!-- Import Apache CXF Bean Definition -->
    <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 implementor="com.zns.ws.impl.UserServiceImpl" address="/user">
    </jaxws:endpoint>

</beans>

 

 

修改web.xml

<!-- Spring Config Location -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-cxf.xml</param-value>
    </context-param>
    <!-- Spring ContextLoaderListener -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Apache CXFServlet -->
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- CXFServlet Mapping -->
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

 

 

运行项目 访问测试

http://localhost:8080/项目名/ws/

 

http://localhost:8080/项目名/ws/user?wsdl

 

 

调用webservice

 

1. wsdl2java调用

 

用cxf的wsdl2java工具生成客户端代码调用

设置系统环境变量

CXF_HOME=D:\apache-cxf-xxx

在path后面加上 %CXF_HOME%/bin;

在cmd命令中输入wsdl2java,如果有提示usage,就表明配置成功

 

wsdl2java -p 生成代码的包名 -d 生成目录 -all  xxx.wsdl

-p  指定其wsdl的命名空间,也就是要生成代码的包名:

-d  指定要产生代码所在目录

-client 生成客户端测试web service的代码

-server 生成服务器启动web service的代码

-impl 生成web service的实现代码

-ant  生成build.xml文件

-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file

 

wsdl2java -p com.zns.ws.output -d D:\EclipseWorkSpace\TestClient\src -all http://localhost:8080/项目名/ws/user?wsdl

 

执行命令后 刷新工程文件 编写测试代码测试

package com.zns.ws.test;

import com.zns.ws.output.UserService;
import com.zns.ws.output.UserServiceImplService;

public class test1 {
    public static void main(String[] args) {
        UserService userService= new UserServiceImplService().getUserServiceImplPort();
        String result=userService.func1("abc");
        System.out.println(result);
    }
}

 

 

2.jquery调用

可以下载soapui工具 来获取soap请求体

var data='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.zns.com/"><soapenv:Header/><soapenv:Body><ws:func1><arg0>zhangsan</arg0></ws:func1></soapenv:Body></soapenv:Envelope>';
$.post("http://localhost:8080/项目名/ws/user",data, function(res, status) {
    var $result=$(res);
    var value=$result.find("return").text();
    alert(value);
},"xml");

 

 

3.HttpURLConnection调用

public static void main(String[] args) throws Exception {
        // 服务的地址
        URL wsUrl = new URL("http://localhost:8080/项目名/ws/user");
        // soap请求体
        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.zns.com/\"><soapenv:Header/><soapenv:Body><ws:func1><arg0>zhangsan</arg0></ws:func1></soapenv:Body></soapenv:Envelope>";
        
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");

        OutputStream os = conn.getOutputStream();
        os.write(soap.getBytes());

        InputStream is = conn.getInputStream();

        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while ((len = is.read(b)) != -1) {
            String ss = new String(b, 0, len, "UTF-8");
            s += ss;
        }
        System.out.println(s);

        is.close();
        os.close();
        conn.disconnect();
    }

 

 

打印输出如下

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:func1Response xmlns:ns2="http://ws.zns.com/">
<return>hello: zhangsan</return>
</ns2:func1Response>
</soap:Body>
</soap:Envelope>

 

转载于:https://www.cnblogs.com/zengnansheng/p/10389301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值