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:
Adam InTae Gerard
2017-09-11 08:22:22 +02:00
committed by Grzegorz Piwowarek
parent d4d0305f3e
commit 3e192a0707
18 changed files with 16 additions and 460 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
## Spring REST Angular Example Project
### Relevant Articles:
- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations)
@@ -1,64 +0,0 @@
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<!DOCTYPE html>
<html ng-app='angularApp'>
<head>
<title>Spring Secured Sockets</title>
<link href="<c:url value="/resources/styles/style.css"/>" rel="stylesheet">
<script src="<c:url value="/resources/vendor/jquery/jquery.min.js" />"></script>
<script src="<c:url value="/resources/vendor/angular/angular.min.js" />"></script>
<script src="<c:url value="/resources/scripts/app.js" />"></script>
<script src="<c:url value="/resources/scripts/factory.js" />"></script>
<script src="<c:url value="/resources/scripts/controllers.js" />"></script>
</head>
<body>
<c:set var="context" scope="session" value="${pageContext.request.contextPath}"/>
<h1>Welcome!</h1>
<br/>
<div class="requestBodyWrapper" ng-controller="requestBody">
<h1>Request Body Controller POST Example</h1>
<form>
<table>
<tr>
<td>User:</td>
<td><input id="requestBodyUserField" type="text" name="username"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input id="requestBodyPasswordField" type="password" name="password"/></td>
</tr>
<tr>
<td>
<button ng-click='postForm("${context}")'>Submit</button>
</td>
</tr>
</table>
</form>
</div>
<div class="responseBodyWrapper" ng-controller="responseBody">
<h1>Response Body Controller POST Example</h1>
<form>
<table>
<tr>
<td>User:</td>
<td><input id="responseBodyUserField" type="text" name="username"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input id="responseBodyPasswordField" type="password" name="password"/></td>
</tr>
<tr>
<td>
<button ng-click='postForm("${context}")'>Submit</button>
</td>
</tr>
</table>
</form>
<h3>{response}</h3>
</div>
</body>
</html>
@@ -1,3 +0,0 @@
'use strict';
var angularApp = angular.module('angularApp', ['ngRoute']);
@@ -1,27 +0,0 @@
angularApp
.controller('requestBody', function ($scope, Form) {
$scope.postForm = function (context) {
var userField = document.getElementById('userField');
var passwordField = document.getElementById('passwordField');
if (userField.value != '' && passwordField.value != '') {
Form.post(context, userField.value,
passwordField.value).then(function (v) {
});
}
};
})
.controller('responseBody', function ($scope, Form) {
$scope.response = "RESPONSEBODY WILL SHOW HERE";
$scope.postForm = function (context) {
var userField = document.getElementById('userField');
var passwordField = document.getElementById('passwordField');
if (userField.value != '' && passwordField.value != '') {
Form.post(context, userField.value,
passwordField.value).then(function (v) {
$scope.response = v;
});
}
};
});
@@ -1,17 +0,0 @@
angularApp
.factory('Form', function ($http, $q) {
return {
post: function (context, username, password) {
var deferred = $q.defer();
$http({
method: 'POST',
url: context + '/authenticate',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({username: username, password: password})
}).then(function (response) {
deferred.resolve(response);
});
return deferred.promise;
}
}
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1
View File
@@ -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)
+1
View File
@@ -239,6 +239,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*UnitTest.java</include>
</includes>
</configuration>
</execution>
@@ -2,7 +2,6 @@ package com.baeldung.controllers;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import org.baeldung.web.main.Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -13,6 +12,7 @@ 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;
@@ -20,15 +20,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ExamplePostControllerRequestIntegrationTest {
@SpringBootTest(classes = MainApplication.class)
public class ExamplePostControllerRequestUnitTest {
MockMvc mockMvc;
@Mock
private ExampleService exampleService;
@InjectMocks
private ExamplePostController exampleController;
@Mock private ExampleService exampleService;
@InjectMocks private ExamplePostController exampleController;
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
private LoginForm lf = new LoginForm();
@@ -2,7 +2,6 @@ package com.baeldung.controllers;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import org.baeldung.web.main.Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -11,6 +10,7 @@ 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;
@@ -19,19 +19,16 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
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 = Application.class)
public class ExamplePostControllerResponseIntegrationTest {
private MockMvc mockMvc;
@Mock
private ExampleService exampleService;
@InjectMocks
private ExamplePostController exampleController;
@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
@@ -48,7 +45,6 @@ public class ExamplePostControllerResponseIntegrationTest {
public void requestBodyTest() {
try {
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
mockMvc
.perform(post("/post/response")
.content(jsonBody)