maven+ssm搭建webservice远程调用

闲话不多说,直接上码

maven依赖

<cxf.version>3.0.1</cxf.version>
<!-- cxf -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>${cxf.version}</version>
</dependency>

webservice
  server(服务端)

  接口

package com.ssm.cxf.server;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface CXFWeather {

    @WebMethod
    public String getWeather(String city);
}

  实现

package com.ssm.cxf.server.impl;

import org.springframework.stereotype.Service;

import com.ssm.cxf.server.CXFWeather;

public class CXFWeatherImpl implements CXFWeather {

    @Override
    public String getWeather(String city) {
        
        return "晴";
    }
}

 applicationContext.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:cxf="http://cxf.apache.org/core"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="cXFWeatherImpl" class="com.ssm.cxf.server.impl.CXFWeatherImpl"></bean>
    <jaxws:server address="/weather">
        <jaxws:serviceBean>
            <ref bean="cXFWeatherImpl"/>
        </jaxws:serviceBean>
    </jaxws:server>

</beans>

 web.xml

<?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">
    <display-name>ssm-cxf-server</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>cxfServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxfServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>

 

*测试

  启动服务端,在浏览器输入访问进行测试:http://localhost:9301/ws/weather?wsdl

  如果是以下内容,表示服务端可以正常访问


 client(客户端)
  生成代码

  1.打开dos窗口

  2.输入以下命令

    wsimport -keep -d E:\eclipse_mars2_workspace\foundation_workspace\ssm-parent\ssm-web\src\main\java http://localhost:9301/ws/weather?wsdl

    wsimport -s E:\eclipse_mars2_workspace\foundation_workspace\ssm-parent\ssm-web\src\main\java http://localhost:9301/ws/weather?wsdl

      wsimport -s 保存生成代码的路径 http://localhost:9301/ws/weather?wsdl
  applicationContext.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:cxf="http://cxf.apache.org/core"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <jaxws:client id="cXFWeather" address="http://localhost:9301/ws/weather?wsdl"
        serviceClass="com.ssm.cxf.server.impl.CXFWeather"></jaxws:client>

</beans>

 调用webservice

package com.ssh.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.cxf.server.impl.CXFWeather;

@Controller
@RequestMapping("/ws")
public class WebServiceController {

    @Autowired
    private CXFWeather cXFWeather;
    
    @RequestMapping("/weather")
    public String getWeather(Model model) {
        String weather = cXFWeather.getWeather("");
        System.out.println(weather);
        model.addAttribute("weather", weather);
        
        return "city";
    }
}

  web.xml

<?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">
    <display-name>ssm-web</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>

    <!-- 解决post中文乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc/springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

 

ps:使用批处理生成客户端代码

  *创建bat文件:新建文本文档 .txt 改成 xxx.bat,将以下内容复制到bat文件中,修改wsdl和dir,保存,双击运行,执行完毕后,刷新客户端项目,即看到生成的代码

:: 设置当前dos窗口显示字符编码(65001:UTF-8)
chcp 65001

:: 修改wsdl和dir
@echo off
:: webservice的wsdl
set wsdl=http://localhost:9301/ws/weather?wsdl
:: 保存生成代码的目录,建议直接复制工程properties的location,
:: 例如maven工程,客户端项目,右键src/main/java 选择properties 复制location 粘贴到dir
set dir=E:\eclipse_mars2_workspace\foundation_workspace\ssm-parent\ssm-web\src\main\java

:: 执行wsimport命令,生成webservice客户端
wsimport -s %dir% %wsdl%
:: 提示信息
echo 代码保存在%dir%
:: 删除编译的class文件 /f:强制,/s:递归,/q:静默 (建议手动删除class文件)
:: @del F:\src\*.class /f /s /q
echo 编译的文件保存在批处理文件目录下
::手动退出dos窗口
pause

 

posted @ 2019-03-12 21:32  指北的司南  阅读(497)  评论(0编辑  收藏  举报