pagination work
This commit is contained in:
parent
c0cc59ee48
commit
1af3eb4b1f
@ -3,12 +3,22 @@ package org.baeldung.persistence;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
public interface IOperations<T extends Serializable> {
|
public interface IOperations<T extends Serializable> {
|
||||||
|
|
||||||
|
// read - one
|
||||||
|
|
||||||
T findOne(final long id);
|
T findOne(final long id);
|
||||||
|
|
||||||
|
// read - all
|
||||||
|
|
||||||
List<T> findAll();
|
List<T> findAll();
|
||||||
|
|
||||||
|
Page<T> findPaginated(int page, int size);
|
||||||
|
|
||||||
|
// write
|
||||||
|
|
||||||
T create(final T entity);
|
T create(final T entity);
|
||||||
|
|
||||||
T update(final T entity);
|
T update(final T entity);
|
||||||
|
@ -4,6 +4,8 @@ import java.io.Serializable;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.persistence.IOperations;
|
import org.baeldung.persistence.IOperations;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@ -12,18 +14,29 @@ import com.google.common.collect.Lists;
|
|||||||
@Transactional
|
@Transactional
|
||||||
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
|
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
|
||||||
|
|
||||||
|
// read - one
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public T findOne(final long id) {
|
public T findOne(final long id) {
|
||||||
return getDao().findOne(id);
|
return getDao().findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read - all
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<T> findAll() {
|
public List<T> findAll() {
|
||||||
return Lists.newArrayList(getDao().findAll());
|
return Lists.newArrayList(getDao().findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Page<T> findPaginated(final int page, final int size) {
|
||||||
|
return getDao().findAll(new PageRequest(page, size));
|
||||||
|
}
|
||||||
|
|
||||||
|
// write
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T create(final T entity) {
|
public T create(final T entity) {
|
||||||
return getDao().save(entity);
|
return getDao().save(entity);
|
||||||
|
@ -8,17 +8,21 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
|
|
||||||
import org.baeldung.persistence.model.Foo;
|
import org.baeldung.persistence.model.Foo;
|
||||||
import org.baeldung.persistence.service.IFooService;
|
import org.baeldung.persistence.service.IFooService;
|
||||||
|
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||||
import org.baeldung.web.util.LinkUtil;
|
import org.baeldung.web.util.LinkUtil;
|
||||||
import org.baeldung.web.util.ResourceCreated;
|
import org.baeldung.web.util.ResourceCreated;
|
||||||
|
import org.baeldung.web.util.RestPreconditions;
|
||||||
import org.baeldung.web.util.SingleResourceRetrieved;
|
import org.baeldung.web.util.SingleResourceRetrieved;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
@ -42,28 +46,38 @@ public class FooController {
|
|||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
// read
|
// read - one
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletRequest request, final HttpServletResponse response) {
|
||||||
return service.findOne(id);
|
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
||||||
|
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
|
||||||
|
return resourceById;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read - all
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET)
|
@RequestMapping(method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<Foo> findAll() {
|
public List<Foo> findAll() {
|
||||||
return service.findAll();
|
return service.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
|
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
|
public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||||
final Foo resourceById = Preconditions.checkNotNull(service.findOne(id));
|
|
||||||
|
|
||||||
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
|
final Page<Foo> resultPage = service.findPaginated(page, size);
|
||||||
return resourceById;
|
if (page > resultPage.getTotalPages()) {
|
||||||
|
throw new MyResourceNotFoundException();
|
||||||
}
|
}
|
||||||
|
// eventPublisher.publishEvent(new PaginatedResultsRetrievedEvent<Foo>(Foo.class, uriBuilder, response, page, resultPage.getTotalPages(), size));
|
||||||
|
|
||||||
|
return resultPage.getContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
// discover
|
||||||
|
|
||||||
@RequestMapping(value = "admin", method = RequestMethod.GET)
|
@RequestMapping(value = "admin", method = RequestMethod.GET)
|
||||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||||
|
@ -2,6 +2,7 @@ package org.baeldung.web.error;
|
|||||||
|
|
||||||
import javax.persistence.EntityNotFoundException;
|
import javax.persistence.EntityNotFoundException;
|
||||||
|
|
||||||
|
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||||
import org.hibernate.exception.ConstraintViolationException;
|
import org.hibernate.exception.ConstraintViolationException;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
@ -50,7 +51,7 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
|
|||||||
|
|
||||||
// 404
|
// 404
|
||||||
|
|
||||||
@ExceptionHandler(value = { EntityNotFoundException.class })
|
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
|
||||||
protected ResponseEntity<Object> handleBadRequest(final EntityNotFoundException ex, final WebRequest request) {
|
protected ResponseEntity<Object> handleBadRequest(final EntityNotFoundException ex, final WebRequest request) {
|
||||||
final String bodyOfResponse = "This should be application specific";
|
final String bodyOfResponse = "This should be application specific";
|
||||||
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package org.baeldung.web.exception;
|
||||||
|
|
||||||
|
public final class MyResourceNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public MyResourceNotFoundException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final String message, final Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyResourceNotFoundException(final Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package org.baeldung.web.util;
|
||||||
|
|
||||||
|
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple static methods to be called at the start of your own methods to verify correct arguments and state. If the Precondition fails, an {@link HttpStatus} code is thrown
|
||||||
|
*/
|
||||||
|
public final class RestPreconditions {
|
||||||
|
|
||||||
|
private RestPreconditions() {
|
||||||
|
throw new AssertionError();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if some value was found, otherwise throw exception.
|
||||||
|
*
|
||||||
|
* @param expression
|
||||||
|
* has value true if found, otherwise false
|
||||||
|
* @throws MyResourceNotFoundException
|
||||||
|
* if expression is false, means value not found.
|
||||||
|
*/
|
||||||
|
public static void checkFound(final boolean expression) {
|
||||||
|
if (!expression) {
|
||||||
|
throw new MyResourceNotFoundException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if some value was found, otherwise throw exception.
|
||||||
|
*
|
||||||
|
* @param expression
|
||||||
|
* has value true if found, otherwise false
|
||||||
|
* @throws MyResourceNotFoundException
|
||||||
|
* if expression is false, means value not found.
|
||||||
|
*/
|
||||||
|
public static <T> T checkFound(final T resource) {
|
||||||
|
if (resource == null) {
|
||||||
|
throw new MyResourceNotFoundException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user