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.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public interface IOperations<T extends Serializable> {
|
||||
|
||||
// read - one
|
||||
|
||||
T findOne(final long id);
|
||||
|
||||
// read - all
|
||||
|
||||
List<T> findAll();
|
||||
|
||||
Page<T> findPaginated(int page, int size);
|
||||
|
||||
// write
|
||||
|
||||
T create(final T entity);
|
||||
|
||||
T update(final T entity);
|
||||
|
|
|
@ -4,6 +4,8 @@ import java.io.Serializable;
|
|||
import java.util.List;
|
||||
|
||||
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.transaction.annotation.Transactional;
|
||||
|
||||
|
@ -12,18 +14,29 @@ import com.google.common.collect.Lists;
|
|||
@Transactional
|
||||
public abstract class AbstractService<T extends Serializable> implements IOperations<T> {
|
||||
|
||||
// read - one
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public T findOne(final long id) {
|
||||
return getDao().findOne(id);
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<T> 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
|
||||
public T create(final T entity) {
|
||||
return getDao().save(entity);
|
||||
|
|
|
@ -8,17 +8,21 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import org.baeldung.web.util.LinkUtil;
|
||||
import org.baeldung.web.util.ResourceCreated;
|
||||
import org.baeldung.web.util.RestPreconditions;
|
||||
import org.baeldung.web.util.SingleResourceRetrieved;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
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.ResponseStatus;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
@ -42,29 +46,39 @@ public class FooController {
|
|||
|
||||
// API
|
||||
|
||||
// read
|
||||
// read - one
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||
return service.findOne(id);
|
||||
public Foo findOne(@PathVariable("id") final Long id, final UriComponentsBuilder uriBuilder, final HttpServletRequest request, final HttpServletResponse response) {
|
||||
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
||||
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
|
||||
return resourceById;
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public List<Foo> findAll() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "admin/foo/{id}", method = RequestMethod.GET)
|
||||
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Foo get(@PathVariable("id") final Long id, final HttpServletRequest request, final HttpServletResponse response) {
|
||||
final Foo resourceById = Preconditions.checkNotNull(service.findOne(id));
|
||||
public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size, final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrieved(this, request, response));
|
||||
return resourceById;
|
||||
final Page<Foo> resultPage = service.findPaginated(page, size);
|
||||
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)
|
||||
@ResponseStatus(value = HttpStatus.NO_CONTENT)
|
||||
public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.baeldung.web.error;
|
|||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
|
||||
import org.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
|
@ -50,7 +51,7 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
|
|||
|
||||
// 404
|
||||
|
||||
@ExceptionHandler(value = { EntityNotFoundException.class })
|
||||
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
|
||||
protected ResponseEntity<Object> handleBadRequest(final EntityNotFoundException ex, final WebRequest request) {
|
||||
final String bodyOfResponse = "This should be application specific";
|
||||
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…
Reference in New Issue