使用远程服务——WebService(Spring版)
使用Spring编写WebService示例
概述
在使用远程服务——WebService(JDK版)_java,webservice_szuaudi的博客-CSDN博客中,我们使用JDK开发了一个WebService实例程序,在本篇中,我们使用Spring来改写该实例程序。我们可以发现通过Spring我们可以像使用远程服务——RMI(spring版)_spring,rmi,java_szuaudi的博客-CSDN博客那样导出和装配web服务。
编码
新建工程
在eclipse中新建maven工程
输入工程名称为rmi-spring
点击“Finish”,这样就创建了一个空的maven工程
目录结构
在工程的pom.xml添加spring核心模块
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.aodi</groupId>
<artifactId>webservice-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<org.springframework.version>4.3.17.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
</project>
注意:与之前的pom.xml相比多了spring-web
模块
在工程中/webservice-spring/src/main/java添加如下包
- me.aodi包::应用程序跟目录;
- me.aodi.model:模拟数据模型;
- me.aodi.service:服务接口;
- me.aodi.service.impl:接口实现类;
在工程中/rmi-spring/src/main/resources中添加spring配置文件application-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="me.aodi.service.impl.UserServiceImpl"></bean>
</beans>
编写WebService功能
- 新建me.aodi.model.User类模拟数据模型;
package me.aodi.model;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注意:该模型类不需要再implements Serializable
- 新建
me.aodi.service.UserService
接口
package me.aodi.service;
import java.rmi.RemoteException;
import java.util.List;
import javax.jws.WebService;
import me.aodi.model.User;
@WebService(serviceName = "user")
public interface UserService{
/**
* 返回所有user
* @return
* @throws RemoteException
*/
public List<User> all();
}
注意:主要在WebServiceClient端使用,用来创建相应的代理bean
- 新建
me.aodi.service.impl.UserServiceImpl
实现类
package me.aodi.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import me.aodi.model.User;
import me.aodi.service.UserService;
@WebService(serviceName = "user", targetNamespace = "http://service.aodi.me/")
public class UserServiceImpl implements UserService {
/**
* 模拟返回所有user
*/
public List<User> all()
{
User user = new User();
user.setId(1);
user.setName("用户1");
List<User> list = new ArrayList<User>(1);
list.add(user);
return list;
}
}
注意:主要在WebServiceServer端使用,用来导出Web服务,@WebService注解与UserService接口的@WebService注解保持一致
- 新建
me.aodi.WebServerServer
服务端类
package me.aodi;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter;
public class WebServiceServer {
public static void main(String[] args) {
SimpleJaxWsServiceExporter exporter = new SimpleJaxWsServiceExporter();
/**
* 获取BeanFactory
*/
BeanFactory beanFactory = new DefaultListableBeanFactory();
ClassPathResource resource = new ClassPathResource("application-server.xml");
BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory);
beanDefinitionReader.loadBeanDefinitions(resource);
/**
* 设置BeanFactory
* 用于把Spring中加@WebService注解的bean导出为Web服务
*/
exporter.setBeanFactory(beanFactory);
exporter.setBaseAddress("http://localhost:8001/");
try {
exporter.afterPropertiesSet();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("spring start");
}
}
- 新建me.aodi.WebServiceClient客户端类
package me.aodi;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean;
import me.aodi.model.User;
import me.aodi.service.UserService;
public class WebServiceClient {
public static void main(String[] args) throws MalformedURLException {
JaxWsPortProxyFactoryBean proxy = new JaxWsPortProxyFactoryBean();
proxy.setWsdlDocumentUrl(new URL("http://localhost:8001/user?wsdl"));
proxy.setServiceInterface(UserService.class);
/**
* 对应@WebService中的name值
* 缺省值为 Java 类的简单名称 + Service。
*/
proxy.setServiceName("user");
/**
* 对应@WebService中的targetNamespace值
* 缺省值为从包含该 Web Service 的包名映射的名称空间
*/
proxy.setNamespaceUri("http://service.aodi.me/");
/**
* 对应@WebService中的portName值
* 缺省值为 WebService.name+Port
*/
proxy.setPortName("UserServiceImplPort");
proxy.afterPropertiesSet();
UserService userService = (UserService) proxy.getObject();
/**
* 调用远程service的方法
*/
List<User> users = userService.all();
System.out.println("userService.all().size:"+users.size());
if (users.size() > 0) {
System.out.println("userService.all()[0].name:"+users.get(0).getName());
}
}
}
运行实例
-
编辑后的工程目录结构
-
运行服务端应用
在WebServiceServer打断点,防止程序立马执行完退出
-
调试模式运行WebServiceServer.java
程序会在断点处停止 -
WebServiceServer的运行日志:
![WebServiceServer运行日志](https://img-blog.csdnimg.cn/20200319162654895.png -
运行WebServiceClient.java
WebServiceClient运行结束就退出了,控制台留下了运行时我们要输出的信息
注意: 如果运行异常,根据错误提示修改WebServiceClient中JaxWsPortProxyFactoryBean对象的参数
改进
在日常的web开发中,应用程序在web容器运行,远程服务的发布就不能在main方法中调用。
我们要做的只是定义一个SimpleJaxWsServiceExporter导出Web服务,定一个JaxWsPortProxyFactoryBean类装配RMI服务。
可以做出以下改进:
- 在spring的配置文件application-server.xml中一个发布Web服务的bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="me.aodi.service.impl.UserServiceImpl"></bean>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8001/"/>
</bean>
</beans>
- 在spring的配置文件application-client.xml中定义一个装配Web服务的bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="me.aodi.service.impl.UserServiceImpl"></bean>
<bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
<property name="baseAddress" value="http://localhost:8001/"/>
</bean>
</beans>
注意:afterPropertiesSet方法会在bean创建后自动触发
- WebServiceServer中只需要启动Spring
package me.aodi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WebServiceServer {
public static void main(String[] args) {
/**
* 启动spring
*/
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application-server.xml");
System.out.println("spring start");
((AbstractApplicationContext) applicationContext).close();
}
}
- WebServiceClient中获取装配好的Web服务接口,调用Web服务
package me.aodi;
import java.net.MalformedURLException;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import me.aodi.model.User;
import me.aodi.service.UserService;
public class WebServiceClient {
public static void main(String[] args) throws MalformedURLException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:application-client.xml");
UserService userService = applicationContext.getBean(UserService.class);
/**
* 调用远程service的方法
*/
List<User> users = userService.all();
System.out.println("userService.all().size:"+users.size());
if (users.size() > 0) {
System.out.println("userService.all()[0].name:"+users.get(0).getName());
}
applicationContext.close();
}
}