当前位置: 首页 > 知识库问答 >
问题:

如何在spring-boot-starter-data-rest中为Swagger文档实现snake_case?

甄德寿
2023-03-14

我正在使用swagger进行spring-boot-starter-data-rest项目的文档。在application.properties文件中,我配置了:spring.jackson.propertyNaming-strategy=snake_case命名策略,但不幸的是,我在swagger文档中得到了camelCase。但是,如果我将项目从spring-boot-starter-data-rest更改为spring-boot-starter-web,同样的配置也会起作用。下面是我在spring Boot2.1.1版本中使用的依赖项。

<?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 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.1.1.RELEASE</version>
                <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.example</groupId>
            <artifactId>demo1</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <name>demo1</name>
            <description>Demo project for Spring Boot</description>
            <properties>
                <java.version>1.8</java.version>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger-ui</artifactId>
                    <version>2.9.2</version>
                </dependency>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger2</artifactId>
                    <version>2.9.2</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-rest</artifactId>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </project>

应用程序.属性

        spring.jackson.property-naming-strategy=SNAKE_CASE
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class Demo1Application {

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

        }

    }
    package com.example.demo1;

    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.PropertyNamingStrategy;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    import 
    org.springframework.data.web.config.SpringDataJacksonConfiguration;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

    import java.util.ArrayList;

    @Configuration
    @EnableSwagger2
    @Import({SpringDataJacksonConfiguration.class})
    public class SwaggerConfig {


        public static ApiInfo metaData(String info) {
            return new ApiInfo(info,
                    "Th",
                    "1.0", "httn.html",
                    new Contact("Thd", "", "thoom"), "decense",
                    "https", new ArrayList());
        }

        @Bean
        public Docket cashFlowApi() {
            return new Docket(DocumentationType.SWAGGER_2).groupName("-caching").select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo1"))
                    .paths(PathSelectors.any())
                    .build()
                    .apiInfo(SwaggerConfig.metaData("BOcPI"));
        }
    }
    package com.example.demo1;

    import java.util.Arrays;
    import java.util.List;

    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import io.swagger.annotations.ApiResponse;
    import io.swagger.annotations.ApiResponses;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.CrossOrigin;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;


    @RestController
    @CrossOrigin
    public class StoreController {

        @GetMapping(value = "/v1/storeMap")
        @ApiOperation(value = "Returns the list of stores", notes = "Returns the list of stores with pagination.")
        @ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully retrieved the stores list"),
                @ApiResponse(code = 204, message = "No content"), @ApiResponse(code = 206, message = "Partial Content"),
                @ApiResponse(code = 401, message = "You are not authorized to view the resource"),
                @ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
                @ApiResponse(code = 404, message = "The resource you were trying to reach is not found"),
                @ApiResponse(code = 500, message = "A technical error happened")})

        public ResponseEntity<Store> getStore(
                @RequestParam(name = "country_code", required = false) @ApiParam(value = "the code)") String countryCode
                ) {

            return ResponseEntity.ok(new Store(1,"ZZ"));
        }

    }

共有1个答案

霍修筠
2023-03-14

我找到了解决方案,问题是对象映射器:

    @Configuration
    public class ObjectMapperAutoConfiguration implements WebMvcConfigurer {

        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            ObjectMapper objectMapper = null;
            for (HttpMessageConverter converter : converters) {
                if (converter instanceof MappingJackson2HttpMessageConverter ) {
                    MappingJackson2HttpMessageConverter jacksonConverter =
                            ((MappingJackson2HttpMessageConverter) converter);

                    if (objectMapper == null) {
                        objectMapper = jacksonConverter.getObjectMapper();
                    } else {
                        jacksonConverter.setObjectMapper(objectMapper);
                    }
                }
            }
        }
    }
 类似资料:
  • 该项目主要利用Spring Boot的自动化配置特性来实现快速地将swagger2引入spring boot应用来生成API文档,简化原生使用swagger2的整合代码。

  • null 我想知道这三者之间的区别。尝试搜索在线文档,其中说这三个几乎与spring数据有关。如何化解困惑? 在这里,我还发现了另一个依赖项spring-boot-starter-web-services。我认为它同时支持SOAP和REST。这只是我的假设,我在等待一个解释

  • 我在Kubernetes(spring boot starter父版本2.2.4.RELEASE)中部署了一个带有开放api 3的spring boot应用程序。其余endpoint可以通过入口访问,因此URL类似于: 我有一个问题,一旦应用程序被部署到入口,api-docs也可以通过: 当我打开斯威格UI我有404,因为斯威格UI似乎正在调查https://host/v3/api-docs...

  • 我在Spring Boot中有一个带有Spring Data Rest的应用程序,我正在尝试使用Swagger maven插件生成带有Swagger的文档。生成控制器文档时没有问题,但存储库没有问题。 我已经在pom中配置了如下形式的swagger maven插件。xml: 我的随身携带物品: 是否可以用swagger-maven-plugin生成存储库留档?

  • 我想在Spring Boot中实现相关id,并想在响应体中发送,有人能帮我吗?我是一个初露头角的Java开发者。 {"相关Guid":"a33b177f-9ea1-4d5d-9f23-b6e4a4a627b5"//然后回复的主要内容。}

  • 问题内容: 我试图将@NotNull约束添加到我的Person对象中,但是我仍然可以@POST具有空电子邮件的新Person。我在MongoDB上使用Spring Boot Rest。 实体类: 存储库类: 应用类别: pom.xml 当我通过邮递员@POST一个新对象时,例如: 我仍然使用此有效负载创建: 问题答案: 我遇到了同样的问题,但是仅启用验证对我而言不起作用,这确实与JPA和Mongo