大多数人忽略了的Spring官方项目,Spring Web Services

简介: 大多数人忽略了的Spring官方项目,Spring Web Services

1、什么是Spring Web Service?

首先,不知道Web service的小伙伴,还需要,去了解下Web service 的相关知识再来看这篇文章。本文基于官方文档,作为基础。

Spring Web Services(Spring-WS)是Spring社区的产品,致力于创建文档驱动的Web服务。Spring Web Services旨在促进约定优先SOAP服务的开发,从而允许使用多种处理XML有效负载的方式之一来创建灵活的Web服务。该产品基于Spring本身,这意味着您可以将诸如依赖项注入之类的Spring概念用作Web服务的组成部分。


人们使用Spring-WS的原因有很多,但是大多数人在找到了遵循Web服务最佳实践所缺乏的替代SOAP堆栈之后才开始使用它。Spring-WS使最佳实践变得容易。这包括诸如WS-I基本概要文件,合同优先开发之类的实践,以及合同与实施之间的松散耦合。

如果你们项目里,还在使用Web Service 作为服务发布,那么它将是最佳实践。

2、为什么Spring-WS要用约定优先开发

众所周知,创建Web服务时,有两种开发样式:(约定滞后)Contract Last和(约定优先)Contract First。当使用Contract Last方法时,将从Java代码开始,然后从中生成Web服务契约(WSDL)。当使用契约优先时,首先要使用WSDL契约,然后使用Java来实现所述契约。

什么意思呢,也就是WSDL的诞生的问题,如何产生。

Spring-WS仅支持契约优先的开发风格。

原因总结来看有以下几点:

  • 可以支持更复杂的对象定义,java有局限
  • 程序更加健壮
  • 性能更优越,Java转Xml相较而言,差一点
  • 重用性好
  • 扩展性强,采用配置文件,改动小

3、采用Springboot + Spring-WS实现一个简单应用

应用场景描述:

公司人事部门,完成员工对于假期的请求。员工发起,假期请求。填写起始日期时间,姓名等信息,公司完成确认。

  1. 搭建一个Springboot应用
    pom文件
