diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/AuthorsController.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/AuthorsController.java new file mode 100644 index 0000000000..019bd5ad9d --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/controller/AuthorsController.java @@ -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 getAllAuthors() { + return authorService.getAllAuthors(); + } + + @PostMapping + public void addAuthor(@RequestBody @JsonView(Views.Public.class) Author author){ + authorService.addAuthors(author); + } +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Author.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Author.java new file mode 100644 index 0000000000..2e30998059 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/model/Author.java @@ -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; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/AuthorService.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/AuthorService.java new file mode 100644 index 0000000000..bf1f637889 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/service/AuthorService.java @@ -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 authors = new ArrayList<>(); + + public List getAllAuthors(){ + return authors; + } + + public void addAuthors(Author author){ + author.setId(authors.size()+1); + authors.add(author); + } + +} diff --git a/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/views/Views.java b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/views/Views.java new file mode 100644 index 0000000000..df638a5647 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-4/src/main/java/com/baeldung/springboot/swagger/views/Views.java @@ -0,0 +1,9 @@ +package com.baeldung.springboot.swagger.views; + +public class Views { + public static class Public { + } + + public static class Private { + } +}