Skip to content

Commit 4baaf9f

Browse files
Thoughtscriptzhendrikse
authored andcommitted
BAEL-1085 (#2428)
* 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
1 parent b6830cc commit 4baaf9f

18 files changed

Lines changed: 668 additions & 3 deletions

File tree

spring-rest-angular/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Spring REST Angular Example Project
2+
3+
### Relevant Articles:
4+
- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations)

spring-rest-angular/pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<project
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4-
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>spring-rest-angular</artifactId>
77
<name>spring-rest-angular</name>
@@ -95,6 +95,7 @@
9595
</excludes>
9696
<includes>
9797
<include>**/*IntegrationTest.java</include>
98+
<include>**/*UnitTest.java</include>
9899
</includes>
99100
</configuration>
100101
</execution>
@@ -113,6 +114,7 @@
113114
<properties>
114115
<guava.version>19.0</guava.version>
115116
<commons-lang3.version>3.5</commons-lang3.version>
117+
<start-class>org.baeldung.web.main.Application</start-class>
116118
</properties>
117119

118120
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.controllers;
2+
3+
import com.baeldung.services.ExampleService;
4+
import com.baeldung.transfer.ResponseTransfer;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import com.baeldung.transfer.LoginForm;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
17+
@Controller
18+
@RequestMapping("/post")
19+
public class ExamplePostController {
20+
21+
private static Logger log = LoggerFactory.getLogger(ExamplePostController.class);
22+
23+
@Autowired ExampleService exampleService;
24+
25+
@PostMapping("/request")
26+
public ResponseEntity postController(@RequestBody LoginForm loginForm) {
27+
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
28+
exampleService.fakeAuthenticate(loginForm);
29+
return ResponseEntity.ok(HttpStatus.OK);
30+
}
31+
32+
@PostMapping("/response")
33+
@ResponseBody
34+
public ResponseTransfer postResponseController(@RequestBody LoginForm loginForm) {
35+
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
36+
return new ResponseTransfer("Thanks For Posting!!!");
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.controllers;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
6+
@Controller
7+
public class ViewController {
8+
9+
@GetMapping("/")
10+
public String welcome() {
11+
return "index";
12+
}
13+
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.services;
2+
3+
import com.baeldung.transfer.LoginForm;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class ExampleService {
8+
9+
public boolean fakeAuthenticate(LoginForm lf) {
10+
return true;
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.transfer;
2+
3+
public class LoginForm {
4+
private String username;
5+
private String password;
6+
7+
public LoginForm() {
8+
}
9+
10+
public String getUsername() {
11+
return username;
12+
}
13+
14+
public void setUsername(String username) {
15+
this.username = username;
16+
}
17+
18+
public String getPassword() {
19+
return password;
20+
}
21+
22+
public void setPassword(String password) {
23+
this.password = password;
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.transfer;
2+
3+
public class ResponseTransfer {
4+
5+
private String text;
6+
7+
public ResponseTransfer(String text) {
8+
this.setText(text);
9+
}
10+
11+
public String getText() {
12+
return text;
13+
}
14+
15+
public void setText(String text) {
16+
this.text = text;
17+
}
18+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
2+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3+
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
4+
5+
<!DOCTYPE html>
6+
<html ng-app='angularApp'>
7+
<head>
8+
<title>Spring Secured Sockets</title>
9+
<link href="<c:url value="/resources/styles/style.css"/>" rel="stylesheet">
10+
<script src="<c:url value="/resources/vendor/jquery/jquery.min.js" />"></script>
11+
<script src="<c:url value="/resources/vendor/angular/angular.min.js" />"></script>
12+
13+
<script src="<c:url value="/resources/scripts/app.js" />"></script>
14+
<script src="<c:url value="/resources/scripts/factory.js" />"></script>
15+
<script src="<c:url value="/resources/scripts/controllers.js" />"></script>
16+
</head>
17+
<body>
18+
<c:set var="context" scope="session" value="${pageContext.request.contextPath}"/>
19+
<h1>Welcome!</h1>
20+
<br/>
21+
<div class="requestBodyWrapper" ng-controller="requestBody">
22+
<h1>Request Body Controller POST Example</h1>
23+
<form>
24+
<table>
25+
<tr>
26+
<td>User:</td>
27+
<td><input id="requestBodyUserField" type="text" name="username"/></td>
28+
</tr>
29+
<tr>
30+
<td>Password:</td>
31+
<td><input id="requestBodyPasswordField" type="password" name="password"/></td>
32+
</tr>
33+
<tr>
34+
<td>
35+
<button ng-click='postForm("${context}")'>Submit</button>
36+
</td>
37+
</tr>
38+
</table>
39+
</form>
40+
</div>
41+
42+
<div class="responseBodyWrapper" ng-controller="responseBody">
43+
<h1>Response Body Controller POST Example</h1>
44+
<form>
45+
<table>
46+
<tr>
47+
<td>User:</td>
48+
<td><input id="responseBodyUserField" type="text" name="username"/></td>
49+
</tr>
50+
<tr>
51+
<td>Password:</td>
52+
<td><input id="responseBodyPasswordField" type="password" name="password"/></td>
53+
</tr>
54+
<tr>
55+
<td>
56+
<button ng-click='postForm("${context}")'>Submit</button>
57+
</td>
58+
</tr>
59+
</table>
60+
</form>
61+
<h3>{response}</h3>
62+
</div>
63+
</body>
64+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
var angularApp = angular.module('angularApp', ['ngRoute']);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
angularApp
2+
.controller('requestBody', function ($scope, Form) {
3+
$scope.postForm = function (context) {
4+
var userField = document.getElementById('userField');
5+
var passwordField = document.getElementById('passwordField');
6+
if (userField.value != '' && passwordField.value != '') {
7+
Form.post(context, userField.value,
8+
passwordField.value).then(function (v) {
9+
10+
});
11+
}
12+
};
13+
})
14+
15+
.controller('responseBody', function ($scope, Form) {
16+
$scope.response = "RESPONSEBODY WILL SHOW HERE";
17+
$scope.postForm = function (context) {
18+
var userField = document.getElementById('userField');
19+
var passwordField = document.getElementById('passwordField');
20+
if (userField.value != '' && passwordField.value != '') {
21+
Form.post(context, userField.value,
22+
passwordField.value).then(function (v) {
23+
$scope.response = v;
24+
});
25+
}
26+
};
27+
});

0 commit comments

Comments
 (0)