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。

相关文章
相关标签/搜索