使用远程服务——WebService(Spring版)

使用Spring编写WebService示例

概述

使用远程服务——WebService(JDK版)_java,webservice_szuaudi的博客-CSDN博客中,我们使用JDK开发了一个WebService实例程序,在本篇中,我们使用Spring来改写该实例程序。我们可以发现通过Spring我们可以像使用远程服务——RMI(spring版)_spring,rmi,java_szuaudi的博客-CSDN博客那样导出和装配web服务。

编码

新建工程

在eclipse中新建maven工程
新建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运行结束就退出了,控制台留下了运行时我们要输出的信息
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();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值