Skip to content

Commit 14b65b3

Browse files
committed
moved Rest Pagination article code from spring-rest-full to spring-boot-rest
1 parent 5d6640a commit 14b65b3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1125
-164
lines changed

spring-boot-rest/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Module for the articles that are part of the Spring REST E-book:
22

33
1. [Bootstrap a Web Application with Spring 5](https://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration)
44
2. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring)
5+
3. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)

spring-boot-rest/pom.xml

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
34
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
45
<modelVersion>4.0.0</modelVersion>
56
<groupId>com.baeldung.web</groupId>
@@ -24,14 +25,38 @@
2425
<groupId>com.fasterxml.jackson.dataformat</groupId>
2526
<artifactId>jackson-dataformat-xml</artifactId>
2627
</dependency>
27-
<dependency>
28+
<dependency>
2829
<groupId>org.hibernate</groupId>
2930
<artifactId>hibernate-entitymanager</artifactId>
3031
</dependency>
3132
<dependency>
3233
<groupId>org.springframework</groupId>
3334
<artifactId>spring-jdbc</artifactId>
3435
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.data</groupId>
38+
<artifactId>spring-data-jpa</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.h2database</groupId>
42+
<artifactId>h2</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework</groupId>
46+
<artifactId>spring-tx</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.data</groupId>
50+
<artifactId>spring-data-commons</artifactId>
51+
</dependency>
52+
53+
<!-- util -->
54+
55+
<dependency>
56+
<groupId>com.google.guava</groupId>
57+
<artifactId>guava</artifactId>
58+
<version>${guava.version}</version>
59+
</dependency>
3560

3661
<dependency>
3762
<groupId>org.springframework.boot</groupId>
@@ -58,5 +83,6 @@
5883
<properties>
5984
<start-class>com.baeldung.SpringBootRestApplication</start-class>
6085
<htmlunit.version>2.32</htmlunit.version>
86+
<guava.version>27.0.1-jre</guava.version>
6187
</properties>
6288
</project>

