WebService--CXF与Spring的整合jaxws:endpoint形式配置以及客户端调用spring配置文件形式,不需要生成客户端代码

目录

一、CXF与Spring整合(jaxws:endpoint形式配置)
1.新建一个maven项目
2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件
二、客户端调用(spring配置文件形式,不需要生成客户端代码)
1.spring配置文件信息
2.写对应接口信息
3.加载Spring的配置文件进行测试
4.需要的jar包

一、CXF与Spring整合(jaxws:endpoint形式配置)

工具要点:idea、maven

1.新建一个maven项目

分享图片

分享图片

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.wp.learn.webservice.webserver</groupId>
  <artifactId>webserver</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <cxf.version>2.2.3</cxf.version>
    <spring.version>3.0.2.RELEASE</spring.version>
  </properties>

  <dependencies>

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

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-testutils</artifactId>
      <version>${cxf.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-local</artifactId>
      <version>${cxf.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-jdk14</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>${cxf.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

</project>

复制代码

2.web.xml文件中配置CXFServlet以及Spring容器,配置Spring的配置文件,配置webservice的配置文件

web.xml:

分享图片

  web.xml

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
    <context:component-scan base-package="com.wp.learn.webservice"></context:component-scan>
    <!-- 导入其他配置文件 -->
    <import resource="applicationContext-ws.xml" />

</beans>

复制代码

applicationContext-ws.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-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

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

    <jaxws:endpoint id="helloWebservice" implementor="#helloServiceImpl"
                    address="/hello" />
</beans>

复制代码

接口HelloService:

分享图片

Impl类HelloServiceImpl:

情况一、如果Impl类的注解是@Webservice,implementor需要写完全路径

分享图片

情况二、Impl类注解是@Service,implementor只需“#”号加bean的id名

分享图片

二、客户端调用(spring配置文件形式,不需要生成客户端代码)

  Webservice的客户端调用,可以用ajax前端调用,java中可以通过生产客户端代码调用,也可以通过Spring配置文件、写对应接口信息调用。本例介绍配置文件,写接口信息进行webservice调用。

1.spring配置文件信息

<jaxws:client id="helloService" serviceClass="com.wp.learn.webservice.cxf.service.IHelloService"
   address="http://localhost:8080/ws/HelloService"> </jaxws:client>

2.写对应接口信息

 需要注意两点:

  • 1、targetNamespace的值必须和webservice服务项目中定义的一致,具体信息可以在WSDL文件中查看
  • 2、接口名称可以自己随便起,但是方法名称、参数格式必须保持一致,否则无法找到服务的实现方法。

复制代码

//注意,该出的targetNamespace的值必须和webService服务项目中定义的必须一致,否则调用不成功
@WebService(targetNamespace = "http://impl.service.cxf.webservice.learn.wp.com/",name = "IHelloService")
public interface IHelloService {
    //接口名称可以不一样,方法名称、参数格式必须保持一致,否则无法找到服务的实现的方法
    public String sayHi(String name);
}

复制代码

3.加载Spring的配置文件进行测试

复制代码

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-ws.xml");
        IHelloService helloService = (IHelloService) context.getBean("helloService");
        String result = helloService.sayHi("帅哥");
        System.out.print(result);
    }
}

复制代码

4.需要的jar包

分享图片

  pom.xml
 
 源码地址:https://gitee.com/wangpinggs/learn/tree/master/webserviceDemo

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...