BAEL-5329 Hide a Request Field in Swagger UI

This commit is contained in:
soni.sk@1969 2022-03-30 11:08:29 +05:30
parent 1df8b146ba
commit 1d1791c90d
4 changed files with 138 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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<Article> getAllArticles(){
return articleService.getAllArticles();
}
@PostMapping("")
public void addArticle(@RequestBody Article article) {
articleService.addArticle(article);
}
}

View File

@ -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;
}
}

View File

@ -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<Article> articles = new ArrayList<>();
public List<Article> getAllArticles(){
return articles;
}
public void addArticle(Article article) {
article.setId(articles.size()+1);
articles.add(article);
}
}