Eclipse—整合Springboot編寫webservice服務

Eclipse—整合Springboot編寫webservice服務

  1. 新建Maven項目,選擇maven-archetype-quickstart類型

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>

  <parent>

   <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.0.3.RELEASE</version>

  </parent>

 

  <groupId>com.lifeng.webservice</groupId>

  <artifactId>demo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>demo</name>

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

 

  <properties>

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

     <maven.compiler.source>1.8</maven.compiler.source>

     <maven.compiler.target>1.8</maven.compiler.target>

  </properties>

  <dependencies>

<dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web-services</artifactId>

</dependency>      

        <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-frontend-jaxws</artifactId>

            <version>3.2.6</version>

        </dependency>

       <dependency>

            <groupId>org.apache.cxf</groupId>

            <artifactId>cxf-rt-transports-http</artifactId>

            <version>3.2.6</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

  </dependencies>

  <build>

       <defaultGoal>compiler</defaultGoal>

       <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins> 

    </build>

</project>

 

  1. 編寫WebService服務接口,代碼如下:

package com.lifeng.webservice.demo.service;

 

import javax.jws.WebService;

 

@WebService(name="IService",

targetNamespace="http://lifeng.webServiceDemo.com") // 命名空間,賦值一個有意義的http地址即可不一定要寫成包名倒序,只寫成包名倒序是一個約定俗成的習慣而已

 

public interface IService {

public String sayHello(String name);

}

  1. 編寫接口實現類

package com.lifeng.webservice.demo.service.impl;

import com.lifeng.webservice.demo.service.IService;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

 

@WebService(name="IService", // 必須與接口中指定的name一致

targetNamespace="http://lifeng.webServiceDemo.com", // 與接口中的命名空間一致

endpointInterface="com.lifeng.webservice.demo.service.IService")  // 接口地址接口類全路徑名稱(包名+類名)

 

@Component

public class ServiceImpl implements IService {

@Override

public String sayHello(String name) {

return "Hello " + name;

}

}

  1. 編寫WebService發佈的配置類

默認服務在Host:port/services/路徑下。但是可以通過ServletRegistrationBean注入servlet進行修改,請看代碼。

package com.lifeng.webservice.demo.service.config;

 

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;

import org.apache.cxf.bus.spring.SpringBus;

import org.apache.cxf.jaxws.EndpointImpl;

import org.apache.cxf.transport.servlet.CXFServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.beans.factory.annotation.Autowired;

 

import com.lifeng.webservice.demo.service.IService;

 

@Configuration

public class WebServiceConfig {

 

@Autowired

private IService service;

 

@Bean("cxfServletRegistration")

public ServletRegistrationBean<CXFServlet> dispatcherServlet() {

// 註冊servlet 攔截/webServiceDemo開頭的請求

// 將默認的“/services/*修改爲“/webServiceDemo/*”。

        return new ServletRegistrationBean<CXFServlet>(

new CXFServlet(),"/webServiceDemo/*");

    }

 

    @Bean(name = Bus.DEFAULT_BUS_ID)

    public SpringBus springBus() {

        return new SpringBus();

    }

 

 

    @Bean

    public Endpoint endpoint() {

        EndpointImpl endpoint = new EndpointImpl(springBus(), service);

          endpoint.publish("/api");  // 這裏相當於把接口發佈在了路徑/services/api下,WSDL文檔路徑爲http://localhost:8080/webServiceDemo/api?wsdl

        return endpoint;

    }

}

  1. springboot啓動類

package com.lifeng.webservice.demo;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

 

/**

 * Hello Spring boot!

 */

@SpringBootApplication

public class WebServiceApp {

    public static void main( String[] args ) {

     SpringApplication.run(WebServiceApp.class, args);

    }

}

 

項目目錄結構如下圖所示:

啓動項目,訪問 http://localhost:8080/webServiceDemo/

點擊WSDL鏈接:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章