CXF+Spring配置
1.服务端
接口定义:
@WebService
public interface UserService {
String login();
User findById(User user);
List<User> getUserList();
}
实现类:
@WebService
public class UserServiceImpl implements UserService {
@Override
public String login() {
return "login success";
}
@Override
public User findById(User user) {
return user;
}
@Override
public List<User> getUserList() {
return list;
}
}
发布CXF WebService服务:
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-3.1.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" />
<!-- 1.使用jaxws:enpoint发布服务 -->
<jaxws:endpoint address="/myService" implementor="com.demo.UserServiceImpl" />
<!-- 2.使用jaxws:server发布服务 -->
<bean id="UserServiceImpl" class="com.demo.UserServiceImpl" />
<jaxws:server serviceClass="com.demo.UserService" address="/myService">
<jaxws:serviceBean>
<ref bean="UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
=============================================================================
2.客户端调用
相同接口:
public interface UserService {
String login();
User findById(User user);
List<User> getUserList();
}
调用方式1:
public static void test1() {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8080/CXFDemo/ws/myService");
UserService userDao = (UserService) factory.create();
User u = userDao.findById(new User(1, "aa", "234"));
System.out.println(u);
}
调用方式2:
先要在applicationContext.xml中配置:
<jaxws:client id="userService" serviceClass="com.demo.UserService"
address="http://localhost:8080/CXFDemo/ws/myService"></jaxws:client>
public static void test2() {
ApplicationContext ct = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
//UserService us = (UserService) ct.getBean("userService");
UserService us = (UserService) ct.getBean(UserService.class);
List<User> list = us.getUserList();
System.out.print(list);
}
1.服务端
接口定义:
@WebService
public interface UserService {
String login();
User findById(User user);
List<User> getUserList();
}
实现类:
@WebService
public class UserServiceImpl implements UserService {
@Override
public String login() {
return "login success";
}
@Override
public User findById(User user) {
return user;
}
@Override
public List<User> getUserList() {
return list;
}
}
发布CXF WebService服务:
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-3.1.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" />
<!-- 1.使用jaxws:enpoint发布服务 -->
<jaxws:endpoint address="/myService" implementor="com.demo.UserServiceImpl" />
<!-- 2.使用jaxws:server发布服务 -->
<bean id="UserServiceImpl" class="com.demo.UserServiceImpl" />
<jaxws:server serviceClass="com.demo.UserService" address="/myService">
<jaxws:serviceBean>
<ref bean="UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
=============================================================================
2.客户端调用
相同接口:
public interface UserService {
String login();
User findById(User user);
List<User> getUserList();
}
调用方式1:
public static void test1() {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://localhost:8080/CXFDemo/ws/myService");
UserService userDao = (UserService) factory.create();
User u = userDao.findById(new User(1, "aa", "234"));
System.out.println(u);
}
调用方式2:
先要在applicationContext.xml中配置:
<jaxws:client id="userService" serviceClass="com.demo.UserService"
address="http://localhost:8080/CXFDemo/ws/myService"></jaxws:client>
public static void test2() {
ApplicationContext ct = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
//UserService us = (UserService) ct.getBean("userService");
UserService us = (UserService) ct.getBean(UserService.class);
List<User> list = us.getUserList();
System.out.print(list);
}