spring 3.0系统集成webservice,踩了很多坑以后总算成功了,故写下这篇博客以记录。

 

1.准备jar包

由于项目是spring3.0,所以应该要使用cxf 2.7版本才可以成功配置,高版本会出现jar包冲突。

具体需要的jar包如下:

spring 3.0系统集成webservice

你可以点此下载:cxf 2.7所需jar包

然后在idea中引入这些jar包(对于该项目来说,可以先把这些jar包复制到项目内的lib文件夹内)

idea引入jar包方法见下图(点击图中红框圈出的+号即可添加):

spring 3.0系统集成webservice

 

2.配置webService

2.1 web.xml

在web.xml中添加如下代码,用于路由/webservice开始的路径。

记得要加在原来的url配置器上方才能被优先匹配。

    <!-- webservice -->
    <servlet>
        <servlet-name>webservice</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>webservice</servlet-name>
        <url-pattern>/webservice/*</url-pattern>
    </servlet-mapping>

2.2 applicationContext.xml

首先将头修改如下,自己看实际代码缺啥补啥:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       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://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

</beans>

然后添加cxf的配置,注意必须引入下面三个资源(据说新版本的cxf可以省去第二个配置文件)

    <!--cxf配置-->
    <!-- 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服务就集成好了。

spring 3.0系统集成webservice

不过别急,这时候由于还没创建webservice服务,所以如果你这时候访问:

http://localhost:8080/supply_refactor/webservice,是会报错的。它会提示你初始化失败没有webservice服务。

 

3.数据库中创建测试数据表test

CREATE TABLE `supply`.`test` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `context` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`id`))
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8;

然后往里面插入一条数据,我这里插入了“测试哈哈哈”

 

4.创建webservice服务

4.1配置hibernate bean映射文件

Test.java

package com.supply.bean;

public class Test implements java.io.Serializable{
    private long id;
    private String context;

    public Test(){}

    public Test(long id,String context){
        super();
        this.id=id;
        this.context=context;
    }

    @Override
    public String toString() {
        return "Test{" +
                "id=" + id +
                ", context='" + context + '\'' +
                '}';
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }
}
View Code

相关文章:

  • 2021-11-03
  • 2022-12-23
  • 2021-07-16
  • 2021-04-01
  • 2022-01-20
  • 2022-12-23
  • 2021-07-20
  • 2021-12-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-27
  • 2021-06-16
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
相关资源
相似解决方案