WebService在ssm框架中的简单应用

WebService的概念

Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序。
最近学习了webservice给自己做个学习记录。要是有不懂的同学可以看看我这篇博客。当然前提是你已经学习了spring框架,最好是学习了ssm框架。
webservice和java比较像,因为他主要是xml文件在不同系统中进行沟通,具有跨平台性。我这里主要学习的是RPC远程调用,需要服务器暴露出接口给客户端实现,接口比较安全。
首先在maven中依赖webservice的jar包
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>3.1.8</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>3.1.8</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-core</artifactId>
	<version>3.1.8</version>
</dependency>

然后在interface接口中使用@WebService注解暴露接口

在服务器的war工程中创建springbean的配置文件,命名空间选上beans,context,jaxws三个,贴代码(当你的interface使用了webservice注解时你就需要在cxf配置文件中配置,将接口暴露出去,注意jaxws中配置的是继承了的serviceImpl(关于这一点我还不是很了解)):

<?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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
		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-4.3.xsd">
	<jaxws:server address="user">
		<jaxws:serviceBean>
			<ref bean="xxxxServiceImpl" />
		</jaxws:serviceBean>
	</jaxws:server>
	
</beans>

配置文件写完了记得在服务端的war工程中的web.xml中配置,要不然是扫描不到你的配置文件的,由于服务端没有用到springmvc,就需要用到tomcat监听器了。当然还要加上webservice自带的中央控制器。

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext_*.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 加入cxf 这个框架的 给我们提供的 一个中央控制器 -->
	<!-- cxf webservices -->
	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
</web-app>

好了,服务端到这里还不算完成哦,还需要mvn -install,将整个项目打包到仓库才算是大功告成,接下来是客户端的配置。客户端差不了多少,这里先明确需要解决的问题:当系统一分为二时怎么在客户端调用服务端的service,spring肯定不能直接注入。

那么上面的打包就起到作用了首先,在maven依赖你上传到服务器的jar包 (注意是依赖接口就行,需要哪些接口只要MAVEN -INSTALL后都可以依赖):

		<dependency>
  			<groupId>com.tym</groupId>
		    <artifactId>xxx_interface</artifactId>
		    <version>0.0.1-SNAPSHOT</version>
  		</dependency>
依赖接口后就需要在客户端同样配置一个spring的bean文件和服务端一样,这里贴上代码(如果写的 serviceClass 是正确的按住ctrl鼠标悬浮时可以进入该类,需要几个就配几个,id是随便起名的):
<?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:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
		<jaxws:client id="userclient" 		
		serviceClass="com.tym.service.UsersService" 
		address="http://localhost:8082/service/user"/>
</beans>
最后需要在客户端的web.xml文件中配置,一样的需要tomcat启动时就扫描配置文件,并且扫描spring的配置文件和请求接受:
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext_*.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

最后就可以在客户端的controller中调用服务端的service了,记得带上跨域的注解,因为服务器调用时域名变了,属于跨域了。跨域在controller后加上

@Controller
@CrossOrigin(origins="*",maxAge=3600)
public class UserController {

@Autowired
private UsersService usersService;
}

这样子就是一个最简单的webservice了,可以用客户端调用服务端的service。

posted @ 2019-06-11 20:05  谈宜明  阅读(951)  评论(0编辑  收藏  举报