spring-boot-rest/src/main/java/com/baeldung/web/SpringBootRestApplication.java renamed to spring-boot-rest/src/main/java/com/baeldung/SpringBootRestApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.web;
1+
package com.baeldung;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.persistence;
2+
3+
import java.io.Serializable;
4+
5+
import org.springframework.data.domain.Page;
6+
7+
public interface IOperations<T extends Serializable> {
8+
9+
// read - all
10+
11+
Page<T> findPaginated(int page, int size);
12+
13+
// write
14+
15+
T create(final T entity);
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.persistence.dao;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
import com.baeldung.persistence.model.Foo;
6+
7+
public interface IFooDao extends JpaRepository<Foo, Long> {
8+
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.persistence.model;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
11+
@Entity
12+
public class Foo implements Serializable {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
private long id;
17+
18+
@Column(nullable = false)
19+
private String name;
20+
21+
public Foo() {
22+
super();
23+
}
24+
25+
public Foo(final String name) {
26+
super();
27+
28+
this.name = name;
29+
}
30+
31+
// API
32+
33+
public long getId() {
34+
return id;
35+
}
36+
37+
public void setId(final long id) {
38+
this.id = id;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(final String name) {
46+
this.name = name;
47+
}
48+
49+
//
50+
51+
@Override
52+
public int hashCode() {
53+
final int prime = 31;
54+
int result = 1;
55+
result = prime * result + ((name == null) ? 0 : name.hashCode());
56+
return result;
57+
}
58+
59+
@Override
60+
public boolean equals(final Object obj) {
61+
if (this == obj)
62+
return true;
63+
if (obj == null)
64+
return false;
65+
if (getClass() != obj.getClass())
66+
return false;
67+
final Foo other = (Foo) obj;
68+
if (name == null) {
69+
if (other.name != null)
70+
return false;
71+
} else if (!name.equals(other.name))
72+
return false;
73+
return true;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
final StringBuilder builder = new StringBuilder();
79+
builder.append("Foo [name=").append(name).append("]");
80+
return builder.toString();
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.persistence.service;
2+
3+
import org.springframework.data.domain.Page;
4+
import org.springframework.data.domain.Pageable;
5+
6+
import com.baeldung.persistence.IOperations;
7+
import com.baeldung.persistence.model.Foo;
8+
9+
public interface IFooService extends IOperations<Foo> {
10+
11+
Page<Foo> findPaginated(Pageable pageable);
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.persistence.service.common;
2+
3+
import java.io.Serializable;
4+
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.PageRequest;
7+
import org.springframework.data.repository.PagingAndSortingRepository;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import com.baeldung.persistence.IOperations;
11+
12+
@Transactional
13+
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
14+
15+
// read - all
16+
17+
@Override
18+
public Page<T> findPaginated(final int page, final int size) {
19+
return getDao().findAll(PageRequest.of(page, size));
20+
}
21+
22+
// write
23+
24+
@Override
25+
public T create(final T entity) {
26+
return getDao().save(entity);
27+
}
28+
29+
protected abstract PagingAndSortingRepository<T, Long> getDao();
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.persistence.service.impl;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.repository.PagingAndSortingRepository;
7+
import org.springframework.stereotype.Service;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
import com.baeldung.persistence.dao.IFooDao;
11+
import com.baeldung.persistence.model.Foo;
12+
import com.baeldung.persistence.service.IFooService;
13+
import com.baeldung.persistence.service.common.AbstractService;
14+
15+
@Service
16+
@Transactional
17+
public class FooService extends AbstractService<Foo> implements IFooService {
18+
19+
@Autowired
20+
private IFooDao dao;
21+
22+
public FooService() {
23+
super();
24+
}
25+
26+
// API
27+
28+
@Override
29+
protected PagingAndSortingRepository<Foo, Long> getDao() {
30+
return dao;
31+
}
32+
33+
// custom methods
34+
35+
@Override
36+
public Page<Foo> findPaginated(Pageable pageable) {
37+
return dao.findAll(pageable);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.baeldung.spring;
2+
3+
import java.util.Properties;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.core.env.Environment;
13+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
14+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
15+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
16+
import org.springframework.orm.jpa.JpaTransactionManager;
17+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
18+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
19+
import org.springframework.transaction.PlatformTransactionManager;
20+
import org.springframework.transaction.annotation.EnableTransactionManagement;
21+
22+
import com.google.common.base.Preconditions;
23+
24+
@Configuration
25+
@EnableTransactionManagement
26+
@PropertySource({ "classpath:persistence-${envTarget:h2}.properties" })
27+
@ComponentScan({ "com.baeldung.persistence" })
28+
// @ImportResource("classpath*:springDataPersistenceConfig.xml")
29+
@EnableJpaRepositories(basePackages = "com.baeldung.persistence.dao")
30+
public class PersistenceConfig {
31+
32+
@Autowired
33+
private Environment env;
34+
35+
public PersistenceConfig() {
36+
super();
37+
}
38+
39+
@Bean
40+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
41+
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
42+
em.setDataSource(dataSource());
43+
em.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
44+
45+
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
46+
// vendorAdapter.set
47+
em.setJpaVendorAdapter(vendorAdapter);
48+
em.setJpaProperties(additionalProperties());
49+
50+
return em;
51+
}
52+
53+
@Bean
54+
public DataSource dataSource() {
55+
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
56+
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
57+
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
58+
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
59+
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
60+
61+
return dataSource;
62+
}
63+
64+
@Bean
65+
public PlatformTransactionManager transactionManager() {
66+
final JpaTransactionManager transactionManager = new JpaTransactionManager();
67+
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
68+
69+
return transactionManager;
70+
}
71+
72+
@Bean
73+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
74+
return new PersistenceExceptionTranslationPostProcessor();
75+
}
76+
77+
final Properties additionalProperties() {
78+
final Properties hibernateProperties = new Properties();
79+
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
80+
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
81+
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
82+
return hibernateProperties;
83+
}
84+
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.web.servlet.ViewResolver;
8+
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
12+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
13+
14+
@Configuration
15+
@ComponentScan("com.baeldung.web")
16+
@EnableWebMvc
17+
public class WebConfig implements WebMvcConfigurer {
18+
19+
public WebConfig() {
20+
super();
21+
}
22+
23+
@Bean
24+
public ViewResolver viewResolver() {
25+
final InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
26+
viewResolver.setPrefix("/WEB-INF/view/");
27+
viewResolver.setSuffix(".jsp");
28+
return viewResolver;
29+
}
30+
31+
// API
32+
@Override
33+
public void addViewControllers(final ViewControllerRegistry registry) {
34+
registry.addViewController("/graph.html");
35+
registry.addViewController("/homepage.html");
36+
}
37+
38+
@Override
39+
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
40+
configurer.defaultContentType(MediaType.APPLICATION_JSON);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)