diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringDocExampleApplication.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringDocExampleApplication.java new file mode 100644 index 0000000000..3ae057630d --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringDocExampleApplication.java @@ -0,0 +1,17 @@ +package com.baeldung.springdoc.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.springdoc.demo.config.SpringDocSwaggerConfig; + +@SpringBootApplication +public class SpringDocExampleApplication { + + public static void main(String[] args) { + SpringApplication application = new SpringApplication(SpringDocExampleApplication.class); + //Note: SpringDocExampleApplication is the name of your main class + application.addListeners(new SpringDocSwaggerConfig()); + application.run(args); + } +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringFoxExampleApplication.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringFoxExampleApplication.java new file mode 100644 index 0000000000..c6106cc95d --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/SpringFoxExampleApplication.java @@ -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); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringDocSwaggerConfig.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringDocSwaggerConfig.java new file mode 100644 index 0000000000..5836235445 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringDocSwaggerConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.springdoc.demo.config; + +import java.util.Properties; + +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.stereotype.Component; + +@Component +public class SpringDocSwaggerConfig implements ApplicationListener { + + @Override + public void onApplicationEvent(final ApplicationPreparedEvent event) { + ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment(); + Properties props = new Properties(); + props.put("springdoc.swagger-ui.path", swaggerPath()); + environment.getPropertySources().addFirst(new PropertiesPropertySource("programmatically", props)); + } + + private String swaggerPath() { + return "/myproject"; // TODO: implement your logic here. + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringFoxSwaggerConfig.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringFoxSwaggerConfig.java new file mode 100644 index 0000000000..e07ca5c2a1 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/config/SpringFoxSwaggerConfig.java @@ -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<>()); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/SwaggerController.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/SwaggerController.java new file mode 100644 index 0000000000..79c35e025e --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/SwaggerController.java @@ -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"; + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/TopicsController.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/TopicsController.java new file mode 100644 index 0000000000..dee825788d --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/controller/TopicsController.java @@ -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> getAllTopics() { + return new ResponseEntity<>(topicService.getAlllTopics(), HttpStatus.OK); + } +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/model/Topic.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/model/Topic.java new file mode 100644 index 0000000000..0e4417b64f --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/model/Topic.java @@ -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; + } +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/service/TopicService.java b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/service/TopicService.java new file mode 100644 index 0000000000..a468d2915b --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/java/com/baeldung/springdoc/demo/service/TopicService.java @@ -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 topicsList; + + public TopicService(){ + this.topicsList = new ArrayList() {{ + add(new Topic(1, "Topic1")); + add(new Topic(2, "Topic2")); + add(new Topic(3, "Topic3")); + }}; + } + + public List getAlllTopics(){ + return topicsList; + } +} diff --git a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties index 03302ebb1c..901f0a6cc3 100644 --- a/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-swagger-2/src/main/resources/application.properties @@ -1,2 +1,7 @@ springdoc.api-docs.enabled=false springdoc.swagger-ui.url=/api_3.yaml + +# Properties for custom Springdoc swagger-ui url + +#springdoc.swagger-ui.disable-swagger-default-url=true +#springdoc.swagger-ui.path=/myproject \ No newline at end of file