Moved Building REST API article related code from spring-rest-full to spring-boot-rest module
This commit is contained in:
parent
04b35f1b68
commit
0b988db96d
|
@ -51,7 +51,6 @@
|
|||
<dependency>
|
||||
<groupId>net.sourceforge.htmlunit</groupId>
|
||||
<artifactId>htmlunit</artifactId>
|
||||
<version>${htmlunit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -67,7 +66,6 @@
|
|||
|
||||
<properties>
|
||||
<start-class>com.baeldung.SpringBootRestApplication</start-class>
|
||||
<htmlunit.version>2.32</htmlunit.version>
|
||||
<guava.version>27.0.1-jre</guava.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -1,16 +1,29 @@
|
|||
package com.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);
|
||||
|
||||
void delete(final T entity);
|
||||
|
||||
void deleteById(final long entityId);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.persistence.service.common;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
@ -8,23 +9,54 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.persistence.IOperations;
|
||||
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().findById(id)
|
||||
.get();
|
||||
}
|
||||
|
||||
// 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(PageRequest.of(page, size));
|
||||
}
|
||||
|
||||
// write
|
||||
|
||||
|
||||
@Override
|
||||
public T create(final T entity) {
|
||||
return getDao().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T update(final T entity) {
|
||||
return getDao().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
getDao().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
getDao().deleteById(entityId);
|
||||
}
|
||||
|
||||
protected abstract PagingAndSortingRepository<T, Long> getDao();
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.baeldung.persistence.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
@ -11,6 +13,7 @@ import com.baeldung.persistence.dao.IFooDao;
|
|||
import com.baeldung.persistence.model.Foo;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import com.baeldung.persistence.service.common.AbstractService;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
|
@ -36,5 +39,13 @@ public class FooService extends AbstractService<Foo> implements IFooService {
|
|||
public Page<Foo> findPaginated(Pageable pageable) {
|
||||
return dao.findAll(pageable);
|
||||
}
|
||||
|
||||
// overridden to be secured
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Foo> findAll() {
|
||||
return Lists.newArrayList(getDao().findAll());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,14 +9,16 @@ import org.springframework.context.ApplicationEventPublisher;
|
|||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
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.bind.annotation.RestController;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.baeldung.persistence.model.Foo;
|
||||
|
@ -24,9 +26,11 @@ import com.baeldung.persistence.service.IFooService;
|
|||
import com.baeldung.web.exception.MyResourceNotFoundException;
|
||||
import com.baeldung.web.hateoas.event.PaginatedResultsRetrievedEvent;
|
||||
import com.baeldung.web.hateoas.event.ResourceCreatedEvent;
|
||||
import com.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
|
||||
import com.baeldung.web.util.RestPreconditions;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
@RequestMapping(value = "/auth/foos")
|
||||
public class FooController {
|
||||
|
||||
|
@ -42,10 +46,24 @@ public class FooController {
|
|||
|
||||
// API
|
||||
|
||||
// read - one
|
||||
|
||||
@GetMapping(value = "/{id}")
|
||||
public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) {
|
||||
final Foo resourceById = RestPreconditions.checkFound(service.findOne(id));
|
||||
|
||||
eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response));
|
||||
return resourceById;
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
||||
@RequestMapping(params = { "page", "size" }, method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
@GetMapping
|
||||
public List<Foo> findAll() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
@GetMapping(params = { "page", "size" })
|
||||
public List<Foo> findPaginated(@RequestParam("page") final int page, @RequestParam("size") final int size,
|
||||
final UriComponentsBuilder uriBuilder, final HttpServletResponse response) {
|
||||
final Page<Foo> resultPage = service.findPaginated(page, size);
|
||||
|
@ -59,7 +77,6 @@ public class FooController {
|
|||
}
|
||||
|
||||
@GetMapping("/pageable")
|
||||
@ResponseBody
|
||||
public List<Foo> findPaginatedWithPageable(Pageable pageable, final UriComponentsBuilder uriBuilder,
|
||||
final HttpServletResponse response) {
|
||||
final Page<Foo> resultPage = service.findPaginated(pageable);
|
||||
|
@ -74,9 +91,8 @@ public class FooController {
|
|||
|
||||
// write
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
@ResponseBody
|
||||
public Foo create(@RequestBody final Foo resource, final HttpServletResponse response) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
final Foo foo = service.create(resource);
|
||||
|
@ -86,4 +102,18 @@ public class FooController {
|
|||
|
||||
return foo;
|
||||
}
|
||||
|
||||
@PutMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
RestPreconditions.checkFound(service.findOne(resource.getId()));
|
||||
service.update(resource);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void delete(@PathVariable("id") final Long id) {
|
||||
service.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.web.hateoas.event;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class SingleResourceRetrievedEvent extends ApplicationEvent {
|
||||
private final HttpServletResponse response;
|
||||
|
||||
public SingleResourceRetrievedEvent(final Object source, final HttpServletResponse response) {
|
||||
super(source);
|
||||
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.web.hateoas.listener;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.baeldung.web.hateoas.event.SingleResourceRetrievedEvent;
|
||||
import com.baeldung.web.util.LinkUtil;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.net.HttpHeaders;
|
||||
|
||||
@Component
|
||||
class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener<SingleResourceRetrievedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) {
|
||||
Preconditions.checkNotNull(resourceRetrievedEvent);
|
||||
|
||||
final HttpServletResponse response = resourceRetrievedEvent.getResponse();
|
||||
addLinkHeaderOnSingleResourceRetrieval(response);
|
||||
}
|
||||
|
||||
void addLinkHeaderOnSingleResourceRetrieval(final HttpServletResponse response) {
|
||||
final String requestURL = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toASCIIString();
|
||||
final int positionOfLastSlash = requestURL.lastIndexOf("/");
|
||||
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);
|
||||
|
||||
final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
response.addHeader(HttpHeaders.LINK, linkHeaderValue);
|
||||
}
|
||||
|
||||
}
|
|
@ -16,11 +16,7 @@ public interface IOperations<T extends Serializable> {
|
|||
// write
|
||||
|
||||
T create(final T entity);
|
||||
|
||||
|
||||
T update(final T entity);
|
||||
|
||||
void delete(final T entity);
|
||||
|
||||
void deleteById(final long entityId);
|
||||
|
||||
}
|
||||
|
|
|
@ -40,16 +40,6 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
|
|||
return getDao().save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(final T entity) {
|
||||
getDao().delete(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(final long entityId) {
|
||||
getDao().delete(entityId);
|
||||
}
|
||||
|
||||
protected abstract PagingAndSortingRepository<T, Long> getDao();
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package org.baeldung.persistence.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.IFooDao;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.persistence.service.IFooService;
|
||||
|
@ -11,8 +9,6 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class FooService extends AbstractService<Foo> implements IFooService {
|
||||
|
@ -38,12 +34,4 @@ public class FooService extends AbstractService<Foo> implements IFooService {
|
|||
return dao.retrieveByName(name);
|
||||
}
|
||||
|
||||
// overridden to be secured
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public List<Foo> findAll() {
|
||||
return Lists.newArrayList(getDao().findAll());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -80,20 +80,6 @@ public class FooController {
|
|||
return foo;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void update(@PathVariable("id") final Long id, @RequestBody final Foo resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
RestPreconditions.checkFound(service.findOne(resource.getId()));
|
||||
service.update(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void delete(@PathVariable("id") final Long id) {
|
||||
service.deleteById(id);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.HEAD)
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void head(final HttpServletResponse resp) {
|
||||
|
|
Loading…
Reference in New Issue