From 017f467ba8f5398555a12e2ba543e4d10157d661 Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sat, 2 Nov 2019 16:30:09 +0530 Subject: [PATCH 1/3] [BAEL-17317] - Check article code - https://www.baeldung.com/rest-api-pagination-in-spring --- spring-boot-rest/pom.xml | 4 ++ .../baeldung/modelmapper/model/Subject.java | 37 +++++++++++++++++++ .../repository/SubjectRepository.java | 16 ++++++++ 3 files changed, 57 insertions(+) create mode 100644 spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java create mode 100644 spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java diff --git a/spring-boot-rest/pom.xml b/spring-boot-rest/pom.xml index 598b589ea0..16150d061a 100644 --- a/spring-boot-rest/pom.xml +++ b/spring-boot-rest/pom.xml @@ -44,6 +44,10 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-data-rest + diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java new file mode 100644 index 0000000000..9efa449dfe --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java @@ -0,0 +1,37 @@ +package com.baeldung.modelmapper.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Subject { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @Column(nullable = false) + private String name; + + public Subject() { + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java new file mode 100644 index 0000000000..ddbf3334ec --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java @@ -0,0 +1,16 @@ +package com.baeldung.modelmapper.repository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.core.annotation.RestResource; + +import com.baeldung.modelmapper.model.Subject; + +public interface SubjectRepository extends PagingAndSortingRepository { + + @RestResource(path = "nameContains") + public Page findByNameContaining(@Param("name") String name, Pageable p); + +} \ No newline at end of file From b3ec0955bbbe81f046e5147dc3a754bbc55f0caa Mon Sep 17 00:00:00 2001 From: "amit.pandey" Date: Fri, 27 Dec 2019 13:32:29 +0530 Subject: [PATCH 2/3] rename package name --- .../com/baeldung/modelmapper/service/IUserService.java | 9 --------- .../controller/PostRestController.java | 10 +++++----- .../{modelmapper => springpagination}/dto/PostDto.java | 2 +- .../{modelmapper => springpagination}/dto/UserDto.java | 2 +- .../{modelmapper => springpagination}/model/Post.java | 2 +- .../model/Preference.java | 2 +- .../model/Subject.java | 2 +- .../{modelmapper => springpagination}/model/User.java | 2 +- .../repository/PostRepository.java | 6 +++--- .../repository/SubjectRepository.java | 4 ++-- .../service/IPostService.java | 4 ++-- .../springpagination/service/IUserService.java | 9 +++++++++ .../service/PostService.java | 6 +++--- .../service/UserService.java | 6 +++--- .../PostDtoUnitTest.java | 6 +++--- 15 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IUserService.java rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/controller/PostRestController.java (91%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/dto/PostDto.java (96%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/dto/UserDto.java (80%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/model/Post.java (97%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/model/Preference.java (92%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/model/Subject.java (93%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/model/User.java (94%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/repository/PostRepository.java (82%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/repository/SubjectRepository.java (83%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/service/IPostService.java (71%) create mode 100644 spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IUserService.java rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/service/PostService.java (87%) rename spring-boot-rest/src/main/java/com/baeldung/{modelmapper => springpagination}/service/UserService.java (76%) rename spring-boot-rest/src/test/java/com/baeldung/{modelmapper => springpagination}/PostDtoUnitTest.java (89%) diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IUserService.java b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IUserService.java deleted file mode 100644 index 79934114c1..0000000000 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IUserService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.modelmapper.service; - -import com.baeldung.modelmapper.model.User; - -public interface IUserService { - - User getCurrentUser(); - -} diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java similarity index 91% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java index e2def62466..c52960b365 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/controller/PostRestController.java @@ -1,9 +1,9 @@ -package com.baeldung.modelmapper.controller; +package com.baeldung.springpagination.controller; -import com.baeldung.modelmapper.dto.PostDto; -import com.baeldung.modelmapper.model.Post; -import com.baeldung.modelmapper.service.IPostService; -import com.baeldung.modelmapper.service.IUserService; +import com.baeldung.springpagination.dto.PostDto; +import com.baeldung.springpagination.model.Post; +import com.baeldung.springpagination.service.IPostService; +import com.baeldung.springpagination.service.IUserService; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/PostDto.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/PostDto.java similarity index 96% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/PostDto.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/PostDto.java index 6fe2b23888..81cec5ed8c 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/PostDto.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/PostDto.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.dto; +package com.baeldung.springpagination.dto; import java.text.ParseException; import java.text.SimpleDateFormat; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/UserDto.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/UserDto.java similarity index 80% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/UserDto.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/UserDto.java index 23110ecbaa..2e4f667b30 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/dto/UserDto.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/dto/UserDto.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.dto; +package com.baeldung.springpagination.dto; public class UserDto { diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Post.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Post.java similarity index 97% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Post.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Post.java index be65ce34a2..88e9ef67bb 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Post.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Post.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.model; +package com.baeldung.springpagination.model; import java.util.Date; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Preference.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Preference.java similarity index 92% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Preference.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Preference.java index 0ab5b1eddf..44f8f41aff 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Preference.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Preference.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.model; +package com.baeldung.springpagination.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Subject.java similarity index 93% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Subject.java index 9efa449dfe..80598ae07d 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/Subject.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/Subject.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.model; +package com.baeldung.springpagination.model; import javax.persistence.Column; import javax.persistence.Entity; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/User.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/User.java similarity index 94% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/User.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/model/User.java index a458b26f4a..4d33834b28 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/model/User.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/model/User.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.model; +package com.baeldung.springpagination.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/PostRepository.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/PostRepository.java similarity index 82% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/PostRepository.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/PostRepository.java index fc3f5733c3..acdc01df94 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/PostRepository.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/PostRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.repository; +package com.baeldung.springpagination.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -7,8 +7,8 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; -import com.baeldung.modelmapper.model.Post; -import com.baeldung.modelmapper.model.User; +import com.baeldung.springpagination.model.Post; +import com.baeldung.springpagination.model.User; public interface PostRepository extends JpaRepository, PagingAndSortingRepository { diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/SubjectRepository.java similarity index 83% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/SubjectRepository.java index ddbf3334ec..03c66f8d72 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/repository/SubjectRepository.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/repository/SubjectRepository.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.repository; +package com.baeldung.springpagination.repository; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -6,7 +6,7 @@ import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RestResource; -import com.baeldung.modelmapper.model.Subject; +import com.baeldung.springpagination.model.Subject; public interface SubjectRepository extends PagingAndSortingRepository { diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IPostService.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IPostService.java similarity index 71% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IPostService.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IPostService.java index 0182a0da41..d1d9bee2c6 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/IPostService.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IPostService.java @@ -1,8 +1,8 @@ -package com.baeldung.modelmapper.service; +package com.baeldung.springpagination.service; import java.util.List; -import com.baeldung.modelmapper.model.Post; +import com.baeldung.springpagination.model.Post; public interface IPostService { diff --git a/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IUserService.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IUserService.java new file mode 100644 index 0000000000..92dce3ed78 --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/IUserService.java @@ -0,0 +1,9 @@ +package com.baeldung.springpagination.service; + +import com.baeldung.springpagination.model.User; + +public interface IUserService { + + User getCurrentUser(); + +} diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/PostService.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/PostService.java similarity index 87% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/PostService.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/service/PostService.java index 5980c30837..bb4869dd33 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/PostService.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/PostService.java @@ -1,4 +1,4 @@ -package com.baeldung.modelmapper.service; +package com.baeldung.springpagination.service; import java.util.List; @@ -8,8 +8,8 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; -import com.baeldung.modelmapper.model.Post; -import com.baeldung.modelmapper.repository.PostRepository; +import com.baeldung.springpagination.model.Post; +import com.baeldung.springpagination.repository.PostRepository; @Service public class PostService implements IPostService { diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/UserService.java b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/UserService.java similarity index 76% rename from spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/UserService.java rename to spring-boot-rest/src/main/java/com/baeldung/springpagination/service/UserService.java index e445f836a4..0694450f44 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/service/UserService.java +++ b/spring-boot-rest/src/main/java/com/baeldung/springpagination/service/UserService.java @@ -1,9 +1,9 @@ -package com.baeldung.modelmapper.service; +package com.baeldung.springpagination.service; import org.springframework.stereotype.Service; -import com.baeldung.modelmapper.model.Preference; -import com.baeldung.modelmapper.model.User; +import com.baeldung.springpagination.model.Preference; +import com.baeldung.springpagination.model.User; @Service public class UserService implements IUserService { diff --git a/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java b/spring-boot-rest/src/test/java/com/baeldung/springpagination/PostDtoUnitTest.java similarity index 89% rename from spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java rename to spring-boot-rest/src/test/java/com/baeldung/springpagination/PostDtoUnitTest.java index 6e94cc4289..948247e166 100644 --- a/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java +++ b/spring-boot-rest/src/test/java/com/baeldung/springpagination/PostDtoUnitTest.java @@ -1,12 +1,12 @@ -package com.baeldung.modelmapper; +package com.baeldung.springpagination; import static org.junit.Assert.assertEquals; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import org.junit.Test; import org.modelmapper.ModelMapper; -import com.baeldung.modelmapper.dto.PostDto; -import com.baeldung.modelmapper.model.Post; +import com.baeldung.springpagination.dto.PostDto; +import com.baeldung.springpagination.model.Post; public class PostDtoUnitTest { From 6a67c8c71924b35e5bd249f5660c0581609ac828 Mon Sep 17 00:00:00 2001 From: "amit.pandey" Date: Fri, 27 Dec 2019 13:36:36 +0530 Subject: [PATCH 3/3] rename package name --- .../main/java/com/baeldung/spring/PersistenceConfig.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-rest/src/main/java/com/baeldung/spring/PersistenceConfig.java b/spring-boot-rest/src/main/java/com/baeldung/spring/PersistenceConfig.java index 2e967751ad..8887510eca 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/spring/PersistenceConfig.java +++ b/spring-boot-rest/src/main/java/com/baeldung/spring/PersistenceConfig.java @@ -24,8 +24,8 @@ import com.google.common.base.Preconditions; @Configuration @EnableTransactionManagement @PropertySource({ "classpath:persistence-${envTarget:h2}.properties" }) -@ComponentScan(basePackages = { "com.baeldung.persistence", "com.baeldung.modelmapper" }) -@EnableJpaRepositories(basePackages = {"com.baeldung.persistence.dao", "com.baeldung.modelmapper.repository"}) +@ComponentScan(basePackages = { "com.baeldung.persistence", "com.baeldung.springpagination" }) +@EnableJpaRepositories(basePackages = {"com.baeldung.persistence.dao", "com.baeldung.springpagination.repository"}) public class PersistenceConfig { @Autowired @@ -39,7 +39,7 @@ public class PersistenceConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "com.baeldung.persistence.model", "com.baeldung.modelmapper.model" }); + em.setPackagesToScan(new String[] { "com.baeldung.persistence.model", "com.baeldung.springpagination.model" }); final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); // vendorAdapter.set