SpringBoot 框架整合webservice

简介: spring boot集成web service框架题记: 本篇博客讲的spring boot如何集成 spring web service,如果您想用Apache CXF集成,那么可能不适合您。为什么使用spring web servce 项目地址 呢?因为spring boot存在的目的就是一个微服务框架,结果又搞个soap框架进去,显得特别不伦不类。

spring boot集成web service框架

题记: 本篇博客讲的spring boot如何集成 spring web service,如果您想用Apache CXF集成,那么可能不适合您。
为什么使用spring web servce 项目地址 呢?因为spring boot存在的目的就是一个微服务框架,结果又搞个soap框架进去,显得特别不伦不类。
正是因为有这么多老项目的重构才会有这么不伦不类的集成。综上,我就选了spring家族的spring web service能够很好的跟spring boot进行集成。

 

那么如何集成呢?我这里讲一个demo,照葫芦画瓢就行

先建一个maven 项目 
然后加入spring boot的依赖(截止目前最新是1.5.2版本)

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

 

加入spring-web-service的依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

 

加入wsdl的依赖

<dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.3</version>
    </dependency>

 

完整的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.itar</groupId>
  <artifactId>springWSTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springWSTest</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>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- spring boot的包 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

    <!--spring web Service的包-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

    <!--spring web service wsdl包-->
    <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.3</version>
    </dependency>

  </dependencies>
</project>

 

 

关于xsd文件

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.yourcompany.com/webservice"
           targetNamespace="http://www.yourcompany.com/webservice" elementFormDefault="qualified">

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:int"/>
            <xs:element name="capital" type="xs:string"/>
            <xs:element name="currency" type="tns:currency"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="GBP"/>
            <xs:enumeration value="EUR"/>
            <xs:enumeration value="PLN"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

 

然后用eclipse工具生成实体类,这里比较关键

coruntries.xds右键,然后选中web service那一项,generate Java code from xml schema using jaxb

这里写图片描述

设置生成代码的路径即可。

 

 

定义webserviceconfig文件,指定url什么的。

package com.itar.ws.config;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "countries")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountriesPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.yourcompany.com/webservice");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
    }
}

 

然后编写endpoint,类似于controller,然后我就丢在controller里面了

package com.itar.ws.controller;

import com.itar.ws.model.GetCountryRequest;
import com.itar.ws.model.GetCountryResponse;
import com.itar.ws.service.CountryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://www.yourcompany.com/webservice";

    private CountryRepository countryRepository;

    @Autowired
    public CountryEndpoint(CountryRepository countryRepository) {
        this.countryRepository = countryRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        response.setCountry(countryRepository.findCountry(request.getName()));

        return response;
    }
}

 

编写springBoot项目的启动类

package com.itar.ws;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

启动即可

访问这个http://localhost:8080/ws/countries.wsdl 可以看到项目启动成功,如下:

 

 

 项目结构如下:

 

相关文章
|
1月前
|
Java 数据处理
【十二】springboot整合WebService
【十二】springboot整合WebService
43 0
|
1月前
|
Java
【十三】springboot整合WebService关于传参数
【十三】springboot整合WebService关于传参数
17 0
|
4月前
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
123 0
|
9月前
|
Java 容器
springboot中简单创建webservice服务
不添加配置类也可以在springboot的启动类了里面发布服务,但是那样的话就不是ioc托管的了,在服务里面就不能注入其他IOC容器里面的对象
|
11月前
|
前端开发 Java
springboot发布webservice
springboot发布webservice
|
Java Maven
Springboot整合CXF发布WebService
Springboot整合CXF发布WebService
Springboot整合CXF发布WebService
springboot配置webservice服务发布
springboot配置webservice服务发布
332 0
|
Java
SpringBoot + cxf 调用 webService
SpringBoot + cxf 调用 webService
641 0
|
Apache 网络架构
SpringBoot整合cxf发布webService
1. 看看项目结构图 2. cxf的pom依赖 1 2 org.apache.cxf3 cxf-spring-boot-starter-jaxws4 3.2.45 3.
2055 0