spring集成cxf初探(经过测试)

本次测试使用spring框架,用idea开发工具,搭建的环境。

首先,使用idea新建一个spring结构的项目

next  下一步 


下一步,自定义自己项目名称。


其次,配置web.xml,添加maven依赖的cxf jar文件,我引入的是cxf 3.1.10的文件,下面是pom.xml 与web.xml 配置

web.xml

<web-app version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>Spring MVC Application</display-name>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring-cxf.xml</param-value>
	</context-param>

    <servlet>
		<servlet-name>mvc-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>mvc-dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	<!--spring 集成cxf web配置-->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<!-- CXFServlet Mapping -->
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/cxf/*</url-pattern>
	</servlet-mapping>

</web-app>

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.springapp</groupId>
    <artifactId>WebCxf</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>WebCxf</name>

    <properties>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

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

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--CXF 依赖-->
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/apache-cxf -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>apache-cxf</artifactId>
            <version>3.1.10</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>WebCxf</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*Tests.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
这里简单配置了一下spring的配置文件mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--注解驱动-->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.spring"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

重点配置文件,spring-cxf.xml 这是cxf的专属配置文件,在web.xml 引入。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- Import Apache CXF Bean Definition -->
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <!--<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>-->
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <!--模拟服务端 webservice-->
    <bean id="hello" class="com.spring.service.impl.HelloWordServiceImpl"/>
    <!--implementor="#hello" 指向bean id="hello"-->
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/helloWorld" />
    <!--模拟客户端-->
    <jaxws:client id="client"
                  serviceClass="com.spring.service.IHelloWord"
                  address="http://localhost:8080/cxf/helloWorld" />
</beans>

简单的java bean实现

定义一个接口:

package com.spring.service;

import javax.jws.WebService;

@WebService
public interface IHelloWord {
    String getWord();
}
实现接口:

package com.spring.service.impl;

import com.spring.service.IHelloWord;

import javax.jws.WebService;

/**
 * 
 */
@WebService
public class HelloWordServiceImpl implements IHelloWord {
    @Override
    public String getWord() {
        return "HELLO WORLD";
    }
}
这里注意一下,注解的适用,虽然会将bean放在service包下,但是注解不是@service,而是@WebService,想要了解更多这个注解请访问 点击打开链接

到这里就可以在浏览器访问了,访问路径:http://localhost:8080/cxf/helloWorld?wsdl

如果在浏览看到如下图的格式,说明你服务器端已经搭建完成。



最后,模拟客户端的实现,正常建一个controller 的bean。

package com.spring.controller;

import com.spring.service.IHelloWord;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 */
@Controller
@RequestMapping(value = "/pc")
public class TestCxfClientController {

    @RequestMapping(value = "webClient")
    public void getCxf(){
        //加载xml配置文件
        ApplicationContext cxf = new ClassPathXmlApplicationContext("spring-cxf.xml");
        IHelloWord bean = (IHelloWord) cxf.getBean("client");
        String result=bean.getWord();
        System.out.println("输出结果:"+result);
    }

}
然后在浏览器访问你的路径,在这里我的路径是:http://localhost:8080/pc/webClient.do  

在控制台看到输出结果,说明你已经成功。

注意:cxf jar文件从版本3.0 以后,就剩这俩xml,这是cxf  jar包自带的,不需要你去新建,你只要引入就行。

还有可能更高版本的 cxf jar文件只剩标注红框 的一个xml文件,根据实际应用,自己删除报错的xml就ok!!!!!






















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值