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(); } }
HelloWorld.java
package cn.oly.server; import javax.jws.WebParam; import javax.jws.WebService; import java.io.FileNotFoundException; import java.util.List; @WebService public interface HelloWorld { String sayHi(@WebParam(name="text")String text) throws FileNotFoundException; String sayHiToUser(User user); String[] SayHiToUserList(List<User> userList); } HelloWorldImpl.java package 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<Integer, User> users = new LinkedHashMap<Integer, User>(); 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<User> userList) { String[] result = new String[userList.size()]; int i = 0; for(User u:userList){ result[i] = "Hello " + u.getName(); i++; } return result; } }
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; } }
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"); } }
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="helloWorld" implementor="cn.oly.server.HelloWorldImpl" address="/helloWorld" /> <bean id="client" class="cn.oly.server.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="cn.oly.server.HelloWorld"/> <property name="address" value="http://localhost:8080/webServices/helloWorld"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>webServices</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <!-- 字符过滤器 --> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
访问路径:http://localhost:8080/webServices/helloWorld?wsdl