Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spring-rest-angular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Spring REST Angular Example Project

### Relevant Articles:
- [RequestBody and ResponseBody Annotations](http://www.baeldung.com/requestbody-and-responsebody-annotations)
6 changes: 4 additions & 2 deletions spring-rest-angular/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-rest-angular</artifactId>
<name>spring-rest-angular</name>
Expand Down Expand Up @@ -95,6 +95,7 @@
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*UnitTest.java</include>
</includes>
</configuration>
</execution>
Expand All @@ -113,6 +114,7 @@
<properties>
<guava.version>19.0</guava.version>
<commons-lang3.version>3.5</commons-lang3.version>
<start-class>org.baeldung.web.main.Application</start-class>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -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!!!");
}
}
Original file line number Diff line number Diff line change
@@ -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";
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
64 changes: 64 additions & 0 deletions spring-rest-angular/src/main/webapp/WEB-INF/jsp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<%@ 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>
3 changes: 3 additions & 0 deletions spring-rest-angular/src/main/webapp/resources/scripts/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

var angularApp = angular.module('angularApp', ['ngRoute']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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;
});
}
};
});
17 changes: 17 additions & 0 deletions spring-rest-angular/src/main/webapp/resources/scripts/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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;
}
}
});
Empty file.
Loading