cxf整合Spring实现webservice调用

cxf整合Spring实现webservice

server端

pom文件内容

<dependencies>
    
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-frontend-jaxwsartifactId>
      <version>3.0.1version>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.12version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>4.2.4.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>4.2.4.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-testartifactId>
      <version>4.2.4.RELEASEversion>
    dependency>
  dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.2version>
        <configuration>
          <source>1.8source>
          <target>1.8target>
          <encoding>UTF-8encoding>
          <showWarnings>trueshowWarnings>
        configuration>
      plugin>
      
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.2version>
        <configuration>
          
          <port>8080port>
          
          <path>/path>
        configuration>
      plugin>
    plugins>
  build>

web.xml文件里的内容



<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>

  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  
  
  
  <servlet>
    <servlet-name>cxfServletservlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>cxfServletservlet-name>
    <url-pattern>/ws/*url-pattern>
  servlet-mapping>
web-app>

applicationContext.xml文件的内容


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">

    
    <jaxws:server address="/userService">
        <jaxws:serviceBean>
            <bean class="com.sks.service.impl.UserServiceImpl"/>
        jaxws:serviceBean>
    jaxws:server>
    
beans>

接口

@WebService
public interface UserService {

    public String sayHello(String name, int age);
}

接口实现类

public class UserServiceImpl implements UserService {

    @Override
    public String sayHello(String name, int age) {
        return "hello " + name + " " + age;
    }
}

最后点击如图所示的按钮运行项目。
cxf整合Spring实现webservice调用_第1张图片
在浏览器中输入http://localhost:8080/ws/userService?wsdl,访问wsdl说明书,可以看到以下内容:
cxf整合Spring实现webservice调用_第2张图片

client端

项目目录结构,web.xml文件中的内容不需要改动
cxf整合Spring实现webservice调用_第3张图片

applicationContext.xml文件内容


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">


    
    <jaxws:client id="userService" address="http://localhost:8080/ws/userService" serviceClass="com.sks.service.UserService">

    jaxws:client>

beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test1 {

    @Resource
    private UserService userService;

    @Test
    public void test1() {
        System.out.println(userService);
        System.out.println(userService.getClass());
        //远程服务调用接口
        String context = userService.sayHello("球球",19);
        System.out.println(context);
    }
}

点击运行以后控制台打印输出结果如下,这说明我们的调用成功了。
cxf整合Spring实现webservice调用_第4张图片

你可能感兴趣的:(JavaEE,java,spring,webservice)