diff --git a/spring-data-rest-2/README.md b/spring-data-rest-2/README.md new file mode 100644 index 0000000000..04a54291b0 --- /dev/null +++ b/spring-data-rest-2/README.md @@ -0,0 +1,20 @@ +## Spring Data REST + +This module contains articles about Spring Data REST + +### Relevant Articles: +- [Guide to Spring Data REST Validators](https://www.baeldung.com/spring-data-rest-validators) +- [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support) +- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal) + +### The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +# Running the project +The application uses [Spring Boot](http://projects.spring.io/spring-boot/), so it is easy to run. You can start it any of a few ways: +* Run the `main` method from `SpringDataRestApplication` +* Use the Maven Spring Boot plugin: `mvn spring-boot:run` +* Package the application as a JAR and run it using `java -jar intro-spring-data-rest-2.jar` + +# Viewing the running application +To view the running application, visit [http://localhost:8080](http://localhost:8080) in your browser diff --git a/spring-data-rest-2/pom.xml b/spring-data-rest-2/pom.xml new file mode 100644 index 0000000000..cf3265c46d --- /dev/null +++ b/spring-data-rest-2/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + spring-data-rest-2 + 1.0 + spring-data-rest-2 + jar + Intro to Spring Data REST + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-rest + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.data + spring-data-rest-hal-explorer + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-starter-test + test + + + com.h2database + h2 + runtime + + + com.querydsl + querydsl-apt + + + com.querydsl + querydsl-jpa + + + + + ${project.artifactId} + + + com.mysema.maven + maven-apt-plugin + ${maven.version} + + + generate-sources + + process + + + target/generated-sources + com.querydsl.apt.jpa.JPAAnnotationProcessor + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + com.baeldung.books.SpringDataRestApplication + 1.0 + + + \ No newline at end of file diff --git a/spring-data-rest-2/src/main/java/com/baeldung/books/SpringDataRestApplication.java b/spring-data-rest-2/src/main/java/com/baeldung/books/SpringDataRestApplication.java new file mode 100644 index 0000000000..097a6aabd7 --- /dev/null +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/SpringDataRestApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.books; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataRestApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringDataRestApplication.class, args); + } + +} diff --git a/spring-data-rest-2/src/main/java/com/baeldung/books/config/DbConfig.java b/spring-data-rest-2/src/main/java/com/baeldung/books/config/DbConfig.java new file mode 100644 index 0000000000..3fe7516537 --- /dev/null +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/config/DbConfig.java @@ -0,0 +1,65 @@ +package com.baeldung.books.config; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; + +@Configuration +@EnableJpaRepositories(basePackages = "com.baeldung.books.repositories") +public class DbConfig { + + @Autowired + private Environment env; + + @Bean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("driverClassName")); + dataSource.setUrl(env.getProperty("url")); + dataSource.setUsername(env.getProperty("user")); + dataSource.setPassword(env.getProperty("password")); + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "com.baeldung.books.models" }); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + em.setJpaProperties(additionalProperties()); + return em; + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + if (env.getProperty("hibernate.hbm2ddl.auto") != null) { + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + } + if (env.getProperty("hibernate.dialect") != null) { + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + } + if (env.getProperty("hibernate.show_sql") != null) { + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); + } + return hibernateProperties; + } + +} + +@Configuration +@Profile("h2") +@PropertySource("classpath:persistence-h2.properties") +class H2Config { +} diff --git a/spring-data-rest-2/src/main/java/com/baeldung/books/config/RestConfig.java b/spring-data-rest-2/src/main/java/com/baeldung/books/config/RestConfig.java new file mode 100644 index 0000000000..8bcde7c97b --- /dev/null +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/config/RestConfig.java @@ -0,0 +1,21 @@ +package com.baeldung.books.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.core.mapping.ExposureConfiguration; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; +import org.springframework.http.HttpMethod; +import org.springframework.web.servlet.config.annotation.CorsRegistry; + +import com.baeldung.books.models.WebsiteUser; + +@Configuration +public class RestConfig implements RepositoryRestConfigurer { + + @Override + public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration, + CorsRegistry cors) { + ExposureConfiguration config = repositoryRestConfiguration.getExposureConfiguration(); + config.forDomainType(WebsiteUser.class).withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PATCH)); + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java b/spring-data-rest-2/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java similarity index 97% rename from spring-data-rest/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java rename to spring-data-rest-2/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java index bbaf9a2771..3301443203 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/config/ValidatorEventRegister.java @@ -1,30 +1,30 @@ -package com.baeldung.books.config; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; -import org.springframework.validation.Validator; - -@Configuration -public class ValidatorEventRegister implements InitializingBean { - - @Autowired - ValidatingRepositoryEventListener validatingRepositoryEventListener; - - @Autowired - private Map validators; - - @Override - public void afterPropertiesSet() throws Exception { - List events = Arrays.asList("beforeCreate", "afterCreate", "beforeSave", "afterSave", "beforeLinkSave", "afterLinkSave", "beforeDelete", "afterDelete"); - - for (Map.Entry entry : validators.entrySet()) { - events.stream().filter(p -> entry.getKey().startsWith(p)).findFirst().ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue())); - } - } -} +package com.baeldung.books.config; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; +import org.springframework.validation.Validator; + +@Configuration +public class ValidatorEventRegister implements InitializingBean { + + @Autowired + ValidatingRepositoryEventListener validatingRepositoryEventListener; + + @Autowired + private Map validators; + + @Override + public void afterPropertiesSet() throws Exception { + List events = Arrays.asList("beforeCreate", "afterCreate", "beforeSave", "afterSave", "beforeLinkSave", "afterLinkSave", "beforeDelete", "afterDelete"); + + for (Map.Entry entry : validators.entrySet()) { + events.stream().filter(p -> entry.getKey().startsWith(p)).findFirst().ifPresent(p -> validatingRepositoryEventListener.addValidator(p, entry.getValue())); + } + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java b/spring-data-rest-2/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java similarity index 98% rename from spring-data-rest/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java rename to spring-data-rest-2/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java index 4a961e5250..e87176af69 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/exception/handlers/RestResponseEntityExceptionHandler.java @@ -1,26 +1,26 @@ -package com.baeldung.books.exception.handlers; - -import org.springframework.data.rest.core.RepositoryConstraintViolationException; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.ObjectError; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.context.request.WebRequest; -import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; - -import java.util.stream.Collectors; - -@ControllerAdvice -public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { - - @ExceptionHandler({ RepositoryConstraintViolationException.class }) - public ResponseEntity handleAccessDeniedException(Exception ex, WebRequest request) { - RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex; - - String errors = nevEx.getErrors().getAllErrors().stream().map(ObjectError::toString).collect(Collectors.joining("\n")); - return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE); - } - +package com.baeldung.books.exception.handlers; + +import org.springframework.data.rest.core.RepositoryConstraintViolationException; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import java.util.stream.Collectors; + +@ControllerAdvice +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { + + @ExceptionHandler({ RepositoryConstraintViolationException.class }) + public ResponseEntity handleAccessDeniedException(Exception ex, WebRequest request) { + RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex; + + String errors = nevEx.getErrors().getAllErrors().stream().map(ObjectError::toString).collect(Collectors.joining("\n")); + return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE); + } + } \ No newline at end of file diff --git a/spring-data-rest/src/main/java/com/baeldung/books/models/WebsiteUser.java b/spring-data-rest-2/src/main/java/com/baeldung/books/models/WebsiteUser.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/books/models/WebsiteUser.java rename to spring-data-rest-2/src/main/java/com/baeldung/books/models/WebsiteUser.java diff --git a/spring-data-rest/src/main/java/com/baeldung/books/repositories/UserRepository.java b/spring-data-rest-2/src/main/java/com/baeldung/books/repositories/UserRepository.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/books/repositories/UserRepository.java rename to spring-data-rest-2/src/main/java/com/baeldung/books/repositories/UserRepository.java diff --git a/spring-data-rest/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java b/spring-data-rest-2/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java similarity index 96% rename from spring-data-rest/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java rename to spring-data-rest-2/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java index ad7511c2ba..c7b05244df 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java +++ b/spring-data-rest-2/src/main/java/com/baeldung/books/validators/WebsiteUserValidator.java @@ -1,33 +1,33 @@ -package com.baeldung.books.validators; - -import org.springframework.stereotype.Component; -import org.springframework.validation.Errors; -import org.springframework.validation.Validator; - -import com.baeldung.books.models.WebsiteUser; - -@Component("beforeCreateWebsiteUserValidator") -public class WebsiteUserValidator implements Validator { - - @Override - public boolean supports(Class clazz) { - return WebsiteUser.class.equals(clazz); - } - - @Override - public void validate(Object obj, Errors errors) { - - WebsiteUser user = (WebsiteUser) obj; - if (checkInputString(user.getName())) { - errors.rejectValue("name", "name.empty"); - } - - if (checkInputString(user.getEmail())) { - errors.rejectValue("email", "email.empty"); - } - } - - private boolean checkInputString(String input) { - return (input == null || input.trim().length() == 0); - } -} +package com.baeldung.books.validators; + +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; + +import com.baeldung.books.models.WebsiteUser; + +@Component("beforeCreateWebsiteUserValidator") +public class WebsiteUserValidator implements Validator { + + @Override + public boolean supports(Class clazz) { + return WebsiteUser.class.equals(clazz); + } + + @Override + public void validate(Object obj, Errors errors) { + + WebsiteUser user = (WebsiteUser) obj; + if (checkInputString(user.getName())) { + errors.rejectValue("name", "name.empty"); + } + + if (checkInputString(user.getEmail())) { + errors.rejectValue("email", "email.empty"); + } + } + + private boolean checkInputString(String input) { + return (input == null || input.trim().length() == 0); + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/halbrowser/App.java b/spring-data-rest-2/src/main/java/com/baeldung/halbrowser/App.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/halbrowser/App.java rename to spring-data-rest-2/src/main/java/com/baeldung/halbrowser/App.java diff --git a/spring-data-rest/src/main/java/com/baeldung/halbrowser/config/DBLoader.java b/spring-data-rest-2/src/main/java/com/baeldung/halbrowser/config/DBLoader.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/halbrowser/config/DBLoader.java rename to spring-data-rest-2/src/main/java/com/baeldung/halbrowser/config/DBLoader.java diff --git a/spring-data-rest/src/main/java/com/baeldung/halbrowser/config/RestConfig.java b/spring-data-rest-2/src/main/java/com/baeldung/halbrowser/config/RestConfig.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/halbrowser/config/RestConfig.java rename to spring-data-rest-2/src/main/java/com/baeldung/halbrowser/config/RestConfig.java diff --git a/spring-data-rest/src/main/java/com/baeldung/halbrowser/data/BookRepository.java b/spring-data-rest-2/src/main/java/com/baeldung/halbrowser/data/BookRepository.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/halbrowser/data/BookRepository.java rename to spring-data-rest-2/src/main/java/com/baeldung/halbrowser/data/BookRepository.java diff --git a/spring-data-rest/src/main/java/com/baeldung/halbrowser/model/Book.java b/spring-data-rest-2/src/main/java/com/baeldung/halbrowser/model/Book.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/halbrowser/model/Book.java rename to spring-data-rest-2/src/main/java/com/baeldung/halbrowser/model/Book.java diff --git a/spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/Application.java b/spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/Application.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/Application.java rename to spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/Application.java diff --git a/spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/controllers/UserController.java b/spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/controllers/UserController.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/controllers/UserController.java rename to spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/controllers/UserController.java diff --git a/spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/entities/User.java b/spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/entities/User.java similarity index 100% rename from spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/entities/User.java rename to spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/entities/User.java diff --git a/spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java b/spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java similarity index 82% rename from spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java rename to spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java index a20197dc8b..fa727eff6c 100644 --- a/spring-data-rest/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java +++ b/spring-data-rest-2/src/main/java/com/baeldung/springdatawebsupport/application/repositories/UserRepository.java @@ -1,12 +1,11 @@ package com.baeldung.springdatawebsupport.application.repositories; -import com.baeldung.springdatawebsupport.application.entities.User; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Repository; +import com.baeldung.springdatawebsupport.application.entities.User; + @Repository public interface UserRepository extends PagingAndSortingRepository, QuerydslPredicateExecutor { diff --git a/spring-data-rest-2/src/main/resources/application.properties b/spring-data-rest-2/src/main/resources/application.properties new file mode 100644 index 0000000000..06cb22a4fe --- /dev/null +++ b/spring-data-rest-2/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.default=h2 \ No newline at end of file diff --git a/spring-data-rest-2/src/main/resources/logback.xml b/spring-data-rest-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/spring-data-rest-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/spring-data-rest-2/src/main/resources/persistence-h2.properties b/spring-data-rest-2/src/main/resources/persistence-h2.properties new file mode 100644 index 0000000000..d535f9dbe4 --- /dev/null +++ b/spring-data-rest-2/src/main/resources/persistence-h2.properties @@ -0,0 +1,8 @@ +driverClassName=org.h2.Driver +url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 +username=sa +password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file diff --git a/spring-data-rest-2/src/test/java/com/baeldung/SpringContextTest.java b/spring-data-rest-2/src/test/java/com/baeldung/SpringContextTest.java new file mode 100644 index 0000000000..8fa0a4c239 --- /dev/null +++ b/spring-data-rest-2/src/test/java/com/baeldung/SpringContextTest.java @@ -0,0 +1,17 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.books.SpringDataRestApplication; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringDataRestApplication.class) +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} diff --git a/spring-data-rest/src/test/java/com/baeldung/books/validator/SpringDataRestValidatorIntegrationTest.java b/spring-data-rest-2/src/test/java/com/baeldung/books/validator/SpringDataRestValidatorIntegrationTest.java similarity index 100% rename from spring-data-rest/src/test/java/com/baeldung/books/validator/SpringDataRestValidatorIntegrationTest.java rename to spring-data-rest-2/src/test/java/com/baeldung/books/validator/SpringDataRestValidatorIntegrationTest.java diff --git a/spring-data-rest/src/test/java/com/baeldung/springdatawebsupport/application/test/UserControllerIntegrationTest.java b/spring-data-rest-2/src/test/java/com/baeldung/springdatawebsupport/application/test/UserControllerIntegrationTest.java similarity index 100% rename from spring-data-rest/src/test/java/com/baeldung/springdatawebsupport/application/test/UserControllerIntegrationTest.java rename to spring-data-rest-2/src/test/java/com/baeldung/springdatawebsupport/application/test/UserControllerIntegrationTest.java diff --git a/spring-data-rest/README.md b/spring-data-rest/README.md index ab1991b08f..cbdd8986eb 100644 --- a/spring-data-rest/README.md +++ b/spring-data-rest/README.md @@ -4,15 +4,12 @@ This module contains articles about Spring Data REST ### Relevant Articles: - [Introduction to Spring Data REST](https://www.baeldung.com/spring-data-rest-intro) -- [Guide to Spring Data REST Validators](https://www.baeldung.com/spring-data-rest-validators) - [Working with Relationships in Spring Data REST](https://www.baeldung.com/spring-data-rest-relationships) - [AngularJS CRUD Application with Spring Data REST](https://www.baeldung.com/angularjs-crud-with-spring-data-rest) - [Projections and Excerpts in Spring Data REST](https://www.baeldung.com/spring-data-rest-projections-excerpts) - [Spring Data REST Events with @RepositoryEventHandler](https://www.baeldung.com/spring-data-rest-events) - [Customizing HTTP Endpoints in Spring Data REST](https://www.baeldung.com/spring-data-rest-customize-http-endpoints) - [Spring Boot with SQLite](https://www.baeldung.com/spring-boot-sqlite) -- [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support) -- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal) ### The Course The "REST With Spring" Classes: http://bit.ly/restwithspring diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml index 4a22ce92d1..e223c0488e 100644 --- a/spring-data-rest/pom.xml +++ b/spring-data-rest/pom.xml @@ -33,15 +33,6 @@ org.springframework.boot spring-boot-starter-data-jpa - - - org.springframework.data - spring-data-rest-hal-explorer - - - org.springframework.boot - spring-boot-starter-validation - org.springframework.boot spring-boot-autoconfigure diff --git a/spring-data-rest/src/main/java/com/baeldung/books/config/MvcConfig.java b/spring-data-rest/src/main/java/com/baeldung/books/config/MvcConfig.java index 69e984ad06..0065173905 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/config/MvcConfig.java +++ b/spring-data-rest/src/main/java/com/baeldung/books/config/MvcConfig.java @@ -1,5 +1,7 @@ package com.baeldung.books.config; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; @@ -22,6 +24,11 @@ public class MvcConfig implements WebMvcConfigurer { configurer.enable(); } + @Bean + WebServerFactoryCustomizer enableDefaultServlet() { + return (factory) -> factory.setRegisterDefaultServlet(true); + } + @Bean AuthorEventHandler authorEventHandler() { return new AuthorEventHandler(); diff --git a/spring-data-rest/src/main/java/com/baeldung/books/config/RestConfig.java b/spring-data-rest/src/main/java/com/baeldung/books/config/RestConfig.java index 8d9ab5952c..1c03fafd5f 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/config/RestConfig.java +++ b/spring-data-rest/src/main/java/com/baeldung/books/config/RestConfig.java @@ -1,14 +1,12 @@ package com.baeldung.books.config; -import com.baeldung.books.models.WebsiteUser; -import com.baeldung.books.projections.CustomBook; import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; -import org.springframework.data.rest.core.mapping.ExposureConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; -import org.springframework.http.HttpMethod; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import com.baeldung.books.projections.CustomBook; + @Configuration public class RestConfig implements RepositoryRestConfigurer { @@ -16,7 +14,5 @@ public class RestConfig implements RepositoryRestConfigurer { public void configureRepositoryRestConfiguration(RepositoryRestConfiguration repositoryRestConfiguration, CorsRegistry cors) { repositoryRestConfiguration.getProjectionConfiguration().addProjection(CustomBook.class); - ExposureConfiguration config = repositoryRestConfiguration.getExposureConfiguration(); - config.forDomainType(WebsiteUser.class).withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PATCH)); } } diff --git a/spring-data-rest/src/main/java/com/baeldung/books/events/AuthorEventHandler.java b/spring-data-rest/src/main/java/com/baeldung/books/events/AuthorEventHandler.java index b390a529ba..4540fa3b24 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/events/AuthorEventHandler.java +++ b/spring-data-rest/src/main/java/com/baeldung/books/events/AuthorEventHandler.java @@ -1,11 +1,14 @@ package com.baeldung.books.events; -import org.springframework.data.rest.core.annotation.*; +import java.util.logging.Logger; + +import org.springframework.data.rest.core.annotation.HandleAfterCreate; +import org.springframework.data.rest.core.annotation.HandleAfterDelete; +import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.HandleBeforeDelete; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; import com.baeldung.books.models.Author; -import com.baeldung.books.models.Book; - -import java.util.logging.Logger; @RepositoryEventHandler public class AuthorEventHandler { diff --git a/spring-data-rest/src/main/java/com/baeldung/books/events/BookEventHandler.java b/spring-data-rest/src/main/java/com/baeldung/books/events/BookEventHandler.java index 0afb3a6279..fc0d9b4098 100644 --- a/spring-data-rest/src/main/java/com/baeldung/books/events/BookEventHandler.java +++ b/spring-data-rest/src/main/java/com/baeldung/books/events/BookEventHandler.java @@ -2,8 +2,6 @@ package com.baeldung.books.events; import java.util.logging.Logger; -import org.apache.commons.logging.Log; -import org.springframework.data.rest.core.annotation.HandleAfterDelete; import org.springframework.data.rest.core.annotation.HandleBeforeCreate; import org.springframework.data.rest.core.annotation.RepositoryEventHandler;