您的位置:首页 > 其它

ajax调用cxf webservice接口和跨域

2016-11-16 09:44 525 查看
1.配置webservice

(可以参考博客上cxf  webservice的配置http://blog.csdn.net/zhshchilss/article/details/43763271)

按照上述配置,就可以使用java客户端调用接口了

ajax的调用

1.额外的配置

新增两个jar包:cors-filter-1.7.jar,java-property-utils-1.9.jar

web.xml新增filter:

<filter>  

    <filter-name>CORS</filter-name>  

    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>  

    <init-param>  

     <param-name>cors.allowOrigin</param-name>  

        <param-value>*</param-value>  

    </init-param>  

    <init-param>  

     <param-name>cors.supportedMethods</param-name>  

        <param-value>GET, POST, HEAD, PUT, DELETE</param-value>  

    </init-param>  

    <init-param>  

     <param-name>cors.supportedHeaders</param-name>  

        <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>  

    </init-param>  

    <init-param>  

        <param-name>cors.exposedHeaders</param-name>  

        <param-value>Set-Cookie</param-value>  

    </init-param>  

    <init-param>  

        <param-name>cors.supportsCredentials</param-name>  

        <param-value>true</param-value>  

    </init-param>  

</filter>  

<filter-mapping>  

    <filter-name>CORS</filter-name>  

    <url-pattern>/*</url-pattern>  

</filter-mapping>

上述配置是解决跨域问题的

后台webservice接口

@WebService

public class Hello implements IHello{

    

    public String sayHello(@WebParam(name="name") String name){

        return  "hello: " + name;

    }

public List<Object> sayList() {
Person p = new Person();
p.setName("刘胡兰");
p.setAge(22);
List<Object> list = new ArrayList<Object>();
list.add("2") ;
list.add("你好");
list.add(p);
// TODO Auto-generated method stub
return list;
}

/**
* 不知道怎么传对象
*/
public Person sayMap(String id , Person person ) {
Person p = new Person();
p.setName("刘胡兰");
p.setAge(22);
// TODO Auto-generated method stub
return p;
}

    public String findData(String type) {

    JSONObject obj = new JSONObject() ;

    if(type.equals("string")){

    obj.put("data", sayHello("刘德华")) ;

    }else if(type.equals("map")){

    obj.put("data", sayMap(null, null)) ;

    }else if(type.equals("list")){

    obj.put("data", sayList()) ;

    }

    return obj.toJSONString() ;
}

ajax调用代码:

//ajax的各个属性,也可以换一下,看看都报什么问题

$.ajax({

//type必须get

    type: 'get',
//contentType: 'application/json',

//这个路径,webs是工程名,IService是在beans.xml配置的接口Hello的路径,findData是Hello里的方法,方法不用配置,调用哪个直接/方法名就可以

    url: 'http://*.*.*.*:8080/webs/IService/findData',

//参数名必须arg0

    data:{arg0:'list'},

//返回必须xml

    dataType: 'xml',

    success: function(result) {
var nodes = result.getElementsByTagName("return") ;
var value = nodes[0].firstChild.nodeValue ;
alert(value);

    },

    error: function(result, status) {

        alert('error='+status);

    }

});

期间遇到的问题:

1.ajax使用get之外的调用方式,后台报错(网上没找到解决办法)

2.后台的入参如public Person sayMap(String id , Person person ) ,

ajax如果传数据啊{id:1},后台报错。传  data:{arg0:'list'},后台就可以接到。

但是如果多个参数,第二个之后的还没找到传参字段是啥

3.dataType需要是xml,因为返回的数据是xml格式的东西

4.success里面解析数据,得到返回的return

其中,接口里面的四个方法

返回string的接口,ajax接的的数据为:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:sayHelloResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return>hello: null</return>

</ns2:sayHelloResponse>

</soap:Body>

</soap:Envelope>

返回list的接口,ajax返回数据为:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:sayListResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">2</return>

<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">你好</return>

<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:person">

<age>22</age>

<name>刘胡兰</name>

</return>

</ns2:sayListResponse>

</soap:Body>

</soap:Envelope>

返回vo的接口,ajax返回数据位:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:sayMapResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return>

<age>22</age>

<name>刘胡兰</name>

</return>

</ns2:sayMapResponse>

</soap:Body>

</soap:Envelope>

如上三中,如果解析return的数据,不容易分辨解析vo,list,string

所以这里对于ajax调用的接口,后台一律定义为

    public String findData(String type) {

     JSONObject obj = new JSONObject() ;

//测试各种情况

     if(type.equals("string")){

     obj.put("data", sayHello("刘德华")) ;

     }else if(type.equals("map")){

     obj.put("data", sayMap(null, null)) ;

     }else if(type.equals("list")){

     obj.put("data", sayList()) ;

     }

     return obj.toJSONString() ;
}

一个入参,字符串或json字符串

所有返回,都返回json字符串

然后前台的接到的数据为:

json

 string

 

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:findDataResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return>{"data":"hello: 刘德华"}</return>

</ns2:findDataResponse>

</soap:Body>

</soap:Envelope>

vo:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:findDataResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return>{"data":{"age":22,"name":"刘胡兰"}}</return>

</ns2:findDataResponse>

</soap:Body>

</soap:Envelope>

list

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns2:findDataResponse xmlns:ns2="http://test.zhshch.com.webs/">

<return>{"data":["2","你好",{"age":22,"name":"刘胡兰"}]}</return>

</ns2:findDataResponse>

</soap:Body>

</soap:Envelope>

通过解析得到json字符串

success: function(result) {
var nodes = result.getElementsByTagName("return") ;
var value = nodes[0].firstChild.nodeValue ;

//value的数据结构{data:****} ;
alert(value);

//然后把value转成json对象,json.data就是后台传过来的vo,list,字符串等

    },

这是ajax调用webservice的例子

网上找了很多其他的例子,但是在本地都一直报错。

这里是我本地可以使用的例子
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: