欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

应用cxf中的aegis开发ws使用

更新时间: 2014-01-05 02:58:57 责任编辑: Author_N1

 

使用CXF中的Aegis开发WS使用

package com.easyway.cxf.service;

import java.util.List;

import com.easyway.cxf.model.User;
/**
 *
 * @author longgangbai
 *
 */
public interface HelloService {
 /**
  * The @WebParam annotation is necessary as java interfaces do not store the Parameter name in the .class file. So if you leave out the annotation your parameter will be named arg0.
  * @param name
  * @return
  */
  public String hello(String name);
 
  /**
   * Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
   * support interfaces directly.  Special XmlAdapter classes need to
   * be written to handle them
   */
  public String sayHi(User user);

  public String[] getAllUseNames(List<User> userList);
}
package com.easyway.cxf.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.easyway.cxf.model.User;
/**
 * 采用Aegis CXF 的使用发布WS
 * @author longgangbai
 *
 */
public class HelloServiceImpl implements HelloService {

    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
   

 public String hello(String username) {
        return "Hello " + username;
 }

 public String sayHi(User user) {
         users.put(users.size() + 1, user);
         return "Hello "  + user.getUsername();
 }
 public String[] getAllUseNames(List<User> userList) {
  String[] userListArr=new String[userList.size()];
  for (int i=0;i<userList.size();i++) {
   userListArr[i]=userList.get(i).getUsername();
  }
  return userListArr;
 }

}

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<mappings>
    <mapping name="HelloService">
        <method name="hello">
            <parameter index="0" name="greeting" nillable='false' />
        </method>
    </mapping>
</mappings>

 

 

package com.easyway.cxf.test.server;

import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ServerFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.service.HelloServiceImpl;

public class AegisCXFServer {
 public static final String SERVICE_ADDRESS="http://localhost:8080/services/helloService";
      public static void main(String args[]) throws Exception {
       HelloService helloworldImpl = new HelloServiceImpl();
          ServerFactoryBean svrFactory = new ServerFactoryBean();
          svrFactory.setServiceClass(HelloService.class);
          svrFactory.setAddress(SERVICE_ADDRESS);
          svrFactory.setServiceBean(helloworldImpl);
          svrFactory.getServiceFactory().setDataBinding(new AegisDatabinding());
          svrFactory.create();
          System.out.println("Server ready...");
 
          Thread.sleep(5 * 60 * 1000);
          System.out.println("Server exiting");
          System.exit(0);
      }
 }
package com.easyway.cxf.test.client;


import org.apache.cxf.aegis.databinding.AegisDatabinding;
import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.easyway.cxf.service.HelloService;
import com.easyway.cxf.test.server.AegisCXFServer;

 

public final class AegisCXFClient {

    private AegisCXFClient() {
    }

    public static void main(String args[]) throws Exception {
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(HelloService.class);
        if (args != null && args.length > 0 && !"".equals(args[0])) {
            factory.setAddress(args[0]);
        } else {
            factory.setAddress(AegisCXFServer.SERVICE_ADDRESS);
        }
        factory.getServiceFactory().setDataBinding(new AegisDatabinding());
        HelloService client = (HelloService)factory.create();
        System.out.println("Invoke hello()....");
        System.out.println(client.hello(System.getProperty("user.name")));
        System.exit(0);
    }

}

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:58:57
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/16957/
本文WWW.DOC100.NET DOC100.NET版权所有。