IDEA+maven+CXF+WebService

WebService需要服务端和客户端构建多模块项目,为了方便此处以服务端为主项目


服务端:

1. 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.lyj</groupId>
  <artifactId>WebServiceCXFTest1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <modules>
    <module>WebServiceCXFClient1</module>
  </modules>
  <packaging>pom</packaging>

  <name>WebServiceCXFTest1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <cxf.version>3.1.7</cxf.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
    </dependency>
  </dependencies>


</project>

2.编写接口、实例和发布接口

(1)
package com.lyj.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * Created by LYJ on 2016/10/5.
 */
@WebService
public interface HelloWS {
    @WebMethod
    public String sayHello(String name);
}
(2)
package com.lyj.ws;

import javax.jws.WebService;

/**
 * Created by LYJ on 2016/10/5.
 */
@WebService
public class HelloWSImpl implements HelloWS{
    public String sayHello(String name) {
        System.out.println(  name+"      服务端---------     --- HelloWSImpl.sayHello\n");
        return "hello "+name;
    }
}
(3)
package com.lyj.test;

import com.lyj.ws.HelloWSImpl;

import javax.xml.ws.Endpoint;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ){
        String address="http://localhost:9090/cxf";
        Endpoint.publish(address,new HelloWSImpl());
        System.out.println("发布webservice成功");


    }

}
打开浏览器输入http://localhost:9090/cxf?wsdl得到如下图所示就说明已成功开启WebService

image

这样我们就构建好了一个基于CXF的WebService,运行main方法(一直运行),以便我们编写客户端


客户端

1.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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>WebServiceCXFTest1</artifactId>
        <groupId>com.lyj</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>WebServiceCXFClient1</artifactId>
    <packaging>jar</packaging>

    <name>WebServiceCXFClient1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>3.1.7</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <!--源码生成地址-->
                            <sourceRoot>${project.build.sourceDirectory}</sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>http://localhost:9090/cxf?wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.运行generate-sources即可生成wsdl对应的java文件

image
image

3.编写测试文件

package com.lyj.test;

import com.lyj.ws.HelloWS;
import com.lyj.ws.HelloWSImplService;

public class App 
{
    public static void main( String[] args ){

        HelloWSImplService factory=new HelloWSImplService();

        HelloWS helloWS=factory.getHelloWSImplPort();
        String result =helloWS.sayHello("tom");
        System.out.println(result + "      client--------- result    --- App.main\n");
    }
}

附录:

1.运行后服务端结果

image

2.运行后客户端结果

image

3.项目结构图1

image

4.项目结构图2

image

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值