spring boot集成web service框架教程

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>

我们来写wsdl描述文件(这里不一定要手写,通过工具类自动生成也可以的)

<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>

具体的应该怎么写,请看下官方文档这里

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

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

这里写图片描述

选生成代码的地方。OK

然后编写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;
    }
}

然后写spring boot启动文件

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 可以看到项目启动成功

请求

这是body里面的,用post

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:gs="http://www.yourcompany.com/webservice">
   <soapenv:Header/>
   <soapenv:Body>
      <gs:getCountryRequest>
         <gs:name>Spain</gs:name>
      </gs:getCountryRequest>
   </soapenv:Body>
</soapenv:Envelope>

这里写图片描述

提问时间
1. 我要改request里面某些字段名称怎么办?

可以在生成的实体文件里面,用注解XMLElement里面的一个属性name来标识

这里写图片描述

  1. response也是一样的做法

项目下载:GIT传送门

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
### 回答1: Spring Boot是一种基于Java的快速应用程序开发框架,旨在简化新Spring应用程序的创建和部署。它提供了自动配置、开箱即用的功能和简单的Maven配置,使您能够使用Spring框架来开发Web应用程序。 要使用Spring Boot开发Web应用程序,首先需要在pom.xml文件中添加spring-boot-starter-web依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 然后,您可以创建一个Spring控制器来处理Web请求。例如,以下是一个简单的控制器,它响应'/hello'路径的GET请求: ``` @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } } ``` 最后,您可以使用Spring Boot提供的内置Tomcat服务器在本地运行应用程序。只需在应用程序的main方法中添加@SpringBootApplication注解并调用SpringApplication.run方法即可启动应用程序: ``` @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这就是使用Spring Boot开发Web应用程序的基本流程。希望这些信息对您有帮助。 ### 回答2: Spring Boot 是一个开源的Java开发框架,它可以帮助开发者快速构建基于Spring框架的应用程序。用Spring Boot来开发Web应用非常方便。 首先,Spring Boot提供了自动配置功能,简化了应用程序的配置过程。通过使用注解和约定,Spring Boot能够自动配置应用程序所需的各种组件和依赖项,例如数据库连接、Web服务器等。这样开发人员不需要手动编写大量的配置代码,提高了开发效率。 其次,Spring Boot具有内嵌的Web服务器(如Tomcat、Jetty等)和Servlet容器。这意味着开发者无需部署额外的Web服务器,只需打包应用程序为可执行的JAR文件,即可直接运行。这样可以减少运维工作和服务器资源的消耗。 另外,Spring Boot还提供了丰富的开发工具和插件,如Spring Boot DevTools和Spring Boot Actuator等。这些工具可以帮助开发人员快速开发和调试应用程序,还可以监控和管理应用程序的各种指标和状态。 除此之外,Spring Boot还支持使用Spring MVC快速构建RESTful API,同时也能与其他框架如Thymeleaf、Freemarker、Velocity等进行无缝集成,方便实现前后端分离的开发模式。 总之,Spring Boot是一个强大而简化的开发框架,它使得Spring应用程序的开发更加快速、高效。无论是开发小型项目还是大型复杂项目,都可以选择使用Spring Boot来开发Web应用。 ### 回答3: Spring Boot是一个用于简化Spring应用程序开发的框架。它提供了一个快速开发的环境,并且具有自动化的配置,减少了开发人员的工作量。在使用Spring Boot开发Web应用程序时,可以遵循以下步骤: 1. 配置pom.xml:在项目的pom.xml文件中,添加Spring Boot相关的依赖项。通常包括Spring WebSpring Data JPA、Spring Security等。 2. 创建Controller:使用注解方式创建Controller类,处理来自客户端的请求。通过@RestController注解,可以将Controller类中的处理方法的返回值直接作为响应体返回给客户端。 3. 创建Service:在Service类中,编写业务逻辑代码。可以使用注解@Service将类标记为Service组件,并且使用@Autowired注解注入其他的依赖。 4. 创建Repository:在Repository类中,编写数据库操作的代码。可以使用注解@Repository将类标记为Repository组件,并且使用Spring Data JPA提供的接口来实现对数据库的操作。 5. 定义实体类:根据业务需求,定义与数据库表对应的实体类。实体类中使用注解@Entity标记类为实体类,并且使用注解@Id标记主键字段。 6. 配置数据库连接:在application.properties文件中,配置数据库连接信息,包括数据库URL、用户名、密码等。 7. 运行应用程序:通过main方法启动Spring Boot应用程序。Spring Boot会自动创建并配置好相应的运行环境,并且根据配置的端口号监听客户端的请求。 8. 测试应用程序:使用工具如Postman发送HTTP请求,测试Controller中定义的接口。可以通过URL访问接口,并且根据请求的参数和响应的结果,验证应用程序的正确性。 通过上述步骤,使用Spring Boot开发Web应用程序能够快速简便地搭建一个可靠的系统。同时,Spring Boot还提供了自动化的配置和部署功能,让开发人员能够专注于业务逻辑的开发,而不需要关心复杂的配置。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值