1. 程式人生 > >maven+ssm搭建webservice遠程調用

maven+ssm搭建webservice遠程調用

sun 直接 port spl .html lips wired apache har

閑話不多說,直接上碼

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>

maven+ssm搭建webservice遠程調用