From: Gustavo Martin Morcuende Date: Sun, 3 Jul 2016 18:09:59 +0000 (+0200) Subject: Spring JPA: using ServiceBasedRestController from resthub project X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=db0b707f126abb04d412ba7835c6c989ddaae894;p=JavaForFun Spring JPA: using ServiceBasedRestController from resthub project --- diff --git a/SpringJava/JPA/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java b/SpringJava/JPA/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java new file mode 100644 index 0000000..d3c1849 --- /dev/null +++ b/SpringJava/JPA/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java @@ -0,0 +1,23 @@ +package de.spring.example.rest.controllers; + +import javax.inject.Inject; + +import org.resthub.web.controller.ServiceBasedRestController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import de.spring.example.persistence.domain.AdDescription; +import de.spring.example.services.AdDescriptionService; + +@RestController +@RequestMapping("/ad-descriptions/") +public class AdDescriptionController extends ServiceBasedRestController { + + @Override + @Inject + public void setService(AdDescriptionService adDescriptionService) { + this.service = adDescriptionService; + } + + // I do not have to do anything here because all I need is implemented by ServiceBasedRestController :) +} diff --git a/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java b/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java new file mode 100644 index 0000000..8e2f798 --- /dev/null +++ b/SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java @@ -0,0 +1,9 @@ +package de.spring.example.services; + +import org.resthub.common.service.CrudService; + +import de.spring.example.persistence.domain.AdDescription; + +public interface AdDescriptionService extends CrudService { + +} diff --git a/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java b/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java new file mode 100644 index 0000000..ad08ede --- /dev/null +++ b/SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java @@ -0,0 +1,27 @@ +package de.spring.example.services.impl; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.resthub.common.service.CrudServiceImpl; + +import de.spring.example.persistence.domain.AdDescription; +import de.spring.example.persistence.repository.AdDescriptionRepository; +import de.spring.example.services.AdDescriptionService; + +@Named("adDescriptionService") +public class AdDescriptionServiceImpl + extends CrudServiceImpl + implements AdDescriptionService { + + @Override + @Inject + public void setRepository(AdDescriptionRepository repository) { + this.repository = repository; + } + + // Extending CrudServiceImpl when we need some business logic. Otherwise we would be using + // the JPA repositories and nothing else :) + + // In this case there is any business logic, but this is just an example. +} diff --git a/SpringJava/JPA/src/main/java/org/resthub/common/service/CrudServiceImpl.java b/SpringJava/JPA/src/main/java/org/resthub/common/service/CrudServiceImpl.java new file mode 100644 index 0000000..b623bf3 --- /dev/null +++ b/SpringJava/JPA/src/main/java/org/resthub/common/service/CrudServiceImpl.java @@ -0,0 +1,138 @@ +package org.resthub.common.service; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; + +import java.io.Serializable; +import java.util.Set; + +/** + * CRUD service that uses a {@link org.springframework.data.repository.PagingAndSortingRepository} Spring Data repository implementation + * + * You should extend it and inject your Repository bean by overriding {@link #setRepository(org.springframework.data.repository.PagingAndSortingRepository)} + * + * @param Your resource class to manage, usually an entity class + * @param Resource id type, usually Long or String + * @param The repository class + */ +@Transactional(readOnly = true) +public class CrudServiceImpl> implements + CrudService { + + protected R repository; + + /** + * @param repository the repository to set + */ + public void setRepository(R repository) { + this.repository = repository; + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public T create(T resource) { + Assert.notNull(resource, "Resource can't be null"); + return repository.save(resource); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public T update(T resource) { + Assert.notNull(resource, "Resource can't be null"); + return repository.save(resource); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void delete(T resource) { + Assert.notNull(resource, "Resource can't be null"); + repository.delete(resource); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void delete(ID id) { + Assert.notNull(id, "Resource ID can't be null"); + repository.delete(id); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void deleteAll() { + repository.deleteAll(); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void deleteAllWithCascade() { + Iterable list = repository.findAll(); + for (T entity : list) { + repository.delete(entity); + } + } + + /** + * {@inheritDoc} + */ + @Override + public T findById(ID id) { + Assert.notNull(id, "Resource ID can't be null"); + return repository.findOne(id); + } + + /** + * {@inheritDoc} + */ + @Override + public Iterable findByIds(Set ids) { + Assert.notNull(ids, "Resource ids can't be null"); + return repository.findAll(ids); + } + + /** + * {@inheritDoc} + */ + @Override + public Iterable findAll() { + return repository.findAll(); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public Page findAll(Pageable pageRequest) { + Assert.notNull(pageRequest, "page request can't be null"); + return repository.findAll(pageRequest); + } + + /** + * {@inheritDoc} + */ + @Override + public Long count() { + return repository.count(); + } +} diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml index 2a7f621..a74056a 100644 --- a/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml +++ b/SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml @@ -15,7 +15,7 @@ - + diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml b/SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml index d6a3185..16a44fa 100644 --- a/SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml +++ b/SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml @@ -1,3 +1,4 @@ + - +