首页 > Web开发 > 详细

maven+ssm搭建webservice远程调用

时间:2019-03-12 21:41:47      阅读:717      评论:0      收藏:0      [点我收藏+]

闲话不多说,直接上码

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远程调用

原文:https://www.cnblogs.com/lin-nest/p/10519808.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!