Merge remote-tracking branch 'upstream/master' into JAVA-3570

This commit is contained in:
Krzysztof Woyke
2020-12-29 15:11:31 +01:00
630 changed files with 356 additions and 271 deletions
@@ -23,6 +23,12 @@ public class UserController {
this.userRepository = userRepository;
}
@GetMapping("/index")
public String showUserList(Model model) {
model.addAttribute("users", userRepository.findAll());
return "index";
}
@GetMapping("/signup")
public String showSignUpForm(User user) {
return "add-user";
@@ -35,7 +41,6 @@ public class UserController {
}
userRepository.save(user);
model.addAttribute("users", userRepository.findAll());
return "redirect:/index";
}
@@ -43,6 +48,7 @@ public class UserController {
public String showUpdateForm(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
model.addAttribute("user", user);
return "update-user";
}
@@ -54,7 +60,7 @@ public class UserController {
}
userRepository.save(user);
model.addAttribute("users", userRepository.findAll());
return "redirect:/index";
}
@@ -62,7 +68,7 @@ public class UserController {
public String deleteUser(@PathVariable("id") long id, Model model) {
User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id));
userRepository.delete(user);
model.addAttribute("users", userRepository.findAll());
return "index";
return "redirect:/index";
}
}
@@ -28,6 +28,11 @@ public class UserControllerUnitTest {
userController = new UserController(mockedUserRepository);
}
@Test
public void whenCalledIndex_thenCorrect() {
assertThat(userController.showUserList(mockedModel)).isEqualTo("index");
}
@Test
public void whenCalledshowSignUpForm_thenCorrect() {
User user = new User("John", "john@domain.com");
@@ -78,6 +83,6 @@ public class UserControllerUnitTest {
@Test(expected = IllegalArgumentException.class)
public void whenCalleddeleteUser_thenIllegalArgumentException() {
assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("index");
assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("redirect:/index");
}
}
@@ -0,0 +1,3 @@
### Relevant Articles:
- [Spring Boot: Customize the Jackson ObjectMapper](https://www.baeldung.com/spring-boot-customize-jackson-objectmapper)
@@ -15,3 +15,21 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring Boot and Togglz Aspect](https://www.baeldung.com/spring-togglz)
- [Getting Started with GraphQL and Spring Boot](https://www.baeldung.com/spring-graphql)
- [An Introduction to Kong](https://www.baeldung.com/kong)
### GraphQL sample queries
Query
```shell script
curl \
--request POST 'localhost:8081/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query {\n recentPosts(count: 2, offset: 0) {\n id\n title\n author {\n id\n posts {\n id\n }\n }\n }\n}"}'
```
Mutation
```shell script
curl \
--request POST 'localhost:8081/graphql' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation {\n writePost(title: \"New Title\", author: \"Author2\", text: \"New Text\") {\n id\n category\n author {\n id\n name\n }\n }\n}"}'
```
@@ -6,7 +6,6 @@
<artifactId>spring-boot-libraries</artifactId>
<name>spring-boot-libraries</name>
<packaging>war</packaging>
<description>This is simple boot application for Spring boot actuator test</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
@@ -2,12 +2,14 @@ package com.baeldung.demo;
import com.baeldung.graphql.GraphqlConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(GraphqlConfiguration.class)
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
@@ -1,7 +1,5 @@
package com.baeldung.graphql;
import java.util.Optional;
import com.coxautodev.graphql.tools.GraphQLResolver;
public class PostResolver implements GraphQLResolver<Post> {
@@ -11,7 +9,7 @@ public class PostResolver implements GraphQLResolver<Post> {
this.authorDao = authorDao;
}
public Optional<Author> getAuthor(Post post) {
return authorDao.getAuthor(post.getAuthorId());
public Author getAuthor(Post post) {
return authorDao.getAuthor(post.getAuthorId()).orElseThrow(RuntimeException::new);
}
}
@@ -0,0 +1,2 @@
server:
port: 8081
@@ -3,7 +3,7 @@ type Post {
title: String!
text: String!
category: String
author: Author
author: Author!
}
type Author {
@@ -9,7 +9,7 @@ server.error.include-exception=true
server.error.include-stacktrace=always
## Server connections configuration
server.tomcat.max-threads=200
server.tomcat.threads.max=200
server.connection-timeout=5s
server.max-http-header-size=8KB
server.tomcat.max-swallow-size=2MB
@@ -171,7 +171,7 @@
<properties>
<java.version>1.8</java.version>
<hibernate.version>5.2.10.Final</hibernate.version>
<springdoc.version>1.2.32</springdoc.version>
<springdoc.version>1.5.2</springdoc.version>
<asciidoctor-plugin.version>1.5.6</asciidoctor-plugin.version>
<kotlin.version>1.4.0</kotlin.version>
<snippetsDirectory>${project.build.directory}/generated-snippets</snippetsDirectory>
@@ -5,6 +5,17 @@ import java.util.Collection;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import com.baeldung.springdoc.exception.BookNotFoundException;
import com.baeldung.springdoc.model.Book;
import com.baeldung.springdoc.repository.BookRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@@ -21,17 +32,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.springdoc.exception.BookNotFoundException;
import com.baeldung.springdoc.model.Book;
import com.baeldung.springdoc.repository.BookRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@RestController
@RequestMapping("/api/book")
public class BookController {
@@ -58,7 +58,7 @@ public class BookController {
}
@GetMapping("/filter")
public Page<Book> filterBooks(Pageable pageable) {
public Page<Book> filterBooks(@ParameterObject Pageable pageable) {
return repository.getBooks(pageable);
}
@@ -1,169 +0,0 @@
{
"info": {
"_postman_id": "910d9690-f629-4491-bbbd-adb30982a386",
"name": "GraphQL collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "mutations",
"item": [
{
"name": "writePost",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "graphql",
"graphql": {
"query": "mutation writePost ($title: String!, $text: String!, $category: String) {\n writePost (title: $title, text: $text, category: $category) {\n id\n title\n text\n category\n }\n}",
"variables": "{\n \"title\": \"\",\n \"text\": \"\",\n \"category\": \"\"\n}"
},
"options": {
"graphql": {}
}
},
"url": {
"raw": "http://localhost:9090/springbootapp/graphql",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"springbootapp",
"graphql"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
},
{
"name": "queries",
"item": [
{
"name": "get recent posts",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "graphql",
"graphql": {
"query": "{\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n text\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}",
"variables": ""
}
},
"url": {
"raw": "http://localhost:9090/springbootapp/graphql",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"springbootapp",
"graphql"
]
}
},
"response": []
},
{
"name": "recentPosts - variables",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "graphql",
"graphql": {
"query": "query recentPosts ($count: Int, $offset: Int) {\n recentPosts (count: $count, offset: $offset) {\n id\n title\n text\n category\n }\n}",
"variables": "{\n \"count\": 1,\n \"offset\": 0\n}"
},
"options": {
"graphql": {}
}
},
"url": {
"raw": "http://localhost:9090/springbootapp/graphql",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"springbootapp",
"graphql"
]
}
},
"response": []
},
{
"name": "get recent posts - raw",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/graphql",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "query {\r\n recentPosts(count: 10, offset: 0) {\r\n id\r\n title\r\n category\r\n author {\r\n id\r\n name\r\n thumbnail\r\n }\r\n }\r\n}"
},
"url": {
"raw": "http://localhost:9090/springbootapp/graphql",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"springbootapp",
"graphql"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "b54f267b-c450-4f2d-8105-2f23bab4c922",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "00b575be-03d4-4b29-b137-733ead139638",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"id": "20a274e5-6d51-40d6-81cb-af9eb115b21b",
"key": "url",
"value": "",
"type": "string"
}
],
"protocolProfileBehavior": {}
}