From 9734a47ea61eadff0b287f793921cf31f6a610c2 Mon Sep 17 00:00:00 2001 From: enpy Date: Mon, 26 Mar 2018 08:22:47 +0200 Subject: [PATCH] BAEL-1649 Difference between @Controller and @RestController (#3885) * Difference between @Controller and @RestController * Difference between @Controller and @RestController - test fix --- .../main/java/com/baeldung/model/Book.java | 33 +++++++++++++++++ .../web/controller/SimpleBookController.java | 31 ++++++++++++++++ .../controller/SimpleBookRestController.java | 29 +++++++++++++++ .../controller/SimpleBookControllerTest.java | 35 +++++++++++++++++++ .../SimpleBookRestControllerTest.java | 35 +++++++++++++++++++ 5 files changed, 163 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/model/Book.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookController.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookRestController.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookControllerTest.java create mode 100644 spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookRestControllerTest.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/Book.java b/spring-mvc-java/src/main/java/com/baeldung/model/Book.java new file mode 100644 index 0000000000..b0cabe0125 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/Book.java @@ -0,0 +1,33 @@ +package com.baeldung.model; + +public class Book { + + private int id; + private String author; + private String title; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookController.java new file mode 100644 index 0000000000..37c6bac35b --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookController.java @@ -0,0 +1,31 @@ +package com.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.model.Book; + +@Controller +@RequestMapping("books") +public class SimpleBookController { + + @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json") + public @ResponseBody Book getBook(@PathVariable int id) { + return findBookById(id); + } + + private Book findBookById(int id) { + Book book = null; + if (id == 42) { + book = new Book(); + book.setId(id); + book.setAuthor("Douglas Adamas"); + book.setTitle("Hitchhiker's guide to the galaxy"); + } + return book; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookRestController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookRestController.java new file mode 100644 index 0000000000..85c60ce650 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/SimpleBookRestController.java @@ -0,0 +1,29 @@ +package com.baeldung.web.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.model.Book; + +@RestController +@RequestMapping("books-rest") +public class SimpleBookRestController { + + @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json") + public Book getBook(@PathVariable int id) { + return findBookById(id); + } + + private Book findBookById(int id) { + Book book = null; + if (id == 42) { + book = new Book(); + book.setId(id); + book.setAuthor("Douglas Adamas"); + book.setTitle("Hitchhiker's guide to the galaxy"); + } + return book; + } +} diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookControllerTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookControllerTest.java new file mode 100644 index 0000000000..4be0ded963 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookControllerTest.java @@ -0,0 +1,35 @@ +package com.baeldung.web.controller; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.baeldung.web.controller.SimpleBookController; + +public class SimpleBookControllerTest { + + private MockMvc mockMvc; + private static final String CONTENT_TYPE = "application/json;charset=UTF-8"; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookController()).build(); + } + + @Test + public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception { + this.mockMvc + .perform(get("/books/42")) + .andExpect(status().isOk()) + .andExpect(content().contentType(CONTENT_TYPE)) + .andExpect(jsonPath("$.id").value(42)); + } + +} + diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookRestControllerTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookRestControllerTest.java new file mode 100644 index 0000000000..23b8c639d3 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/SimpleBookRestControllerTest.java @@ -0,0 +1,35 @@ +package com.baeldung.web.controller; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.baeldung.web.controller.SimpleBookController; + +public class SimpleBookRestControllerTest { + + private MockMvc mockMvc; + private static final String CONTENT_TYPE = "application/json;charset=UTF-8"; + + @Before + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new SimpleBookController()).build(); + } + + @Test + public void givenBookId_whenMockMVC_thenVerifyResponse() throws Exception { + this.mockMvc + .perform(get("/books/42")) + .andExpect(status().isOk()) + .andExpect(content().contentType(CONTENT_TYPE)) + .andExpect(jsonPath("$.id").value(42)); + } + +} +