Spring JPA: using ServiceBasedRestController from resthub project
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 18:09:59 +0000 (20:09 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 3 Jul 2016 18:09:59 +0000 (20:09 +0200)
SpringJava/JPA/src/main/java/de/spring/example/rest/controllers/AdDescriptionController.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/example/services/AdDescriptionService.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/example/services/impl/AdDescriptionServiceImpl.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/org/resthub/common/service/CrudServiceImpl.java [new file with mode: 0644]
SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml
SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml
SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml [new file with mode: 0644]

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 (file)
index 0000000..d3c1849
--- /dev/null
@@ -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<AdDescription, Long, AdDescriptionService> {
+       
+       @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 (file)
index 0000000..8e2f798
--- /dev/null
@@ -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<AdDescription, Long> {
+
+}
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 (file)
index 0000000..ad08ede
--- /dev/null
@@ -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<AdDescription, Long, AdDescriptionRepository>
+       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 (file)
index 0000000..b623bf3
--- /dev/null
@@ -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 <T> Your resource class to manage, usually an entity class
+ * @param <ID> Resource id type, usually Long or String
+ * @param <R> The repository class
+ */
+@Transactional(readOnly = true)
+public class CrudServiceImpl<T, ID extends Serializable, R extends PagingAndSortingRepository<T, ID>> implements
+        CrudService<T, ID> {
+
+    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<T> 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<T> findByIds(Set<ID> ids) {
+        Assert.notNull(ids, "Resource ids can't be null");
+        return repository.findAll(ids);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Iterable<T> findAll() {
+        return repository.findAll();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    @Transactional
+    public Page<T> findAll(Pageable pageRequest) {
+        Assert.notNull(pageRequest, "page request can't be null");
+        return repository.findAll(pageRequest);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Long count() {
+        return repository.count();
+    }
+}
index 2a7f621..a74056a 100644 (file)
@@ -15,7 +15,7 @@
 \r
        <context:annotation-config />\r
 \r
-    <context:component-scan base-package="de.spring.example.persistence, org.resthub" />\r
+    <context:component-scan base-package="de.spring.example.persistence" />\r
     \r
     <context:property-placeholder location="classpath:jpa.properties" />\r
     \r
index d6a3185..16a44fa 100644 (file)
@@ -1,3 +1,4 @@
+<?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:mvc="http://www.springframework.org/schema/mvc"
@@ -25,7 +26,7 @@
    
        <context:annotation-config />
    
-       <context:component-scan base-package="de.spring.example.rest"/>
+       <context:component-scan base-package="de.spring.example.rest, org.resthub"/>
        
        <!--
                Required beans for generating XML responses from Java objects using JAXB annotations
diff --git a/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml b/SpringJava/JPA/src/main/resources/spring-configuration/spring-configuration.xml
new file mode 100644 (file)
index 0000000..94eab34
--- /dev/null
@@ -0,0 +1,13 @@
+<?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:context="http://www.springframework.org/schema/context"
+       xsi:schemaLocation="
+               http://www.springframework.org/schema/beans
+               http://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/context
+        http://www.springframework.org/schema/context/spring-context.xsd">
+        
+       <context:component-scan base-package="de.spring.example.services"/>
+       
+</beans>
\ No newline at end of file