JAVA-18299 : Changes made for adding json view example fot hiding req… (#13829)

* JAVA-18299 : Changes made for adding json view example fot hiding request feilds in swagger

* JAVA-18299: Changes made for adding new example for JsonView Support in swagger hiding field

* JAVA-18299: Changes made for adding new example for JsonView Support in swagger hiding field

* JAVA-18299: Changes made for removing extra boiler plate code

* JAVA-18299: Changes made to incorporate review comments
This commit is contained in:
Bipin kumar 2023-04-26 12:28:12 +05:30 committed by GitHub
parent c8cd042d64
commit 8f5aff6073
4 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package com.baeldung.springboot.swagger.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springboot.swagger.model.Author;
import com.baeldung.springboot.swagger.service.AuthorService;
import com.baeldung.springboot.swagger.views.Views;
import com.fasterxml.jackson.annotation.JsonView;
@RestController
@RequestMapping("/authors")
public class AuthorsController {
@Autowired
AuthorService authorService;
@JsonView(Views.Public.class)
@GetMapping
public List<Author> getAllAuthors() {
return authorService.getAllAuthors();
}
@PostMapping
public void addAuthor(@RequestBody @JsonView(Views.Public.class) Author author){
authorService.addAuthors(author);
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.springboot.swagger.model;
import com.baeldung.springboot.swagger.views.Views;
import com.fasterxml.jackson.annotation.JsonView;
public class Author {
@JsonView(Views.Private.class)
private Integer id;
@JsonView(Views.Public.class)
private String name;
@JsonView(Views.Public.class)
private String email;
public Author() {
}
public Author(String name, String email) {
this.name = name;
this.email = email;
}
public void setId(Integer id) {
this.id = id;
}
}

View File

@ -0,0 +1,23 @@
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.Author;
@Service
public class AuthorService {
private List<Author> authors = new ArrayList<>();
public List<Author> getAllAuthors(){
return authors;
}
public void addAuthors(Author author){
author.setId(authors.size()+1);
authors.add(author);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.springboot.swagger.views;
public class Views {
public static class Public {
}
public static class Private {
}
}