spring mvc - cxf webservice项目

来源:互联网 发布:新华软件学校 编辑:程序博客网 时间:2024/06/05 09:39

提到cxf ,毫不避免的要说到webservice

什么是webservice,我就不多说了。

新建一个maven 项目  通过pom引用cxf的jar

<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>3.0.4</version>
</dependency>
<dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-transports-http</artifactId>
   <version>3.0.4</version>
</dependency>

我用的版本是3.0.4

引入springmvc 的jar

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.7.RELEASE</version>
</dependency>

新建一个包com.andy.cxf

新建一个interface   ITestService 如下


package com.text.service;


import javax.jws.WebService;


@WebService
public interface ITestService {
public String test();
}

新建实现类

包名com.text.service.impl

类名TestServiceImpl

内容:

package com.text.service.impl;


import javax.jws.WebService;


import com.text.service.ITestService;


@WebService
public class TestServiceImpl implements ITestService{

public String test(){
return "success";
}
}


在classpath下面新建一个beanReServer.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"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://cxf.apache.org/jaxws
                     http://cxf.apache.org/schemas/jaxws.xsd">


<jaxws:endpoint id="TestFacade" implementor="com.text.service.impl.TestServiceImpl" address="/TestFacade" />
   
</beans>


在web.xml里面加入

<context-param>
         <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beanReServer.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
<servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


至此,成功搭建完毕,启动项目

http://localhost:8080/test-cxf


成功访问

由于cxf版本的版本差异,可能需要引入一些 xml如下

<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"/>

我用的3.0.4版本不需要引入


源码我已经上次,地址:http://download.csdn.net/detail/q343814703/9154103


0 0