Java 21460 Move code from spring-boot-swagger & spring-boot-swagger-2 to spring-boot-swagger-springfox module (#14310)

* JAVA-21460 Upgrade spring-boot-swagger-jwt modules to use SpringDoc in place of SpringFox

* JAVA-21460 Upgrade spring-boot-swagger-2 module to use SpringDoc in place of SpringFox

* JAVA-21460 Upgrade spring-boot-swagger module to use SpringDoc in place of SpringFox

* JAVA-21460 Move code from spring-boot-swagger & spring-boot-swagger-2 to  spring-boot-swagger-springfox module

* JAVA-21457 Upgrade spring-security-web-rest to use SpringDoc in place of SpringFox
This commit is contained in:
anuragkumawat
2023-06-28 10:13:24 +05:30
committed by GitHub
parent 2f0f9d7e47
commit 44179d2187
32 changed files with 140 additions and 36 deletions
@@ -0,0 +1,12 @@
package com.baeldung.apiswagger;
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);
}
}
@@ -0,0 +1,26 @@
package com.baeldung.apiswagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfiguration {
public static final String BOOK_TAG = "book service";
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.tags(new Tag(BOOK_TAG, "the book API with description api tag"));
}
}
@@ -0,0 +1,21 @@
package com.baeldung.apiswagger.controller;
import com.baeldung.apiswagger.config.SwaggerConfiguration;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/api/book")
@Api(tags = {SwaggerConfiguration.BOOK_TAG})
public class BookController {
@GetMapping("/")
public List<String> getBooks() {
return Arrays.asList("book1", "book2");
}
}
@@ -0,0 +1,13 @@
package com.baeldung.springdoc.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringFoxExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringFoxExampleApplication.class, args);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.springdoc.demo.config;
import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SpringFoxSwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build().apiInfo(metaInfo());
}
private ApiInfo metaInfo() {
return new ApiInfo("Sample API REST", "API REST", "1.0", "Terms of Service", null, "Apache License Version 2.0",
"https://www.apache.org/licesen.html", new ArrayList<>());
}
}
@@ -0,0 +1,13 @@
package com.baeldung.springdoc.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SwaggerController {
@RequestMapping("/myproject")
public String getRedirectUrl() {
return "redirect:swagger-ui.html";
}
}
@@ -0,0 +1,26 @@
package com.baeldung.springdoc.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springdoc.demo.model.Topic;
import com.baeldung.springdoc.demo.service.TopicService;
@RestController
public class TopicsController {
@Autowired
TopicService topicService;
@GetMapping(value = "/topics")
public ResponseEntity<List<Topic>> getAllTopics() {
return new ResponseEntity<>(topicService.getAlllTopics(), HttpStatus.OK);
}
}
@@ -0,0 +1,26 @@
package com.baeldung.springdoc.demo.model;
public class Topic {
Integer id;
String name;
public Topic(Integer id, String name) {
super();
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,26 @@
package com.baeldung.springdoc.demo.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.baeldung.springdoc.demo.model.Topic;
@Service
public class TopicService {
private List<Topic> topicsList;
public TopicService(){
this.topicsList = new ArrayList<Topic>() {{
add(new Topic(1, "Topic1"));
add(new Topic(2, "Topic2"));
add(new Topic(3, "Topic3"));
}};
}
public List<Topic> getAlllTopics(){
return topicsList;
}
}
@@ -0,0 +1,44 @@
package com.baeldung.swagger2;
import static com.google.common.collect.Lists.newArrayList;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.baeldung.web.controller"))
.paths(PathSelectors.ant("/foos/*"))
.build()
.apiInfo(apiInfo())
.useDefaultResponseMessages(false)
.globalResponses(HttpMethod.GET, newArrayList(
new ResponseBuilder().code("500")
.description("500 message").build(),
new ResponseBuilder().code("403")
.description("Forbidden!!!!!").build()
));
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", new Contact("John Doe", "www.example.com", "myeaddress@company.com"), "License of API", "API license URL", Collections.emptyList());
return apiInfo;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.swagger2boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSwaggerApplication.class, args);
}
}
@@ -0,0 +1,63 @@
package com.baeldung.swagger2boot.configuration;
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.swagger.web.DocExpansion;
import springfox.documentation.swagger.web.ModelRendering;
import springfox.documentation.swagger.web.OperationsSorter;
import springfox.documentation.swagger.web.TagsSorter;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
@Configuration
public class SwaggerConfiguration {
private ApiInfo apiInfo() {
return new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service",
new Contact("Umang Budhwar", "www.baeldung.com", "umangbudhwar@gmail.com"),
"License of API", "API license URL", Collections.emptyList());
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* SwaggerUI information
*/
@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.deepLinking(true)
.displayOperationId(false)
.defaultModelsExpandDepth(1)
.defaultModelExpandDepth(1)
.defaultModelRendering(ModelRendering.EXAMPLE)
.displayRequestDuration(false)
.docExpansion(DocExpansion.NONE)
.filter(false)
.maxDisplayedTags(null)
.operationsSorter(OperationsSorter.ALPHA)
.showExtensions(false)
.tagsSorter(TagsSorter.ALPHA)
.supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
.validatorUrl(null)
.build();
}
}
@@ -0,0 +1,35 @@
package com.baeldung.swagger2boot.controller;
import java.time.LocalDate;
import java.time.LocalTime;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@RestController
public class RegularRestController {
@ApiIgnore
@ApiOperation(value = "This method is used to get the author name.")
@GetMapping("/getAuthor")
public String getAuthor() {
return "Umang Budhwar";
}
@ApiOperation(value = "This method is used to get the current date.", hidden = true)
@GetMapping("/getDate")
public LocalDate getDate() {
return LocalDate.now();
}
@ApiOperation(value = "This method is used to get the current time.")
@GetMapping("/getTime")
public LocalTime getTime() {
return LocalTime.now();
}
}
@@ -0,0 +1,13 @@
package com.baeldung.swaggerconf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootSwaggerConfApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSwaggerConfApplication.class, args);
}
}
@@ -0,0 +1,37 @@
package com.baeldung.swaggerconf.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
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.Collections;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
private ApiInfo apiInfo() {
return new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service",
new Contact("General UserName", "www.baeldung.com", "user-name@gmail.com"),
"License of API", "API license URL", Collections.emptyList());
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerconf.controller"))
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(regex("/good-path/.*"))
.build();
}
}
@@ -0,0 +1,17 @@
package com.baeldung.swaggerconf.controller;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@RequestMapping("good-path/error-excluded-annotation")
public class ErrorControllerExcludedForAnnotation extends BasicErrorController {
public ErrorControllerExcludedForAnnotation(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
super(errorAttributes, serverProperties.getError());
}
}
@@ -0,0 +1,21 @@
package com.baeldung.swaggerconf.controller;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
@Component
@RequestMapping("good-path/error-excluded-apiignore")
@RestController
@ApiIgnore
public class ErrorControllerExcludedForApiIgnore extends BasicErrorController {
public ErrorControllerExcludedForApiIgnore(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
super(errorAttributes, serverProperties.getError());
}
}
@@ -0,0 +1,19 @@
package com.baeldung.swaggerconf.controller;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Component
@RequestMapping("wrong-path/error-excluded-path")
@RestController
public class ErrorControllerExcludedForPath extends BasicErrorController {
public ErrorControllerExcludedForPath(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
super(errorAttributes, serverProperties.getError());
}
}
@@ -0,0 +1,33 @@
package com.baeldung.swaggerconf.controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalTime;
@RestController
@RequestMapping("good-path")
public class RegularRestController {
@ApiOperation(value = "This method is used to get the author name.")
@GetMapping("/getAuthor")
public String getAuthor() {
return "Name Surname";
}
@ApiOperation(value = "This method is used to get the current date.")
@GetMapping("/getDate")
public LocalDate getDate() {
return LocalDate.now();
}
@ApiOperation(value = "This method is used to get the current time.")
@GetMapping("/getTime")
public LocalTime getTime() {
return LocalTime.now();
}
}
@@ -0,0 +1,17 @@
package com.baeldung.swaggerconf.excluded;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@RequestMapping("good-path/error-excluded-package")
public class ErrorControllerExcludedForPackage extends BasicErrorController {
public ErrorControllerExcludedForPackage(ErrorAttributes errorAttributes, ServerProperties serverProperties) {
super(errorAttributes, serverProperties.getError());
}
}
@@ -0,0 +1,14 @@
package com.baeldung.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CustomController {
@RequestMapping(value = "/custom", method = RequestMethod.POST)
public String custom() {
return "custom";
}
}
@@ -0,0 +1 @@
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
@@ -0,0 +1,33 @@
package com.baeldung.swaggerconf;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SwaggerConfExcludeErrorControllerIntegrationTest {
@Autowired
private MockMvc mvc;
@Test
public void whenCallingSwaggerJSON_stringObjectDoesNotContainAnyErrorControllers() throws Exception {
ResultActions resultActions = mvc.perform(get("/v2/api-docs")).andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String content = result.getResponse().getContentAsString();
Assert.assertNotNull(content);
Assert.assertFalse(content.contains("error-controller"));
}
}
@@ -0,0 +1,20 @@
package com.baeldung.web;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
public class SwaggerLiveTest {
private static final String URL_PREFIX = "http://localhost:8080/spring-security-rest/api";
@Test
public void whenVerifySpringFoxIsWorking_thenOK() {
final Response response = RestAssured.get(URL_PREFIX + "/v2/api-docs");
assertEquals(200, response.statusCode());
System.out.println(response.asString());
}
}