BAEL-1085: @RequestBody and @ResponseBody Annotation (#2583)
* Simple Boot REST application and example * BAEL-509 - Removed extra duplicate ant matcher * Example and Unit Tests * documentation * BAEL-1085 - changes per PR code review and document review - altered integration to unit test - all's good * BAEL-1085 - Renamed unit tests and added both to pom.xml * IntelliJ formatter * REVERT - Had removed a duplicate ant matcher from BAEL-509 - this was the incorrect process - reverting now, article has been corrected, but will issue a seperate PR for this * BAEL-509: Removed duplicate ant matcher - does not impact code at runtime * BAEL-1085: Per editor's request, removed Angular client - using CURL POST commands now in article - as such moved the code into a more appropriate module: spring-rest
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
d4d0305f3e
commit
3e192a0707
@@ -16,3 +16,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
- [HTTP PUT vs HTTP PATCH in a REST API](http://www.baeldung.com/http-put-patch-difference-spring)
|
||||
- [Exploring the Spring Boot TestRestTemplate](http://www.baeldung.com/spring-boot-testresttemplate)
|
||||
- [Spring – Log Incoming Requests](http://www.baeldung.com/spring-http-logging)
|
||||
- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations)
|
||||
@@ -239,6 +239,7 @@
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
<include>**/*UnitTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.ResponseTransfer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/post")
|
||||
public class ExamplePostController {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ExamplePostController.class);
|
||||
|
||||
@Autowired ExampleService exampleService;
|
||||
|
||||
@PostMapping("/request")
|
||||
public ResponseEntity postController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
exampleService.fakeAuthenticate(loginForm);
|
||||
return ResponseEntity.ok(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/response")
|
||||
@ResponseBody
|
||||
public ResponseTransfer postResponseController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
return new ResponseTransfer("Thanks For Posting!!!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class ViewController {
|
||||
|
||||
@GetMapping("/")
|
||||
public String welcome() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ExampleService {
|
||||
|
||||
public boolean fakeAuthenticate(LoginForm lf) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.transfer;
|
||||
|
||||
public class LoginForm {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public LoginForm() {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.transfer;
|
||||
|
||||
public class ResponseTransfer {
|
||||
|
||||
private String text;
|
||||
|
||||
public ResponseTransfer(String text) {
|
||||
this.setText(text);
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.baeldung.config.MainApplication;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerRequestUnitTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/request")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import org.baeldung.config.MainApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerResponseUnitTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/response")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"text\":\"Thanks For Posting!!!\"}"));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user