From 3f9bddc9ab91223111fbbe3105df0ac2e446f1c7 Mon Sep 17 00:00:00 2001 From: anuragkumawat Date: Tue, 6 Jun 2023 09:30:56 +0530 Subject: [PATCH] JAVA-21456 Upgrade spring-boot-mvc-4 to use SpringDoc in place of SpringFox (#14139) * JAVA-21456 Upgrade spring-boot-mvc-4 to use SpringDoc in place of SpringFox * JAVA-21456 Replace no longer needed ModelAttribute with RequestBody * JAVA-21456 Review comments changes * JAVA-21456 Formatting changes pom * JAVA-21456 Correct formatting pom --- spring-boot-modules/spring-boot-mvc-4/pom.xml | 12 ++++++------ .../swagger/ArticleApplication.java | 18 ++++++------------ .../controller/ArticlesController.java | 2 +- .../springboot/swagger/model/Article.java | 19 +++++++------------ 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/spring-boot-modules/spring-boot-mvc-4/pom.xml b/spring-boot-modules/spring-boot-mvc-4/pom.xml index b1c079b715..800ca8e31e 100644 --- a/spring-boot-modules/spring-boot-mvc-4/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-4/pom.xml @@ -32,11 +32,11 @@ spring-boot-devtools true - - io.springfox - springfox-boot-starter - ${spring.fox.version} - + + org.springdoc + springdoc-openapi-ui + ${springdoc.version} + com.fasterxml.jackson.core jackson-databind @@ -70,7 +70,7 @@ - 3.0.0 + 1.7.0 com.baeldung.springboot.swagger.ArticleApplication diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java index 8be380baa0..2b8388f914 100644 --- a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java @@ -5,14 +5,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; @SpringBootApplication -@EnableSwagger2 @EnableWebMvc public class ArticleApplication { @@ -21,12 +17,10 @@ public class ArticleApplication { } @Bean - public Docket api() { - return new Docket(DocumentationType.SWAGGER_2) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build(); + public OpenAPI openAPI() { + return new OpenAPI().info(new Info().title("SpringDoc example") + .description("SpringDoc application") + .version("v0.0.1")); } } diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java index c4336a7cfe..96812e367a 100644 --- a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java @@ -21,7 +21,7 @@ public class ArticlesController { } @PostMapping("") - public void addArticle(@ModelAttribute Article article) { + public void addArticle(@RequestBody Article article) { articleService.addArticle(article); } diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Article.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Article.java index f6318c04b3..8a54e54427 100644 --- a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Article.java +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Article.java @@ -1,21 +1,16 @@ package com.baeldung.springboot.swagger.model; import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonView; -import io.swagger.annotations.ApiModelProperty; -import io.swagger.annotations.ApiParam; - +import io.swagger.v3.oas.annotations.Hidden; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.media.Schema.AccessMode; public class Article { - //@JsonIgnore - //@JsonProperty(access = JsonProperty.Access.READ_ONLY) - //@ApiModelProperty(hidden = true) - //@ApiParam(hidden = true) - //@ApiModelProperty(readOnly = true) - @ApiParam(hidden = true) + // @JsonIgnore + // @JsonProperty(access = JsonProperty.Access.READ_ONLY) + // @Schema(accessMode = AccessMode.READ_ONLY) + @Hidden private int id; private String title; private int numOfWords;