<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <jaxen.version>1.1.4</jaxen.version>
        <jdom.version>2.0.1</jdom.version>
        <joda-time.version>2.10.6</joda-time.version>
        <log4j.version>1.2.16</log4j.version>
        <sourcesDir>${project.basedir}/target/generated-sources/axis</sourcesDir>
        <classesDir>${project.basedir}/target/classes</classesDir>
        <wsdl>${project.basedir}/../airline.wsdl</wsdl>
        <wsdl4j.version>1.6.1</wsdl4j.version>
        <xmlschema.version>2.1.0</xmlschema.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>${jdom.version}</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>${jaxen.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ws.xmlschema</groupId>
            <artifactId>xmlschema-core</artifactId>
            <version>${xmlschema.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>${wsdl4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>${project.basedir}/src/main/resources/hr.xsd</sources>
                    <packageName>com.example.demo.schema</packageName>
                    <target>2.1</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/xjc</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.采用约定优先开发风格,完成对象的定义

xsd文件

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:hr="http://mycompany.com/hr/schemas"
           elementFormDefault="qualified"
           targetNamespace="http://mycompany.com/hr/schemas">
    <xs:element name="HolidayRequest">
        <xs:complexType>
            <xs:all>
                <xs:element name="Holiday" type="hr:HolidayType"/>
                <xs:element name="Employee" type="hr:EmployeeType"/>
            </xs:all>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="HolidayType">
        <xs:sequence>
            <xs:element name="StartDate" type="xs:date"/>
            <xs:element name="EndDate" type="xs:date"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="EmployeeType">
        <xs:sequence>
            <xs:element name="Number" type="xs:integer"/>
            <xs:element name="FirstName" type="xs:string"/>
            <xs:element name="LastName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

3.应用配置文件


application.properties

server.port=8848
logging.level.web=DEBUG
spring.webservices.path=/webservices
spring.webservices.servlet.init.transformWsdlLocations=true

4.Endpoint

开发WebService实现Endpoint,HolidayEndpoint

package com.example.demo.endpoint;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.demo.service.HumanResourceService;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
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;
/**
 * @author 小隐乐乐
 * @since 2020/9/18 22:44
 */
@Endpoint
public class HolidayEndpoint {
    private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas";
    private XPathExpression<Element> startDateExpression;
    private XPathExpression<Element> endDateExpression;
    private XPathExpression<Element> firstNameExpression;
    private XPathExpression<Element> lastNameExpression;
    private HumanResourceService humanResourceService;
    @Autowired
    public HolidayEndpoint(HumanResourceService humanResourceService) {
        this.humanResourceService = humanResourceService;
        Namespace namespace = Namespace.getNamespace("hr", NAMESPACE_URI);
        XPathFactory xPathFactory = XPathFactory.instance();
        startDateExpression = xPathFactory.compile("//hr:StartDate", Filters.element(), null, namespace);
        endDateExpression = xPathFactory.compile("//hr:EndDate", Filters.element(), null, namespace);
        firstNameExpression = xPathFactory.compile("//hr:FirstName", Filters.element(), null, namespace);
        lastNameExpression = xPathFactory.compile("//hr:LastName", Filters.element(), null, namespace);
    }
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "HolidayRequest")
    public void handleHolidayRequest(@RequestPayload Element holidayRequest) throws Exception {
        Date startDate = parseDate(startDateExpression, holidayRequest);
        Date endDate = parseDate(endDateExpression, holidayRequest);
        String name = firstNameExpression.evaluateFirst(holidayRequest).getText() + " "
                + lastNameExpression.evaluateFirst(holidayRequest).getText();
        humanResourceService.bookHoliday(startDate, endDate, name);
    }
    private Date parseDate(XPathExpression<Element> expression, Element element) throws ParseException {
        Element result = expression.evaluateFirst(element);
        if (result != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            return dateFormat.parse(result.getText());
        } else {
            throw new IllegalArgumentException("Could not evaluate [" + expression + "] on [" + element + "]");
        }
    }
}

5.实现服务

服务接口

package com.example.demo.service;
import java.util.Date;
/**
 * @author 小隐乐乐
 * @since 2020/9/18 23:00
 */
public interface HumanResourceService {
    /**
     * 请假.
     *
     * @param startDate 假期开始时间
     * @param endDate   假期结束时间
     * @param name      请假人
     */
    void bookHoliday(Date startDate, Date endDate, String name);
}

服务接口实现

package com.example.demo.service.impl;
import com.example.demo.service.HumanResourceService;
import org.springframework.stereotype.Service;
import java.util.Date;
/**
 * @author 小隐乐乐
 * @since 2020/9/18 23:02
 */
@Service
public class HumanResourceServiceImpl implements HumanResourceService {
    @Override
    public void bookHoliday(Date startDate, Date endDate, String name) {
        System.out.println("Booking holiday for [" + startDate + "-" + endDate + "] for [" + name + "] ");
    }
}

6.采用配置生成,生成WSDL,发布WSDL服务

package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection;
/**
 * @author 小隐乐乐
 * @since 2020/9/18 23:34
 */
@Configuration
public class EndpointConfig {
    @Bean
    public DefaultWsdl11Definition holiday() {
        DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
        definition.setPortTypeName("HumanResource");
        definition.setLocationUri("/webservices/holidayService/");
        definition.setTargetNamespace("http://mycompany.com/hr/definitions");
        definition.setSchemaCollection(holidayXsd());
        return definition;
    }
    @Bean
    public CommonsXsdSchemaCollection holidayXsd() {
        CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection(new ClassPathResource("/hr.xsd"));
        collection.setInline(true);
        return collection;
    }
}

7.maven插件

用于生成Schema实体

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.5.0</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sources>${project.basedir}/src/main/resources/hr.xsd</sources>
                    <packageName>com.example.demo.schema</packageName>
                    <target>2.1</target>
                </configuration>
            </plugin>

4、应用测试

采用SoapUi完成接口测试。

服务发布地址:http://localhost:8848/webservices/holidayService/holiday.wsdl

服务请求地址:http://localhost:8848/webservices/holidayService

请求报文

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://mycompany.com/hr/schemas">
   <soapenv:Header/>
   <soapenv:Body>
      <sch:HolidayRequest>
         <!--You may enter the following 2 items in any order-->
         <sch:Holiday>
            <sch:StartDate>2008-09-29</sch:StartDate>
            <sch:EndDate>2014-09-19</sch:EndDate>
         </sch:Holiday>
         <sch:Employee>
            <sch:Number>100</sch:Number>
            <sch:FirstName>verrantque per auras</sch:FirstName>
            <sch:LastName>per auras</sch:LastName>
         </sch:Employee>
      </sch:HolidayRequest>
   </soapenv:Body>
</soapenv:Envelope>

测试完毕,一个简单的应用接口,完成开发。

目录
相关文章
|
4月前
|
安全 Java API
Java Web 在线商城项目最新技术实操指南帮助开发者高效完成商城项目开发
本项目基于Spring Boot 3.2与Vue 3构建现代化在线商城,涵盖技术选型、核心功能实现、安全控制与容器化部署,助开发者掌握最新Java Web全栈开发实践。
456 1
|
3月前
|
缓存 安全 Java
《深入理解Spring》过滤器(Filter)——Web请求的第一道防线
Servlet过滤器是Java Web核心组件,可在请求进入容器时进行预处理与响应后处理,适用于日志、认证、安全、跨域等全局性功能,具有比Spring拦截器更早的执行时机和更广的覆盖范围。
|
5月前
|
JavaScript Java 微服务
现代化 Java Web 在线商城项目技术方案与实战开发流程及核心功能实现详解
本项目基于Spring Boot 3与Vue 3构建现代化在线商城系统,采用微服务架构,整合Spring Cloud、Redis、MySQL等技术,涵盖用户认证、商品管理、购物车功能,并支持Docker容器化部署与Kubernetes编排。提供完整CI/CD流程,助力高效开发与扩展。
605 64
|
4月前
|
存储 安全 Java
如何在 Spring Web 应用程序中使用 @SessionScope 和 @RequestScope
Spring框架中的`@SessionScope`和`@RequestScope`注解用于管理Web应用中的状态。`@SessionScope`绑定HTTP会话生命周期,适用于用户特定数据,如购物车;`@RequestScope`限定于单个请求,适合无状态、线程安全的操作,如日志记录。合理选择作用域能提升应用性能与可维护性。
195 1
|
6月前
|
Java 关系型数据库 数据库连接
Spring Boot项目集成MyBatis Plus操作PostgreSQL全解析
集成 Spring Boot、PostgreSQL 和 MyBatis Plus 的步骤与 MyBatis 类似,只不过在 MyBatis Plus 中提供了更多的便利功能,如自动生成 SQL、分页查询、Wrapper 查询等。
563 3
|
5月前
|
存储 NoSQL Java
探索Spring Boot的函数式Web应用开发
通过这种方式,开发者能以声明式和函数式的编程习惯,构建高效、易测试、并发友好的Web应用,同时也能以较小的学习曲线迅速上手,因为这些概念与Spring Framework其他部分保持一致性。在设计和编码过程中,保持代码的简洁性和高内聚性,有助于维持项目的可管理性,也便于其他开发者阅读和理解。
167 0
|
6月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
275 0
|
6月前
|
前端开发 Java API
酒店管理系统基于 JavaFX Spring Boot 和 React 经典项目重构实操
本文介绍了基于现代技术栈的酒店管理系统开发方案,整合了JavaFX、Spring Boot和React三大技术框架。系统采用前后端分离架构,JavaFX构建桌面客户端,React开发Web管理界面,Spring Boot提供RESTful API后端服务。核心功能模块包括客房管理和客户预订流程,文中提供了JavaFX实现的客房管理界面代码示例和React开发的预订组件代码,展示了如何实现客房信息展示、添加修改操作以及在线预订功能。
370 1
|
6月前
|
存储 Java 数据库连接
简单学Spring Boot | 博客项目的三层架构重构
本案例通过采用三层架构(数据访问层、业务逻辑层、表现层)重构项目,解决了集中式开发导致的代码臃肿问题。各层职责清晰,结合依赖注入实现解耦,提升了系统的可维护性、可测试性和可扩展性,为后续接入真实数据库奠定基础。
507 0
|
6月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
405 0