diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml index 4e8001ae7b..1845d60e94 100644 --- a/spring-data-rest/pom.xml +++ b/spring-data-rest/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.4.RELEASE + 1.5.1.RELEASE diff --git a/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java new file mode 100644 index 0000000000..bed1a6f846 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@EnableWebMvc +public class MvcConfig extends WebMvcConfigurerAdapter{ + + public MvcConfig(){ + super(); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java index 0b55ac89b6..2bd9c025dd 100644 --- a/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java @@ -2,9 +2,10 @@ package com.baeldung.repositories; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; - +import org.springframework.web.bind.annotation.CrossOrigin; import com.baeldung.models.WebsiteUser; +@CrossOrigin @RepositoryRestResource(collectionResourceRel = "users", path = "users") public interface UserRepository extends CrudRepository { diff --git a/spring-data-rest/src/main/webapp/users.html b/spring-data-rest/src/main/webapp/users.html new file mode 100644 index 0000000000..c153f4a36e --- /dev/null +++ b/spring-data-rest/src/main/webapp/users.html @@ -0,0 +1,51 @@ + + + + +User CRUD + + + + + +
+ + + + + + + + + + + + + +
ID:
Name:
Email:
+

+ Get User + Update User + Add User + Delete User + +

+

{{message}}

+

{{errorMessage}}

+ +
+
+ Get all Users
+

+
+ {{usr.name}} {{usr.email}} +
+
+ + \ No newline at end of file diff --git a/spring-data-rest/src/main/webapp/view/app.js b/spring-data-rest/src/main/webapp/view/app.js new file mode 100644 index 0000000000..0bdc6e1979 --- /dev/null +++ b/spring-data-rest/src/main/webapp/view/app.js @@ -0,0 +1,122 @@ +var app = angular.module('app',[]); + +app.controller('UserCRUDCtrl', ['$scope','UserCRUDService', function ($scope,UserCRUDService) { + + $scope.updateUser = function () { + UserCRUDService.updateUser($scope.user.id,$scope.user.name,$scope.user.email) + .then(function success(response){ + $scope.message = 'User data updated!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error updating user!'; + $scope.message = ''; + }); + } + + $scope.getUser = function () { + var id = $scope.user.id; + UserCRUDService.getUser($scope.user.id) + .then(function success(response){ + $scope.user = response.data; + $scope.user.id = id; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message = ''; + if (response.status === 404){ + $scope.errorMessage = 'User not found!'; + } + else { + $scope.errorMessage = "Error getting user!"; + } + }); + } + + $scope.addUser = function () { + if ($scope.user != null && $scope.user.name) { + UserCRUDService.addUser($scope.user.name, $scope.user.email) + .then (function success(response){ + $scope.message = 'User added!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error adding user!'; + $scope.message = ''; + }); + } + else { + $scope.errorMessage = 'Please enter a name!'; + $scope.message = ''; + } + } + + $scope.deleteUser = function () { + UserCRUDService.deleteUser($scope.user.id) + .then (function success(response){ + $scope.message = 'User deleted!'; + $scope.user = null; + $scope.errorMessage=''; + }, + function error(response){ + $scope.errorMessage = 'Error deleting user!'; + $scope.message=''; + }) + } + + $scope.getAllUsers = function () { + UserCRUDService.getAllUsers() + .then(function success(response){ + $scope.users = response.data._embedded.users; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message=''; + $scope.errorMessage = 'Error getting users!'; + }); + } + +}]); + +app.service('UserCRUDService',['$http', function ($http) { + + this.getUser = function getUser(userId){ + return $http({ + method: 'GET', + url: 'users/'+userId + }); + } + + this.addUser = function addUser(name, email){ + return $http({ + method: 'POST', + url: 'users', + data: {name:name, email:email} + }); + } + + this.deleteUser = function deleteUser(id){ + return $http({ + method: 'DELETE', + url: 'users/'+id + }) + } + + this.updateUser = function updateUser(id,name,email){ + return $http({ + method: 'PATCH', + url: 'users/'+id, + data: {name:name, email:email} + }) + } + + this.getAllUsers = function getAllUsers(){ + return $http({ + method: 'GET', + url: 'users' + }); + } + +}]); \ No newline at end of file diff --git a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java index 21a067a645..43b5dd7da6 100644 --- a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java +++ b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java @@ -18,6 +18,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; +import org.json.JSONException; import static org.junit.Assert.assertEquals; @@ -37,7 +38,7 @@ public class SpringDataRelationshipsTest { private static final String AUTHOR_NAME = "George Orwell"; @Test - public void whenSaveOneToOneRelationship_thenCorrect() { + public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException { Library library = new Library(LIBRARY_NAME); template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); @@ -55,7 +56,7 @@ public class SpringDataRelationshipsTest { } @Test - public void whenSaveOneToManyRelationship_thenCorrect() { + public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{ Library library = new Library(LIBRARY_NAME); template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); @@ -77,7 +78,7 @@ public class SpringDataRelationshipsTest { } @Test - public void whenSaveManyToManyRelationship_thenCorrect() { + public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{ Author author1 = new Author(AUTHOR_NAME); template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class);