zoukankan      html  css  js  c++  java
  • Axis2 WebService(基于REST风格)

    Axis2除了提供传统的webservice方法外,还提供了对Rest的支持。Axis2同时提供WS-*风格的接口和REST风格的接口。除 了支持WS-*规范外,Axis2已经可以配置作为REST容器来发送和接收RESTful Web Service请求和应答。这让Axis2的灵活性、易用性、安全性和可靠性等都得到大幅度地提升。

    准备工作

    本文代码是基于Axis2Service2工程。你可以从 Axis2 WebService(配置、发布、调用)获得详细介绍,该文详细介绍如何发布、调用。

    新建Axis2Rest工程项目,将Axis2Service2代码复制过来。注意需要自己将jar文件复制到lib下,并加入工程引用。一切正常的话,访问 http://localhost:8086/Axis2Rest/services/AxisService?wsdl 将会看到如下界面。

    调用Axis2的rest服务

    1、AxisService提供showName方法,传递rest字符串,如下。

    http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest

    从上面可以看出这个就是rest风格。Axis1.0是无法通过showName?name=rest来获取信息的。

    2、使用axis客户端调用

    public class TestRest {

    private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService";

    public static void main(String[] args) throws AxisFault {

    Options options = new Options();
    options.setTo(new EndpointReference(toEpr));

    //客户端REST方式调用服务跟普通服务的区别,REST调用必须加上下面这个代码。
    options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

    ServiceClient sender = new ServiceClient();
    //axis2-1.5.4不需要下面这句代码,否则会报错
    //sender.engageModule(new QName(Constants.MODULE_ADDRESSING));
    sender.setOptions(options);
    OMElement result = sender.sendReceive(getPayload());

    try {
    XMLStreamWriter writer = XMLOutputFactory.newInstance()
    .createXMLStreamWriter(System.out);
    result.serialize(writer);
    writer.flush();
    } catch (XMLStreamException e) {
    e.printStackTrace();
    } catch (FactoryConfigurationError e) {
    e.printStackTrace();
    }
    }
    private static OMElement getPayload() {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace(
    "http://ws", "example1");
    OMElement method = fac.createOMElement("showName", omNs);
    OMElement value = fac.createOMElement("name", omNs);
    value.addChild(fac.createOMText(value, "Rest"));
    method.addChild(value);

    return method;
    }

    说明:
    1、sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不需要下面这句代码,否则会报错

    2、客户端REST方式调用服务跟普通服务的区别,就是Rest有下面这个代码;
    options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
    两者返回的数据都是

    <ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>

    3、getPayload方法

    OMNamespace omNs = fac.createOMNamespace("http://ws", "example1"); 指定命名空间,如果没对的话会报如下错误<faultstring>namespace mismatch require http://ws found http://ws1</faultstring>

    OMElement method = fac.createOMElement("showName", omNs); 要传递的方法名为 "showName"

    OMElement value = fac.createOMElement("name", omNs); 传递的参数为name

    value.addChild(fac.createOMText(value, "Rest"));  传递参数name的值为Rest。

     
  • 相关阅读:
    在一个页面用Ajax访问另一个页面弹出询问框怎么办
    还有一张呢
    系统相关信息查看
    linux dd 命令使用说明
    Github 使用说明
    Hadoop HA 资料
    fedora 在virtualbox安装guest additions
    git 使用记录
    linux console 小技巧
    Agent admitted failure to sign using the key 错误及解决方法
  • 原文地址:https://www.cnblogs.com/duanxz/p/4379800.html
Copyright © 2011-2022 走看看