From 1d1791c90d989b111c21474ec9332685e12d61dd Mon Sep 17 00:00:00 2001 From: "soni.sk@1969" Date: Wed, 30 Mar 2022 11:08:29 +0530 Subject: [PATCH 1/3] BAEL-5329 Hide a Request Field in Swagger UI --- .../swagger/ArticleApplication.java | 31 +++++++++++ .../controller/ArticlesController.java | 28 ++++++++++ .../springboot/swagger/model/Article.java | 54 +++++++++++++++++++ .../swagger/service/ArticleService.java | 25 +++++++++ 4 files changed, 138 insertions(+) create mode 100644 spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java create mode 100644 spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java create mode 100644 spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java create mode 100644 spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java new file mode 100644 index 0000000000..fd03efa695 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java @@ -0,0 +1,31 @@ +package com.baeldung.springboot.swagger; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +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; + +@SpringBootApplication +@EnableSwagger2 +public class ArticleApplication { + + public static void main(String[] args) { + SpringApplication.run(ArticleApplication.class, args); + + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java new file mode 100644 index 0000000000..380f40dbd5 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java @@ -0,0 +1,28 @@ +package com.baeldung.springboot.swagger.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import com.baeldung.springboot.swagger.model.Article; +import com.baeldung.springboot.swagger.service.ArticleService; + +@RestController +@RequestMapping("/articles") +public class ArticlesController { + + @Autowired + private ArticleService articleService; + + @GetMapping("") + public List
getAllArticles(){ + return articleService.getAllArticles(); + } + + @PostMapping("") + public void addArticle(@RequestBody Article article) { + articleService.addArticle(article); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java new file mode 100644 index 0000000000..ba927d5b4f --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java @@ -0,0 +1,54 @@ +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; + +public class Article { + + //@JsonIgnore + //@JsonProperty(access = JsonProperty.Access.READ_ONLY) + //@ApiModelProperty(hidden = true) + @ApiParam(hidden = true) + //@ApiModelProperty(readOnly = true) + private int id; + private String title; + private int numOfWords; + + public Article() { + } + + public Article(int id, String title) { + this.id = id; + this.title = title; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getNumOfWords() { + return numOfWords; + } + + public void setNumOfWords(int numOfWords) { + this.numOfWords = numOfWords; + } + +} diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java new file mode 100644 index 0000000000..b41bfabb29 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java @@ -0,0 +1,25 @@ +package com.baeldung.springboot.swagger.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.baeldung.springboot.swagger.model.Article; + +@Service +public class ArticleService { + + private List
articles = new ArrayList<>(); + + public List
getAllArticles(){ + return articles; + } + + public void addArticle(Article article) { + article.setId(articles.size()+1); + articles.add(article); + } + + +} From 31a9e429f1d64e9b5d867f4d20422b1a5d337c9b Mon Sep 17 00:00:00 2001 From: "soni.sk@1969" Date: Wed, 30 Mar 2022 11:09:45 +0530 Subject: [PATCH 2/3] Updated indentation --- .../swagger/ArticleApplication.java | 25 +++++++++--------- .../controller/ArticlesController.java | 26 +++++++++---------- .../springboot/swagger/model/Article.java | 2 +- .../swagger/service/ArticleService.java | 22 ++++++++-------- 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java index fd03efa695..568d31e8bc 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java @@ -14,18 +14,17 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 public class ArticleApplication { - public static void main(String[] args) { - SpringApplication.run(ArticleApplication.class, args); - - } - - @Bean - public Docket api() { - return new Docket(DocumentationType.SWAGGER_2) - .select() - .apis(RequestHandlerSelectors.any()) - .paths(PathSelectors.any()) - .build(); - } + public static void main(String[] args) { + SpringApplication.run(ArticleApplication.class, args); + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } } diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java index 380f40dbd5..96812e367a 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java @@ -11,18 +11,18 @@ import com.baeldung.springboot.swagger.service.ArticleService; @RestController @RequestMapping("/articles") public class ArticlesController { - - @Autowired - private ArticleService articleService; - - @GetMapping("") - public List
getAllArticles(){ - return articleService.getAllArticles(); - } - - @PostMapping("") - public void addArticle(@RequestBody Article article) { - articleService.addArticle(article); - } + + @Autowired + private ArticleService articleService; + + @GetMapping("") + public List
getAllArticles() { + return articleService.getAllArticles(); + } + + @PostMapping("") + public void addArticle(@RequestBody Article article) { + articleService.addArticle(article); + } } diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java index ba927d5b4f..6512b4e1a7 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java @@ -13,7 +13,7 @@ public class Article { //@JsonIgnore //@JsonProperty(access = JsonProperty.Access.READ_ONLY) //@ApiModelProperty(hidden = true) - @ApiParam(hidden = true) + //@ApiParam(hidden = true) //@ApiModelProperty(readOnly = true) private int id; private String title; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java index b41bfabb29..04f6e6c6e3 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java @@ -9,17 +9,17 @@ import com.baeldung.springboot.swagger.model.Article; @Service public class ArticleService { - - private List
articles = new ArrayList<>(); - - public List
getAllArticles(){ - return articles; - } - - public void addArticle(Article article) { - article.setId(articles.size()+1); - articles.add(article); - } + + private List
articles = new ArrayList<>(); + + public List
getAllArticles() { + return articles; + } + + public void addArticle(Article article) { + article.setId(articles.size() + 1); + articles.add(article); + } } From d8b5ab011172d8876fc9f3e0ee3605188b845ca0 Mon Sep 17 00:00:00 2001 From: "soni.sk@1969" Date: Thu, 14 Apr 2022 21:31:50 +0530 Subject: [PATCH 3/3] BAEL-5329 : Updated code module --- .../spring-boot-mvc-4/README.md | 4 ++ spring-boot-modules/spring-boot-mvc-4/pom.xml | 62 +++++++++++++++++++ .../swagger/ArticleApplication.java | 2 + .../controller/ArticlesController.java | 2 +- .../springboot/swagger/model/Article.java | 1 + .../swagger/service/ArticleService.java | 2 +- .../src/main/resources/static/index.html | 1 + 7 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 spring-boot-modules/spring-boot-mvc-4/README.md create mode 100644 spring-boot-modules/spring-boot-mvc-4/pom.xml rename spring-boot-modules/{spring-boot-mvc => spring-boot-mvc-4}/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java (91%) rename spring-boot-modules/{spring-boot-mvc => spring-boot-mvc-4}/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java (91%) rename spring-boot-modules/{spring-boot-mvc => spring-boot-mvc-4}/src/main/java/com/baeldung/springboot/swagger/model/Article.java (97%) rename spring-boot-modules/{spring-boot-mvc => spring-boot-mvc-4}/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java (99%) create mode 100644 spring-boot-modules/spring-boot-mvc-4/src/main/resources/static/index.html diff --git a/spring-boot-modules/spring-boot-mvc-4/README.md b/spring-boot-modules/spring-boot-mvc-4/README.md new file mode 100644 index 0000000000..651453c6d4 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/README.md @@ -0,0 +1,4 @@ +## Spring Boot MVC + +This module contains articles about Spring Web MVC in Spring Boot projects. + diff --git a/spring-boot-modules/spring-boot-mvc-4/pom.xml b/spring-boot-modules/spring-boot-mvc-4/pom.xml new file mode 100644 index 0000000000..edf6185259 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + spring-boot-mvc-4 + spring-boot-mvc-4 + jar + Module For Spring Boot + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-devtools + true + + + io.springfox + springfox-boot-starter + ${spring.fox.version} + + + com.fasterxml.jackson.core + jackson-databind + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + ${start-class} + JAR + + + + + + + + 3.0.0 + com.baeldung.springboot.swagger.ArticleApplication + + + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-mvc/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 similarity index 91% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java rename to spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java index 568d31e8bc..8be380baa0 100644 --- a/spring-boot-modules/spring-boot-mvc/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 @@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication; 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; @@ -12,6 +13,7 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 +@EnableWebMvc public class ArticleApplication { public static void main(String[] args) { diff --git a/spring-boot-modules/spring-boot-mvc/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 similarity index 91% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java rename to spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/ArticlesController.java index 96812e367a..c4336a7cfe 100644 --- a/spring-boot-modules/spring-boot-mvc/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(@RequestBody Article article) { + public void addArticle(@ModelAttribute Article article) { articleService.addArticle(article); } diff --git a/spring-boot-modules/spring-boot-mvc/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 similarity index 97% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/model/Article.java rename to spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Article.java index 6512b4e1a7..f6318c04b3 100644 --- a/spring-boot-modules/spring-boot-mvc/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 @@ -15,6 +15,7 @@ public class Article { //@ApiModelProperty(hidden = true) //@ApiParam(hidden = true) //@ApiModelProperty(readOnly = true) + @ApiParam(hidden = true) private int id; private String title; private int numOfWords; diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java similarity index 99% rename from spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java rename to spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java index 04f6e6c6e3..3fd0279988 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/ArticleService.java @@ -20,6 +20,6 @@ public class ArticleService { article.setId(articles.size() + 1); articles.add(article); } - + } diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/resources/static/index.html b/spring-boot-modules/spring-boot-mvc-4/src/main/resources/static/index.html new file mode 100644 index 0000000000..9f55d12685 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/resources/static/index.html @@ -0,0 +1 @@ +Welcome to Baeldung \ No newline at end of file