Adding more modifications + the part regarding advanced Docket configurations
This commit is contained in:
31
README.md
31
README.md
@@ -1,12 +1,41 @@
|
||||
# SpringBoot-SwaggerExamples
|
||||
|
||||
Repository made for the purpose of educational teaching of Swagger features in SpringBoot manner.
|
||||
|
||||
|
||||
The root of this repository contains multiple Spring Boot projects that sever different purposes for this topic. So if you need them to run them locally on your PC following the instructions from each of their directory will make your life easy, choose not to do this and it may take a few seconds more :)
|
||||
|
||||
**Remember**: "*Good Swagger Documentation is an essential ingredient of building good Spring Boot Microservices.*"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
### Resources used in this documentation:
|
||||
|
||||
[Advanced Swagger Configuration with Spring Boot](https://progressivecoder.com/advanced-swagger-configuration-with-spring-boot/)
|
||||
|
||||
[Swagger study notes](https://programmer.group/swagger-study-notes.html)
|
||||
|
||||
https://swagger.io/about/
|
||||
|
||||
https://www.cnblogs.com/softidea/p/6230709.html
|
||||
|
||||
[How can I manually describe an example input for a java @RequestBody Map<String, String>?](http://www.javawenti.com/?post=35548)
|
||||
|
||||
[Spring Boot整合SpringFox 3.0與Pageable引數處理_osc_qheq8wav - MdEditor](https://www.gushiciku.cn/pl/ph7P/zh-tw)
|
||||
|
||||
https://cloud.tencent.com/developer/article/1768024
|
||||
|
||||
[Springfox Reference Documentation](https://springfox.github.io/springfox/docs/current/)
|
||||
|
||||
[Setting Up Swagger 2 with a Spring REST API | Baeldung](https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api)
|
||||
|
||||
---
|
||||
|
||||
If you like my content, then just follow me on linkedin: https://www.linkedin.com/in/paladuta-stefan-255b6a136/
|
||||
or follow me on medium: https://medium.com/@stefan.paladuta17.
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 52 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 42 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 82 KiB |
@@ -1,29 +1,268 @@
|
||||
package ro.stefan.config;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.builders.ResponseBuilder;
|
||||
import springfox.documentation.schema.ModelRef;
|
||||
import springfox.documentation.service.Response;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
@Configuration
|
||||
public class SpringFoxConfig {
|
||||
|
||||
/* Chapter 1 Producing and Consuming */
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() {
|
||||
// final Set<String> produces = new HashSet<String>();
|
||||
// produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
// produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
// produces.add("Stefan's custom mapping value");
|
||||
//
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .produces(produces)
|
||||
// .consumes(produces)
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
/* Chapter 2 - Alter the general response */
|
||||
// @Autowired
|
||||
// private TypeResolver typeResolver;
|
||||
//
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() {
|
||||
// final Set<String> produces = new HashSet<String>();
|
||||
// produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
// produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
//
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .produces(produces)
|
||||
// .consumes(produces)
|
||||
// .additionalModels(typeResolver.resolve(Author.class))
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
|
||||
/* Chapter 3 - Global default example with header */
|
||||
// @Autowired
|
||||
// private TypeResolver typeResolver;
|
||||
//
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() {
|
||||
// final Set<String> produces = new HashSet<String>();
|
||||
// produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
// produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
//
|
||||
// List<RequestParameter> listRequestParamters = new ArrayList<RequestParameter>();
|
||||
// RequestParameter requestParamterToken = new RequestParameterBuilder()
|
||||
// .name("ACCESS_TOKEN")
|
||||
// .required(true)
|
||||
// .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
|
||||
// .in(ParameterType.HEADER)
|
||||
// .build();
|
||||
//
|
||||
// listRequestParamters.add(requestParamterToken);
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .produces(produces)
|
||||
// .consumes(produces)
|
||||
// .globalRequestParameters(listRequestParamters)
|
||||
// .additionalModels(typeResolver.resolve(Author.class))
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
/* Chapter 3 - Global default example with query parameter */
|
||||
// @Autowired
|
||||
// private TypeResolver typeResolver;
|
||||
//
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() {
|
||||
// final Set<String> produces = new HashSet<String>();
|
||||
// produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
// produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
//
|
||||
// List<RequestParameter> listRequestParamters = new ArrayList<RequestParameter>();
|
||||
// RequestParameter limitParameter = new RequestParameterBuilder()
|
||||
// .name("limit")
|
||||
// .required(false)
|
||||
// .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
|
||||
// .in(ParameterType.QUERY)
|
||||
// .build();
|
||||
// RequestParameter pagesParameter = new RequestParameterBuilder()
|
||||
// .name("pages")
|
||||
// .required(false)
|
||||
// .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
|
||||
// .in(ParameterType.QUERY)
|
||||
// .build();
|
||||
// listRequestParamters.add(limitParameter);
|
||||
// listRequestParamters.add(pagesParameter);
|
||||
//
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .produces(produces)
|
||||
// .consumes(produces)
|
||||
// .globalRequestParameters(listRequestParamters)
|
||||
// .additionalModels(typeResolver.resolve(Author.class))
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
/* Chapter 3 - Global default example with cookie */
|
||||
// @Autowired
|
||||
// private TypeResolver typeResolver;
|
||||
//
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() {
|
||||
// final Set<String> produces = new HashSet<String>();
|
||||
// produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
// produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
//
|
||||
// List<RequestParameter> listRequestParamters = new ArrayList<RequestParameter>();
|
||||
// RequestParameter timeSpentInApp = new RequestParameterBuilder()
|
||||
// .name("TIME_SPENT_IN_APP")
|
||||
// .required(false)
|
||||
// .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
|
||||
// .in(ParameterType.COOKIE)
|
||||
// .build();
|
||||
// RequestParameter isUserLoggedCookie = new RequestParameterBuilder()
|
||||
// .name("IS_USER_LOGGED")
|
||||
// .required(false)
|
||||
// .query(q -> q.model(modelSpecificationBuilder -> modelSpecificationBuilder.scalarModel(ScalarType.STRING)))
|
||||
// .in(ParameterType.COOKIE)
|
||||
// .build();
|
||||
// listRequestParamters.add(timeSpentInApp);
|
||||
// listRequestParamters.add(isUserLoggedCookie);
|
||||
//
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .produces(produces)
|
||||
// .consumes(produces)
|
||||
// .globalRequestParameters(listRequestParamters)
|
||||
// .additionalModels(typeResolver.resolve(Author.class))
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
|
||||
/* Chapter 4 - Global response default */
|
||||
// @Bean
|
||||
// public Docket getDocketInstanceEx1() throws JsonProcessingException {
|
||||
// List<Response> listResponses = new ArrayList<Response>();
|
||||
//
|
||||
// class ServerError{
|
||||
// private String serverIp;
|
||||
// private String serverName;
|
||||
// private String errorMsg;
|
||||
// private String errorStack;
|
||||
//
|
||||
// public String getServerIp() {
|
||||
// return serverIp;
|
||||
// }
|
||||
// public void setServerIp(String serverIp) {
|
||||
// this.serverIp = serverIp;
|
||||
// }
|
||||
// public String getServerName() {
|
||||
// return serverName;
|
||||
// }
|
||||
// public void setServerName(String serverName) {
|
||||
// this.serverName = serverName;
|
||||
// }
|
||||
// public String getErrorMsg() {
|
||||
// return errorMsg;
|
||||
// }
|
||||
// public void setErrorMsg(String errorMsg) {
|
||||
// this.errorMsg = errorMsg;
|
||||
// }
|
||||
// public String getErrorStack() {
|
||||
// return errorStack;
|
||||
// }
|
||||
// public void setErrorStack(String errorStack) {
|
||||
// this.errorStack = errorStack;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// ObjectMapper objMapper = new ObjectMapper();
|
||||
// listResponses.add(new ResponseBuilder().code("500").description(objMapper.writeValueAsString(new ServerError())).build());
|
||||
// listResponses.add(new ResponseBuilder().code("403").description("Forbidden!!!!!").build());
|
||||
//
|
||||
//
|
||||
// return new Docket(DocumentationType.SWAGGER_2)
|
||||
// .useDefaultResponseMessages(false)
|
||||
// .globalResponses(HttpMethod.GET,listResponses)
|
||||
// .select()
|
||||
// .apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
// .paths(PathSelectors.any())
|
||||
// .build();
|
||||
// }
|
||||
|
||||
/* Chapter 4 - Global response default example 2*/
|
||||
@Bean
|
||||
public Docket getDocketInstance() {
|
||||
final Set<String> produces = new HashSet<String>();
|
||||
produces.add(MediaType.APPLICATION_JSON_VALUE);
|
||||
produces.add(MediaType.APPLICATION_XML_VALUE);
|
||||
produces.add("Stefan's custom mapping value");
|
||||
public Docket getDocketInstanceEx1() throws JsonProcessingException {
|
||||
List<Response> listResponses = new ArrayList<Response>();
|
||||
|
||||
class ServerError{
|
||||
private String serverIp;
|
||||
private String serverName;
|
||||
private String errorMsg;
|
||||
private String errorStack;
|
||||
|
||||
public String getServerIp() {
|
||||
return serverIp;
|
||||
}
|
||||
public void setServerIp(String serverIp) {
|
||||
this.serverIp = serverIp;
|
||||
}
|
||||
public String getServerName() {
|
||||
return serverName;
|
||||
}
|
||||
public void setServerName(String serverName) {
|
||||
this.serverName = serverName;
|
||||
}
|
||||
public String getErrorMsg() {
|
||||
return errorMsg;
|
||||
}
|
||||
public void setErrorMsg(String errorMsg) {
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
public String getErrorStack() {
|
||||
return errorStack;
|
||||
}
|
||||
public void setErrorStack(String errorStack) {
|
||||
this.errorStack = errorStack;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ObjectMapper objMapper = new ObjectMapper();
|
||||
listResponses.add(new ResponseBuilder().code("500").description(objMapper.writeValueAsString(new ServerError())).build());
|
||||
listResponses.add(new ResponseBuilder().code("403").description("Forbidden!!!!!").build());
|
||||
|
||||
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.produces(produces)
|
||||
.useDefaultResponseMessages(false)
|
||||
.globalResponses(HttpMethod.GET,listResponses)
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("ro.stefan.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package ro.stefan.dto;
|
||||
|
||||
public class Author {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user