Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / PaginationDiscoverabilityTest.java
Created January 16, 2012 21:47
Pagination with REST - testing Discoverability - next, previous
@Test
public void whenFirstPageOfResourcesAreRetrieved_thenSecondPageIsNext(){
Response response = givenAuth().get( paths.getFooURL()+"?page=0&size=10" );
String uriToNextPage = extractURIByRel( response.getHeader( LINK ), REL_NEXT );
assertEquals( paths.getFooURL()+"?page=1&size=10", uriToNextPage );
}
@Test
public void whenFirstPageOfResourcesAreRetrieved_thenNoPreviousPage(){
Response response = givenAuth().get( paths.getFooURL()+"?page=0&size=10" );
@eugenp
eugenp / PaginationDiscoverabilityListener.java
Last active March 24, 2022 13:07
Pagination with REST - the Discoverability Listener full
package org.baeldung.web.hateoas;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import com.google.common.base.Preconditions;
@eugenp
eugenp / PaginationDiscoverability.java
Created January 15, 2012 23:54
Pagination with REST - the Discoverability Listener
void addLinkHeaderOnPagedResourceRetrieval(
UriComponentsBuilder uriBuilder, HttpServletResponse response,
Class clazz, int page, int totalPages, int size ){
String resourceName = clazz.getSimpleName().toString().toLowerCase();
uriBuilder.path( "/admin/" + resourceName );
StringBuilder linkHeader = new StringBuilder();
if( hasNextPage( page, totalPages ) ){
String uriNextPage = constructNextPageUri( uriBuilder, page, size );
@eugenp
eugenp / PaginationController.java
Created January 15, 2012 23:29
Pagination with REST - the Controller
@RequestMapping( value = "admin/foo",params = { "page", "size" },method = GET )
@ResponseBody
public List< Foo > findPaginated(
@RequestParam( "page" ) int page, @RequestParam( "size" ) int size,
UriComponentsBuilder uriBuilder, HttpServletResponse response ){
Page< Foo > resultPage = service.findPaginated( page, size );
if( page > resultPage.getTotalPages() ){
throw new ResourceNotFoundException();
}
@eugenp
eugenp / LayerTransactionPropagations.java
Created December 26, 2011 13:41
Transaction configuration with Spring 3.1 - the transaction propagations
@Transactional( propagation = Propagation.REQUIRES_NEW )
public class Controller{
...
}
@Transactional( propagation = Propagation.MANDATORY )
public class Service{
...
}
@Transactional( propagation = Propagation.MANDATORY )
public class DAO{
@eugenp
eugenp / transactionAOPConfig.xml
Created December 25, 2011 22:03
Transaction configuration with Spring 3.1 - the AOP config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
@eugenp
eugenp / persistenceJpaConfig.xml
Created December 23, 2011 22:41
Transaction configuration with Spring 3.1 - the transaction Configuration in XML
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
@eugenp
eugenp / PersistenceJPAConfig.java
Created December 23, 2011 22:35
Transaction configuration with Spring 3.1 - the transaction Configuration
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
...
}
@Bean
@eugenp
eugenp / TransactionalIntegrationTest.java
Created December 23, 2011 22:14
Transaction configuration with Spring 3.1 - transactional test
@TransactionConfiguration( defaultRollback = true )
@Transactional
public class TransactionalIntegrationTest{
...
}
@eugenp
eugenp / pom.xml
Created December 21, 2011 22:19
The Persistence Layer with Spring Data JPA - the Maven pom
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>