博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webservice整合spring cxf
阅读量:4687 次
发布时间:2019-06-09

本文共 4448 字,大约阅读时间需要 14 分钟。

下载cxf包,把他里面的包都添加进lib文件夹中。

创建一个接口。添加@WebService注解

@WebServicepublic interface HelloWorld {    String sayHi(@WebParam(name="text")String text);    String sayHiToUser(User user);    String[] SayHiToUserList(List
userList);}

创建接口的实现类,也添加@WebService注解,给他一个名称:boy

@WebService(serviceName="boy")public class HelloWorldImpl implements HelloWorld {    Map
users = new LinkedHashMap
(); public String sayHi(String text) { return "Hello " + text; } public String sayHiToUser(User user) { users.put(users.size() + 1, user); return "Hello " + user.getName(); } public String[] SayHiToUserList(List
userList) { String[] result = new String[userList.size()]; int i = 0; for (User u : userList) { result[i] = "Hello " + u.getName(); i++; } return result; }}

 

User实体类:

public class User {    private String name;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    }

服务端代码:

public class webServiceApp {      public static void main(String[] args) {          System.out.println("web service start");          HelloWorldImpl implementor= new HelloWorldImpl();          String address="http://localhost:8000/helloWorld";          Endpoint.publish(address, implementor);          System.out.println("web service started"); }}

客户端代码:有集成spring和没集成的

public class HelloWorldClient {    public static void main(String[] args) {                //集成spring和cxf        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld client1 = (HelloWorld) context.getBean("client1");        User user1 = new User();        user1.setName("Tony");        user1.setAge(20);        User user2 = new User();        user2.setName("freeman");        user2.setAge(50);        List
userList = new ArrayList
(); userList.add(user1); userList.add(user2); // String[] res = client1.SayHiToUserList(userList); // System.out.println(res[0]); // System.out.println(res[1]); String sayHi = client1.sayHi("good"); System.out.println(sayHi); // HelloWorld2 client2 = (HelloWorld2) context.getBean("client2"); // User user3 = new User(); // user3.setName("Jerry"); // user3.setAge(20); // String user = client2.sayHiToUser(user3); // System.out.println(user); //没有集成 JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(HelloWorld.class); svr.setAddress("http://localhost:8000/helloWorld"); HelloWorld hw = (HelloWorld) svr.create(); User user = new User(); user.setName("Tony"); System.out.println(hw.sayHi("ffff")); }}

打印的结果是:

Hello good

Hello ffff

 

web.xml的配置

cxf
index.jsp
contextConfigLocation
classpath*:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/*

applicationContext.xml的配置,这个文件是放在资源文件resources下的,他在项目中的路径是在classes里面的。我在这里配置了两个站点。都是可以的。

aaa前面的路径必须与发布的String address="http://localhost:8000/helloWorld";路径相同。

转载于:https://www.cnblogs.com/hjy9420/p/4270173.html

你可能感兴趣的文章
测试计划的编写
查看>>
Entity Framework 4.1 CodeFirst实例
查看>>
java提高篇(十九)-----数组之二
查看>>
hibernate注解方式实现一对多映射
查看>>
记录利用CSS完美解决前端图片变形问题
查看>>
UVA 10791 最小公倍数的和
查看>>
CodeForces–830B--模拟,树状数组||线段树
查看>>
学习——java内存模型
查看>>
标准输入输出重定向
查看>>
C# 命名管道
查看>>
Chinese_PRC
查看>>
Beta冲刺总结
查看>>
windows 10下让jar文件双击可以运行的解决方法
查看>>
WITH (NOLOCK)提高查询效率
查看>>
MyEclipse搭建安卓环境
查看>>
事件委托的使用
查看>>
异常检测(Anomaly Detection)
查看>>
eclipse常用快捷键
查看>>
Elasticsearch 健康状态处理
查看>>
Winfrom 线程实现 http、https 文件下载 显示下载进度详情
查看>>