maven+ssm搭建webservice遠程調用

閒話很少說,直接上碼html

maven依賴java

<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(服務端)web

  接口spring

package com.ssm.cxf.server;

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

@WebService
public interface CXFWeather {

    @WebMethod
    public String getWeather(String city);
}

  實現apache

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.xmlmvc

<?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>

 

*測試app

  啓動服務端,在瀏覽器輸入訪問進行測試:http://localhost:9301/ws/weather?wsdlfrontend

  若是是如下內容,表示服務端能夠正常訪問eclipse


 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
相關文章
相關標籤/搜索