1.工具:Eclipse mars2.0 + cxf 3.2.1 + spring 4.3.8 + jdk1.8 + tomcat 8.0.48
2.新建web项目 zcxfservice;导入cxf和spring的jar(一股脑).cxf.xml可以从cxf-core-xx.jar中取得(放在meta-inf就行了?);
服务端的文件:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>zxcfservice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- cxf3以后,只需要引入这个配置文件即可 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxws:endpoint id="userService"
implementor="com.java.webservice.service.impl.UserServiceImpl"
address="/userWS">
</jaxws:endpoint>
</beans>
User.class:
package com.java.webservice.bean;
/**
* @author zhang
* @desc User bean
*/
public class User {
private long id;
private String name;
private int age;
public User(long id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
IUserService.interface:
package com.java.webservice.service;
import java.util.List;
import javax.jws.WebService;
import com.java.webservice.bean.User;
/**
* @author zhang
* @desc Interface
*/
@WebService
public interface IUserService {
public User getUserById(long id);
public List<User> getAllUsers();
}
UserServiceImpl.class:
package com.java.webservice.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.java.webservice.bean.User;
import com.java.webservice.service.IUserService;
/**
* @author zhang
* @desc 实现IUserService接口
*/
@WebService
public class UserServiceImpl implements IUserService {
@Override
public User getUserById(long id) {
User user = new User(100000, "shelley", 30);
System.out.println(user.toString());
return user;
}
@Override
public List<User> getAllUsers() {
List<User> list = new ArrayList<>();
User user = new User(1000001, "tom", 29);
User user1 = new User(100002, "jerry", 19);
User user2 = new User(100003, "adom", 39);
list.add(user);
list.add(user1);
list.add(user2);
System.out.println(list.toString());
return list;
}
}
tomcat8:在server.xml中<host>内增加 :
<Context path="/cxfserver" docBase="D:dlearningsworkeclipse_workspace1zcxfserviceWebContent" reloadable="true"/>
启动tomcat,在浏览器中输入发布后的wsdl地址:
http://localhost:8080/cxfserver/webservice/userWS?wsdl 如果出现下图,说明成功:
3.客户端
新建web项目zcxfclient,依样导入jar;在zcxf的src目录下,shift+鼠标右键调出cmd窗口
在
cmd窗口:
执行命令: wsimport -keep http://localhost:8080/cxfserver/webservice/userWS?wsdl;本地src目录下会产生相应的class.刷新项目,得到java类;
client-bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="userClient"
serviceClass="com.java.webservice.service.impl.IUserService"
address="http://localhost:8080/cxfserver/webservice/userWS">
</jaxws:client>
</beans>
测试类:UserWSClientTest.class:
package com.webservice.client;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java.webservice.service.User;
import com.java.webservice.service.impl.IUserService;
public class UserWSClientTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
new String[] { "client-bean.xml" });
IUserService userService = (IUserService) classPathXmlApplicationContext.getBean("userClient");
User user = userService.getUserById(100000);
System.out.println(user.getId());
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println("-------------------");
List<User> allUsers = userService.getAllUsers();
for (User u : allUsers) {
System.out.println(u.getId());
System.out.println(u.getName());
System.out.println(u.getAge());
System.out.println("-------------------");
}
}
}
启动main方法,测试. 出现结果,完成.