From dbbc6a6a1df0a3d9d1fc5a32324bc403f2d08acd Mon Sep 17 00:00:00 2001 From: priya-soni Date: Tue, 5 Apr 2022 00:14:28 +0530 Subject: [PATCH] BAEL-5329 Hide a Request Field in Swagger UI (#11992) * BAEL-5329 Hide a Request Field in Swagger UI * Updated indentation --- .../swagger/ArticleApplication.java | 30 +++++++++++ .../controller/ArticlesController.java | 28 ++++++++++ .../springboot/swagger/model/Article.java | 54 +++++++++++++++++++ .../swagger/service/ArticleService.java | 25 +++++++++ 4 files changed, 137 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..568d31e8bc --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/springboot/swagger/ArticleApplication.java @@ -0,0 +1,30 @@ +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..96812e367a --- /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..6512b4e1a7 --- /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..04f6e6c6e3 --- /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); + } + + +}