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>compile</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链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值