博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java】CXF
阅读量:5290 次
发布时间:2019-06-14

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

Apache CXF是一个开源框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构,它允许创建高性能和可扩展的服务,可以将服务部署在 Tomcat 和基于 Spring 的轻量级容器中。

准备架包:http://cxf.apache.org/;将lib包先所有jar包复制到项目目录下

创建web项目,项目结构如下图:

HelloWorldClient.java

package cn.oly.client;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import cn.oly.server.HelloWorld;import cn.oly.server.User;public class HelloWorldClient {    public static void main(String[] args) throws IOException {        // 首先右键run as 运行com.hsy.server.webServiceApp类,然后再运行这段客户端代码        JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean();        jwpfb.setServiceClass(HelloWorld.class);        jwpfb.setAddress("http://localhost:8080/webServices/helloWorld");        HelloWorld hw = (HelloWorld) jwpfb.create();        // System.out.println(hw.sayHi(readFile()));        User user = new User();        user.setName("tName");        user.setDescription("decName");        System.out.println(hw.sayHiToUser(user));    }    public static String readFile() {        // 使用字符流实现文件的读入        BufferedReader br = null;        StringBuffer sb = new StringBuffer();        String str = "";        int i = 0;        try {            br = new BufferedReader(new InputStreamReader(new FileInputStream(                    "client.txt"), "GBK"));            while ((str = br.readLine()) != null) {                if (str.trim() != null) {                    sb.append(str.trim());                }            }            System.out.println(str);        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (br != null) {                    br.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return sb.toString();    }}
View Code

 HelloWorld.java

package cn.oly.server;import javax.jws.WebParam;import javax.jws.WebService;import java.io.FileNotFoundException;import java.util.List;@WebServicepublic interface HelloWorld {    String sayHi(@WebParam(name="text")String text) throws FileNotFoundException;    String sayHiToUser(User user);    String[] SayHiToUserList(List
userList); } HelloWorldImpl.javapackage cn.oly.server;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import javax.jws.WebParam;import javax.jws.WebService;@WebService(endpointInterface="cn.oly.server.HelloWorld",serviceName="HelloWorld")public class HelloWorldImpl implements HelloWorld { Map
users = new LinkedHashMap
(); public String sayHi(@WebParam(name = "text") String text) { File file = new File("D:\\server.txt"); PrintStream ps = null; try { ps = new PrintStream(new FileOutputStream(file)); ps.println("测试文本内容");// 往文件里写入字符串 if(text.trim()!=null&&text.trim()!=""){ ps.append(text.toString()); } } catch (FileNotFoundException e) { e.printStackTrace(); }finally{ ps.close(); } return "Hello"; } 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; }}
View Code

User.java

package cn.oly.server;import java.io.Serializable;@SuppressWarnings("serial")public class User implements Serializable {    private String id;    private String name;    private String age;    private String description;    public User() {        super();    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }}
View Code

Web.java

package cn.oly.testServer;import cn.oly.server.HelloWorldImpl;import javax.xml.ws.Endpoint;public class Web {    public static void main(String[] args) {        System.out.println("web service start");        HelloWorldImpl implementor = new HelloWorldImpl();        String address = "http://localhost:8080/webServices/helloWorld";        Endpoint.publish(address, implementor);        System.out.println("web service started");    }}
View Code

applicationContext.xml

View Code

web.xml

webServices
index.jsp
contextConfigLocation
WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/*
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encoding
/*
View Code

访问路径:http://localhost:8080/webServices/helloWorld?wsdl

转载于:https://www.cnblogs.com/olyx/p/6409999.html

你可能感兴趣的文章
JavaScript基础---获取元素的属性(title,style,width)
查看>>
简单了解HashCode()
查看>>
闭包理解
查看>>
asp.net C#后台实现下载文件的几种方法(全)
查看>>
MySQL用户变量的用法
查看>>
HDU 2002 计算球体积
查看>>
Web前端开发工程师的具备条件
查看>>
为什么要用日志框架 Logback 基本使用
查看>>
实用Android开发工具和资源精选
查看>>
TileMap
查看>>
JS属性大全
查看>>
java复制文件
查看>>
第一册:lesson seventy nine.
查看>>
GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
查看>>
团队作业
查看>>
数据持久化时的小bug
查看>>
mysql中key 、primary key 、unique key 与index区别
查看>>
bzoj2257
查看>>
Linux查看文件编码格式及文件编码转换<转>
查看>>
Leetcode: Find Leaves of Binary Tree
查看>>