CXF+Spring+Maven之服务端

最近学了一下cxf,按照官网整下来有问题,后来自己结合各种例子自己也做了一个服务器端,直接上demo结构。
这里写图片描述

1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kongl</groupId>
    <artifactId>ws_spring_cxf</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws_spring_cxf Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <!-- CXF版本 -->
        <cxf.version>3.1.8</cxf.version>
        <spring.version>4.1.5.RELEASE</spring.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-webapp -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>9.2.13.v20150730</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>ws_spring_cxf</finalName>
    </build>
</project>

2、SEI以及实现

WeatherInterface .java

@WebService(
        targetNamespace="http://weather.kongl.com/",//指定 wsdl的命名空间
        name="WeatherInterface",//指定portType的名称
        portName="WeatherInterfacePort",//指定port的名称
        serviceName="WeatherService"//服务视图的名称       
        )
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {

    //查询三天天气
    public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName);

}

WeatherInterfaceImpl

public class WeatherInterfaceImpl implements WeatherInterface {

    public  List<WeatherModel> queryWeather(String cityName) {
        List<WeatherModel> list = new ArrayList<WeatherModel>();

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DATE);

        WeatherModel weatherModel_1  =new WeatherModel();
        weatherModel_1.setDetail("晴");
        weatherModel_1.setData(new Date());
        weatherModel_1.setTemperature_max(5);
        weatherModel_1.setTemperature_min(-6);

        WeatherModel weatherModel_2  =new WeatherModel();
        weatherModel_2.setDetail("阴");
        calendar.set(Calendar.DATE, day+1);
        weatherModel_2.setData(calendar.getTime());
        weatherModel_2.setTemperature_max(10);
        weatherModel_2.setTemperature_min(-3);

        list.add(weatherModel_1);
        list.add(weatherModel_2);       
        return list;
    }
}

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

<!-- service -->
<bean id="weatherInterface" class="com.kongl.cxf.service.WeatherInterfaceImpl"/>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 发布服务
使用jaxws:server和jaxws:endpoint可以发布服务
webservice地址=tomcat地址+项目名+cxf servlet的路径+/weather
 -->
<jaxws:server address="/weather" serviceClass="com.kongl.cxf.service.WeatherInterface">
    <jaxws:serviceBean>
        <ref bean="weatherInterface"/>
    </jaxws:serviceBean>
</jaxws:server>
</beans>

4、web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 加载 spring容器 -->
     <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
      <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- cxf的servlet -->
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 本系统webservice的路径必须以/ws/开头 -->
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

    <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>
</web-app>

最后访问http://localhost:8080/ws_spring_cxf/ws/weather?wsdl此地址,出现下面的截图说明服务发布成功。

运行结果

未完待续。。。。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值