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.0modelVersion>

  <groupId>com.lyjgroupId>
  <artifactId>WebServiceCXFTest1artifactId>
  <version>1.0-SNAPSHOTversion>
  <modules>
    <module>WebServiceCXFClient1module>
  modules>
  <packaging>pompackaging>

  <name>WebServiceCXFTest1name>
  <url>http://maven.apache.orgurl>

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

  <dependencies>
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-frontend-jaxwsartifactId>
      <version>${cxf.version}version>
    dependency>
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-transports-httpartifactId>
      <version>${cxf.version}version>
    dependency>
    <dependency>
      <groupId>org.apache.cxfgroupId>
      <artifactId>cxf-rt-transports-http-jettyartifactId>
      <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

IDEA+maven+CXF+WebService_第1张图片

这样我们就构建好了一个基于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>WebServiceCXFTest1artifactId>
        <groupId>com.lyjgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>WebServiceCXFClient1artifactId>
    <packaging>jarpackaging>

    <name>WebServiceCXFClient1name>
    <url>http://maven.apache.orgurl>

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

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.cxfgroupId>
                <artifactId>cxf-codegen-pluginartifactId>
                <version>3.1.7version>
                <executions>
                    <execution>
                        <id>generate-sourcesid>
                        <phase>generate-sourcesphase>
                        <configuration>
                            
                            <sourceRoot>${project.build.sourceDirectory}sourceRoot>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>http://localhost:9090/cxf?wsdlwsdl>
                                wsdlOption>
                            wsdlOptions>
                        configuration>
                        <goals>
                            <goal>wsdl2javagoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

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

IDEA+maven+CXF+WebService_第2张图片
IDEA+maven+CXF+WebService_第3张图片

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.运行后服务端结果

IDEA+maven+CXF+WebService_第4张图片

2.运行后客户端结果

IDEA+maven+CXF+WebService_第5张图片

3.项目结构图1

IDEA+maven+CXF+WebService_第6张图片

4.项目结构图2

IDEA+maven+CXF+WebService_第7张图片

你可能感兴趣的:(java)