JPA + Spring + REST
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 1 Jul 2016 00:37:29 +0000 (02:37 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 1 Jul 2016 00:37:29 +0000 (02:37 +0200)
14 files changed:
SpringJava/JPA/pom.xml
SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/persistence/example/domain/Ad.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/persistence/example/repository/AdRepository.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/de/spring/rest/controllers/AdController.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/org/resthub/common/exception/NotFoundException.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/org/resthub/web/controller/RepositoryBasedRestController.java [new file with mode: 0644]
SpringJava/JPA/src/main/java/org/resthub/web/controller/RestController.java [new file with mode: 0644]
SpringJava/JPA/src/main/resources/jpa.properties
SpringJava/JPA/src/main/resources/log4j2.xml
SpringJava/JPA/src/main/resources/spring-configuration/jpa-configuration.xml
SpringJava/JPA/src/main/resources/spring-configuration/mvc/rest/rest-config.xml [new file with mode: 0644]
SpringJava/JPA/src/main/webapp/WEB-INF/web.xml [new file with mode: 0644]
SpringJava/JPA/src/test/java/de/spring/persistence/example/domain/AdTest.java [new file with mode: 0644]

index d91a5b4..c8a763a 100644 (file)
@@ -3,23 +3,24 @@
        <modelVersion>4.0.0</modelVersion>
        <groupId>de.spring.jpa</groupId>
        <artifactId>spring-jpa</artifactId>
+       <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>spring-jpa</name>
-       <url>http://gumartinm.name</url>
+       <url>https://gumartinm.name/</url>
        <description>JPA Spring Framework</description>
        <organization>
                <name>Gustavo Martin Morcuende</name>
-               <url>http://www.gumartinm.name</url>
+               <url>https://gumartinm.name/</url>
        </organization>
        <scm>
-               <developerConnection>scm:git:http://git.gumartinm.name/SpringWebServicesForFun</developerConnection>
-               <url>http://git.gumartinm.name/SpringWebServicesForFun</url>
+               <developerConnection>scm:git:http://git.gumartinm.name/Spring/JPA</developerConnection>
+               <url>http://git.gumartinm.name/Spring/JPA</url>
        </scm>
 
        <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-               <spring.version>4.2.4.RELEASE</spring.version>
+               <spring.version>4.3.0.RELEASE</spring.version>
        </properties>
 
        <profiles>
@@ -42,7 +43,7 @@
                <dependency>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-slf4j-impl</artifactId>
-                       <version>2.5</version>
+                       <version>2.6.1</version>
                </dependency>
                <!--
                        2/3 Required dependency for log4j 2 with slf4j: log4j 2 maven plugin 
@@ -51,7 +52,7 @@
                <dependency>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-core</artifactId>
-                       <version>2.5</version>
+                       <version>2.6.1</version>
                </dependency>
                <!--
                        3/3 Required dependency for getting rid of commons logging. This is 
@@ -63,7 +64,7 @@
                <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>jcl-over-slf4j</artifactId>
-                       <version>1.7.13</version>
+                       <version>1.7.21</version>
                </dependency>
                
                <dependency>
                                </exclusion>
                        </exclusions>
                </dependency>
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-webmvc</artifactId>
+                       <version>${spring.version}</version>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>commons-logging</groupId>
+                                       <artifactId>commons-logging</artifactId>
+                               </exclusion>
+                       </exclusions>
+               </dependency>
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-oxm</artifactId>
+                       <version>${spring.version}</version>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>commons-logging</groupId>
+                                       <artifactId>commons-logging</artifactId>
+                               </exclusion>
+                       </exclusions>
+               </dependency>
                
                <dependency>
                        <groupId>cglib</groupId>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
+               <exclusions>
+                               <exclusion>
+                                       <groupId>commons-logging</groupId>
+                                       <artifactId>commons-logging</artifactId>
+                               </exclusion>
+                       </exclusions>
                </dependency>
                
                <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-jpa</artifactId>
-                       <version>1.9.2.RELEASE</version>
+                       <version>1.10.2.RELEASE</version>
                        <exclusions>
                                <exclusion>
                                        <groupId>org.springframework</groupId>
                                        <groupId>org.springframework</groupId>
                                        <artifactId>spring-context</artifactId>
                                </exclusion>
+                               <exclusion>
+                                       <groupId>commons-logging</groupId>
+                                       <artifactId>commons-logging</artifactId>
+                               </exclusion>
                        </exclusions>
                </dependency>
 
                <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
-                       <version>5.0.7.Final</version>
+                       <version>5.1.0.Final</version>
+               </dependency>
+
+
+               <!-- Required by spring-webmvc -->
+               <dependency>
+                       <groupId>javax.servlet</groupId>
+                       <artifactId>javax.servlet-api</artifactId>
+                       <version>4.0.0-b01</version>
+                       <scope>provided</scope>
                </dependency>
 
+               <!--
+                       Jackson JSON Processor, required by spring-webmvc. See messageConverters
+                       in rest-config.xml
+               -->
+               <dependency>
+                       <groupId>com.fasterxml.jackson.core</groupId>
+                       <artifactId>jackson-databind</artifactId>
+                       <version>2.6.4</version>
+               </dependency>
+
+
+               <!--
+                       Required by spring-context for using JSR-303. See LocalValidatorFactoryBean 
+                       in rest-config.xml
+               -->
+               <dependency>
+                       <groupId>javax.validation</groupId>
+                       <artifactId>validation-api</artifactId>
+                       <version>1.1.0.Final</version>
+               </dependency>
+               <dependency>
+                       <groupId>org.hibernate</groupId>
+                       <artifactId>hibernate-validator</artifactId>
+                       <version>5.2.2.Final</version>
+               </dependency>
 
                <!-- Unitary and integration tests -->
                <dependency>
                        <artifactId>spring-test</artifactId>
                        <version>${spring.version}</version>
                        <scope>test</scope>
+                                                       <exclusions>
+                                       <exclusion>
+                                               <groupId>commons-logging</groupId>
+                                               <artifactId>commons-logging</artifactId>
+                                       </exclusion>
+                               </exclusions>
                </dependency>
                <dependency>
                        <groupId>org.mockito</groupId>
                                <artifactId>maven-failsafe-plugin</artifactId>
                                <version>2.18.1</version>
                        </plugin>
-
                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                        </includes>
                                </configuration>
                        </plugin>
+                       <plugin>
+                               <groupId>org.apache.maven.plugins</groupId>
+                               <artifactId>maven-war-plugin</artifactId>
+                               <version>2.6</version>
+                               <configuration>
+                                       <webResources>
+                                               <resource>
+                                                       <filtering>true</filtering>
+                                                       <directory>src/main/webapp</directory>
+                                                       <includes>
+                                                               <include>WEB-INF/web.xml</include>
+                                                       </includes>
+                                               </resource>
+                                       </webResources>
+                               </configuration>
+                       </plugin>
                </plugins>
        </build>
 </project>
diff --git a/SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java b/SpringJava/JPA/src/main/java/de/spring/persistence/converters/LocalDateTimeAttributeConverter.java
new file mode 100644 (file)
index 0000000..2d2259c
--- /dev/null
@@ -0,0 +1,22 @@
+package de.spring.persistence.converters;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+@Converter(autoApply = true)
+public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
+
+       @Override
+       public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
+               return (localDateTime == null ? null : Timestamp.valueOf(localDateTime));
+       }
+
+       @Override
+       public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
+               return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
+       }
+
+}
diff --git a/SpringJava/JPA/src/main/java/de/spring/persistence/example/domain/Ad.java b/SpringJava/JPA/src/main/java/de/spring/persistence/example/domain/Ad.java
new file mode 100644 (file)
index 0000000..cc9fd31
--- /dev/null
@@ -0,0 +1,35 @@
+package de.spring.persistence.example.domain;
+
+import java.io.Serializable;
+import java.time.LocalDateTime;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(schema = "mybatis_example")
+public class Ad implements Serializable {
+
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       @Column(name = "id", updatable = false, nullable = false)
+       private Long id;
+       
+       private Long companyId;
+       
+       @Column
+       private Long companyCategId;
+       
+       @Column
+       private String adMobileImage;
+       
+       @Column(nullable = false)
+       private LocalDateTime createdAt;
+       
+       @Column(nullable = false)
+       private LocalDateTime updatedAt;
+}
diff --git a/SpringJava/JPA/src/main/java/de/spring/persistence/example/repository/AdRepository.java b/SpringJava/JPA/src/main/java/de/spring/persistence/example/repository/AdRepository.java
new file mode 100644 (file)
index 0000000..8230c43
--- /dev/null
@@ -0,0 +1,9 @@
+package de.spring.persistence.example.repository;
+
+import org.springframework.data.repository.PagingAndSortingRepository;
+
+import de.spring.persistence.example.domain.Ad;
+
+public interface AdRepository extends PagingAndSortingRepository<Ad, Long> {
+
+}
diff --git a/SpringJava/JPA/src/main/java/de/spring/rest/controllers/AdController.java b/SpringJava/JPA/src/main/java/de/spring/rest/controllers/AdController.java
new file mode 100644 (file)
index 0000000..16a61f9
--- /dev/null
@@ -0,0 +1,15 @@
+package de.spring.rest.controllers;
+
+import org.resthub.web.controller.RepositoryBasedRestController;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import de.spring.persistence.example.domain.Ad;
+import de.spring.persistence.example.repository.AdRepository;
+
+@RestController
+@RequestMapping("/ads/")
+public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepository>{
+
+       // I do not have to do anything here because all I need is implemented by RepositoryBasedRestController :)
+}
diff --git a/SpringJava/JPA/src/main/java/org/resthub/common/exception/NotFoundException.java b/SpringJava/JPA/src/main/java/org/resthub/common/exception/NotFoundException.java
new file mode 100644 (file)
index 0000000..7135761
--- /dev/null
@@ -0,0 +1,24 @@
+package org.resthub.common.exception;
+
+/**
+ * Exception thrown when not result was found (for example findById with null return value)
+ */
+public class NotFoundException extends RuntimeException {
+    
+    public NotFoundException() {
+        super();
+    }
+
+    public NotFoundException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+
+    public NotFoundException(final String message) {
+        super(message);
+    }
+
+    public NotFoundException(final Throwable cause) {
+        super(cause);
+    }
+    
+}
diff --git a/SpringJava/JPA/src/main/java/org/resthub/web/controller/RepositoryBasedRestController.java b/SpringJava/JPA/src/main/java/org/resthub/web/controller/RepositoryBasedRestController.java
new file mode 100644 (file)
index 0000000..2fd4c10
--- /dev/null
@@ -0,0 +1,156 @@
+package org.resthub.web.controller;
+
+import org.resthub.common.exception.NotFoundException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.repository.PagingAndSortingRepository;
+import org.springframework.util.Assert;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestParam;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * Abstract REST controller using a repository implementation
+ * <p/>
+ * <p>
+ * You should extend this class when you want to use a 2 layers pattern : Repository and Controller. This is the default
+ * controller implementation to use if you have no service (also called business) layer. You will be able to transform
+ * it to a ServiceBasedRestController later easily if needed.
+ * </p>
+ *
+ * <p>Default implementation uses "id" field (usually a Long) in order to identify resources in web request.
+ * If your want to identity resources by a slug (human readable identifier), your should override findById() method with for example :
+ *
+ * <pre>
+ * <code>
+   {@literal @}Override
+   public Sample findById({@literal @}PathVariable String id) {
+        Sample sample = this.repository.findByName(id);
+        if (sample == null) {
+            throw new NotFoundException();
+        }
+        return sample;
+   }
+   </code>
+ * </pre>
+ *
+ *
+ * @param <T>  Your resource class to manage, maybe an entity or DTO class
+ * @param <ID> Resource id type, usually Long or String
+ * @param <R>  The repository class
+ * @see ServiceBasedRestController
+ */
+public abstract class RepositoryBasedRestController<T, ID extends Serializable, R extends PagingAndSortingRepository>
+        implements RestController<T, ID> {
+
+    protected R repository;
+
+    protected Logger logger = LoggerFactory.getLogger(RepositoryBasedRestController.class);
+
+    /**
+     * You should override this setter in order to inject your repository with @Inject annotation
+     *
+     * @param repository The repository to be injected
+     */
+    public void setRepository(R repository) {
+        this.repository = repository;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public T create(@RequestBody T resource) {
+        return (T)this.repository.save(resource);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public T update(@PathVariable ID id, @RequestBody T resource) {
+        Assert.notNull(id, "id cannot be null");
+
+        T retrievedResource = this.findById(id);
+        if (retrievedResource == null) {
+            throw new NotFoundException();
+        }
+
+        return (T)this.repository.save(resource);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Iterable<T> findAll() {
+        return repository.findAll();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Page<T> findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
+                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
+                                 @RequestParam(value = "direction", required = false, defaultValue = "") String direction,
+                                 @RequestParam(value = "properties", required = false) String properties) {
+        Assert.isTrue(page > 0, "Page index must be greater than 0");
+        Assert.isTrue(direction.isEmpty() || direction.equalsIgnoreCase(Sort.Direction.ASC.toString()) || direction.equalsIgnoreCase(Sort.Direction.DESC.toString()), "Direction should be ASC or DESC");
+        if(direction.isEmpty()) {
+            return this.repository.findAll(new PageRequest(page - 1, size));
+        } else {
+            Assert.notNull(properties);
+            return this.repository.findAll(new PageRequest(page - 1, size, new Sort(Sort.Direction.fromString(direction.toUpperCase()), properties.split(","))));
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public T findById(@PathVariable ID id) {
+        T entity = (T)this.repository.findOne(id);
+        if (entity == null) {
+            throw new NotFoundException();
+        }
+
+        return entity;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public Iterable<T> findByIds(@RequestParam(value="ids[]") Set<ID> ids){
+        Assert.notNull(ids, "ids list cannot be null");
+        return this.repository.findAll(ids);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void delete() {
+        Iterable<T> list = repository.findAll();
+        for (T entity : list) {
+            repository.delete(entity);
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void delete(@PathVariable ID id) {
+        T resource = this.findById(id);
+        this.repository.delete(resource);
+    }
+
+}
diff --git a/SpringJava/JPA/src/main/java/org/resthub/web/controller/RestController.java b/SpringJava/JPA/src/main/java/org/resthub/web/controller/RestController.java
new file mode 100644 (file)
index 0000000..9aa9a26
--- /dev/null
@@ -0,0 +1,119 @@
+package org.resthub.web.controller;
+
+import org.resthub.common.exception.NotFoundException;
+import org.springframework.data.domain.Page;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.*;
+
+import java.io.Serializable;
+import java.util.Set;
+
+/**
+ * REST controller interface
+ *
+ * @param <T>  Your resource POJO to manage, maybe an entity or DTO class
+ * @param <ID> Primary resource identifier at webservice level, usually Long or String
+ */
+public interface RestController<T, ID extends Serializable> {
+
+    /**
+     * Create a new resource<br />
+     * REST webservice published : POST /
+     *
+     * @param resource The resource to create
+     * @return CREATED http status code if the request has been correctly processed, with updated resource enclosed in the body, usually with and additional identifier automatically created by the database
+     */
+    @RequestMapping(method = RequestMethod.POST)
+    @ResponseStatus(HttpStatus.CREATED)
+    @ResponseBody
+    T create(@RequestBody T resource);
+
+    /**
+     * Update an existing resource<br/>
+     * REST webservice published : PUT /{id}
+     *
+     * @param id       The identifier of the resource to update, usually a Long or String identifier. It is explicitely provided in order to handle cases where the identifier could be changed.
+     * @param resource The resource to update
+     * @return OK http status code if the request has been correctly processed, with the updated resource enclosed in the body
+     * @throws NotFoundException
+     */
+    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
+    @ResponseBody
+    T update(@PathVariable ID id, @RequestBody T resource);
+
+    /**
+     * Find all resources, and return the full collection (plain list not paginated)<br/>
+     * REST webservice published : GET /?page=no
+     *
+     * @return OK http status code if the request has been correctly processed, with the list of all resource enclosed in the body.
+     * Be careful, this list should be big since it will return ALL resources. In this case, consider using paginated findAll method instead.
+     */
+    @RequestMapping(method = RequestMethod.GET, params = "page=no")
+    @ResponseBody
+    Iterable<T> findAll();
+
+    /**
+     * Find all resources, and return a paginated and optionaly sorted collection<br/>
+     * REST webservice published : GET /search?page=0&size=20 or GET /search?page=0&size=20&direction=desc&properties=name
+     *
+     * @param page       Page number starting from 0. default to 0
+     * @param size       Number of resources by pages. default to 10
+     * @param direction  Optional sort direction, could be "asc" or "desc"
+     * @param properties Ordered list of comma separeted properies used for sorting resulats. At least one property should be provided if direction is specified
+     * @return OK http status code if the request has been correctly processed, with the a paginated collection of all resource enclosed in the body.
+     */
+    @RequestMapping(method = RequestMethod.GET)
+    @ResponseBody
+    Page<T> findPaginated(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
+                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
+                          @RequestParam(value = "direction", required = false, defaultValue = "ASC") String direction,
+                          @RequestParam(value = "properties", required = false) String properties);
+
+    /**
+     * Find a resource by its identifier<br/>
+     * REST webservice published : GET /{id}
+     *
+     * @param id The identifier of the resouce to find
+     * @return OK http status code if the request has been correctly processed, with resource found enclosed in the body
+     * @throws NotFoundException
+     */
+    @RequestMapping(value = "{id}", method = RequestMethod.GET)
+    @ResponseBody
+    T findById(@PathVariable ID id);
+
+    /**
+     * Find multiple resources by their identifiers<br/>
+     * REST webservice published : GET /?ids[]=
+     * <p/>
+     * example : /?ids[]=1&ids[]=2&ids[]=3
+     *
+     * @param ids List of ids to retrieve
+     * @return OK http status code with list of retrieved resources. Not found resources are ignored:
+     * no Exception thrown. List is empty if no resource found with any of the given ids.
+     */
+    @RequestMapping(method = RequestMethod.GET, params = "ids[]")
+    @ResponseBody
+    Iterable<T> findByIds(@RequestParam(value = "ids[]") Set<ID> ids);
+
+    /**
+     * Delete all resources<br/>
+     * REST webservice published : DELETE /<br/>
+     * Return No Content http status code if the request has been correctly processed
+     */
+    @RequestMapping(method = RequestMethod.DELETE)
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    void delete();
+
+    /**
+     * Delete a resource by its identifier<br />
+     * REST webservice published : DELETE /{id}<br />
+     * Return No Content http status code if the request has been correctly processed
+     *
+     * @param id The identifier of the resource to delete
+     * @throws NotFoundException
+     */
+    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
+    @ResponseStatus(HttpStatus.NO_CONTENT)
+    void delete(@PathVariable ID id);
+
+}
index f0755b8..9f628d3 100644 (file)
@@ -1,2 +1,2 @@
-jpa.dialect=org.hibernate.dialect.DB2Dialect
-jpa.show_sql=true
\ No newline at end of file
+jpa.dialect=org.hibernate.dialect.MySQL5Dialect
+jpa.show_sql=false
\ No newline at end of file
index d684e48..eec10be 100644 (file)
     <Loggers>
     
 
-               <!-- 
-               General logging Spring.
-        -->
-        <Logger name="org.springframework" level="INFO" additivity="false">
-            <AppenderRef ref="STDOUT" />
-        </Logger>
+    <!-- 
+       General logging Spring.
+       -->
+       <Logger name="org.springframework" level="INFO" additivity="false">
+               <AppenderRef ref="STDOUT" />
+       </Logger>
     
+    <!--
+       How to log Hibernate QUERIES by means of LOG4J/SLF4J:
+               
+       See: org.hibernate.engine.jdbc.spi.SqlStatementLogger
+               
+       1. With hibernate.show_sql=false we stop Hibernate logging to STDOUT.
+       2. DEBUG level (see SqlStatementLogger) is the only way of logging QUERIES using LOG4J.
+               SqlStatementLogger uses log.DEBUG and no other level :(
+       3. JVM requires the following system property: -Dorg.jboss.logging.provider=slf4j for using LOG4J.
+    -->
+       <Logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
+               <AppenderRef ref="STDOUT" />
+       </Logger>
 
-               <!-- 
-                       Anything else will be using TRACE logging level.
-                -->        
-        <Root level="INFO">
-            <AppenderRef ref="STDOUT"/>
-        </Root>
+       <!-- 
+               Anything else will be using TRACE logging level.
+       -->        
+    <Root level="INFO">
+       <AppenderRef ref="STDOUT"/>
+    </Root>
         
     </Loggers>
 </Configuration>
index 4381156..286d067 100644 (file)
@@ -15,7 +15,7 @@
 \r
        <context:annotation-config />\r
 \r
-    <context:component-scan base-package="de.spring.jpa.repository.impl" />\r
+    <context:component-scan base-package="de.spring.persistence, org.resthub" />\r
     \r
     <context:property-placeholder location="classpath:jpa.properties" />\r
     \r
                                <property name="showSql" value="${jpa.show_sql}" />\r
                        </bean>\r
                </property>\r
-               <property name="packagesToScan" value="de.spring.jpa.domain" />\r
+               <property name="packagesToScan" value="de.spring.persistence.**.domain" />\r
        </bean>\r
 \r
        <jpa:repositories entity-manager-factory-ref="jpaEntityManagerFactory"\r
-               base-package="de.spring.jpa.repository"\r
+               base-package="de.spring.persistence.**.repository"\r
                transaction-manager-ref="transactionManager" />\r
 \r
 </beans>\r
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
new file mode 100644 (file)
index 0000000..1c2f309
--- /dev/null
@@ -0,0 +1,93 @@
+<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"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns:util="http://www.springframework.org/schema/util"
+       xmlns:p="http://www.springframework.org/schema/p"
+       xsi:schemaLocation="
+               http://www.springframework.org/schema/beans
+               http://www.springframework.org/schema/beans/spring-beans.xsd
+        http://www.springframework.org/schema/mvc
+        http://www.springframework.org/schema/mvc/spring-mvc.xsd
+        http://www.springframework.org/schema/context
+        http://www.springframework.org/schema/context/spring-context.xsd
+        http://www.springframework.org/schema/util
+        http://www.springframework.org/schema/util/spring-util.xsd">
+   
+       <!--
+               I am declaring my beans without the automatic annotation. :/
+               Better because we are saving memory but it requires more configuration.
+               
+               See: org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
+               <mvc:annotation-driven/>
+        -->
+        
+   
+       <context:annotation-config />
+   
+       <context:component-scan base-package="de.spring.rest"/>
+       
+       <!--
+               Required beans for generating XML responses from Java objects using JAXB annotations
+               Jackson also works but it doesn't generate XML with namespaces... O.o
+               
+               This implementation will be slower than the one using Jackson :( but I am going to use it just for WADL generation :)
+       -->    
+    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
+        <property name="packagesToScan" value="org.jvnet.ws.wadl"/>
+    </bean>
+       <bean id="jaxbConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
+       <constructor-arg ref="jaxbMarshaller" />
+       </bean>
+    
+       <!-- Required beans for generating JSON responses from Java objects -->
+    <bean id="jsonObjectMapperFactory" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
+       p:indentOutput="true" p:failOnEmptyBeans="false">
+        <property name="featuresToDisable">
+            <array>
+                <util:constant static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
+                <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION"/>
+            </array>
+        </property>
+    </bean>
+    
+    <util:list id="messageConverters">
+        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" p:objectMapper-ref="jsonObjectMapperFactory"/>
+               <ref bean="jaxbConverter" />
+        <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
+    </util:list>
+
+
+       <bean name="handlerAdapter"
+               class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
+               <property name="webBindingInitializer">
+                       <bean
+                               class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
+                               <!-- It enables us to use JSR-303 -->
+                               <property name="validator">
+                                       <bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
+                               </property>
+                       </bean>
+               </property>
+               <property name="messageConverters" ref="messageConverters" />
+               
+               
+               <property name="requestBodyAdvice">
+                       <util:list>
+                               <bean id="requestBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewRequestBodyAdvice"/>
+                       </util:list>
+               </property>
+               
+               
+               <property name="responseBodyAdvice">
+                       <util:list>
+                               <bean id="responseBodyAdvice" class="org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice"/>
+                       </util:list>
+               </property>
+       </bean>
+    
+       <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
+
+       <mvc:default-servlet-handler />
+       
+</beans>
diff --git a/SpringJava/JPA/src/main/webapp/WEB-INF/web.xml b/SpringJava/JPA/src/main/webapp/WEB-INF/web.xml
new file mode 100644 (file)
index 0000000..01de2a8
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
+         version="2.4">
+
+    <display-name>Spring JPA: example JPA</display-name>
+
+    <listener>
+        <listener-class>
+            org.springframework.web.context.ContextLoaderListener
+        </listener-class>
+    </listener>
+
+    <context-param>
+        <param-name>spring.profiles.default</param-name>
+        <param-value>${environment.profile}</param-value>
+        <param-name>contextConfigLocation</param-name>
+        <param-value>
+            classpath*:spring-configuration/*.xml
+        </param-value>
+    </context-param>
+    
+    <!-- Spring REST support -->
+    <servlet>
+        <servlet-name>spring-rest</servlet-name>
+        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
+        <load-on-startup>1</load-on-startup>
+        <async-supported>true</async-supported>
+        <init-param>
+           <param-name>contextConfigLocation</param-name>
+           <param-value>classpath*:spring-configuration/mvc/rest/*.xml</param-value>
+        </init-param>
+    </servlet>
+
+    <servlet-mapping>
+        <servlet-name>spring-rest</servlet-name>
+        <!-- REQUIRED PATTERN BY swagger-ui. IT DOESN'T WORK WITH ANY OTHER o.O -->
+        <url-pattern>/*</url-pattern>
+    </servlet-mapping>
+
+</web-app>
diff --git a/SpringJava/JPA/src/test/java/de/spring/persistence/example/domain/AdTest.java b/SpringJava/JPA/src/test/java/de/spring/persistence/example/domain/AdTest.java
new file mode 100644 (file)
index 0000000..7fb4c57
--- /dev/null
@@ -0,0 +1,5 @@
+package de.spring.persistence.example.domain;
+
+public class AdTest {
+
+}