Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Feb 14, 2015
2 parents 01ae793 + 202fe7c commit 949c275
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 33 deletions.
17 changes: 15 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@

<groupId>com.leanstacks</groupId>
<artifactId>skeleton-ws-spring-boot</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
<name>Web Services Project Skeleton</name>
<description>Skeleton project for RESTful web services using Spring Boot.</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand All @@ -39,6 +45,13 @@
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

<!-- Dependencies for Unit Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/leanstacks/ws/batch/GreetingBatchBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -22,6 +23,9 @@ public class GreetingBatchBean {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private CounterService counterService;

@Autowired
private GreetingService greetingService;

Expand All @@ -38,6 +42,8 @@ public class GreetingBatchBean {
public void cronJob() {
logger.info("> cronJob");

counterService.increment("method.invoked.greetingBatchBean.cronJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
Expand All @@ -57,6 +63,9 @@ public void cronJob() {
public void fixedRateJob() {
logger.info("> fixedRateJob");

counterService
.increment("method.invoked.greetingBatchBean.fixedRateJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
Expand All @@ -79,6 +88,9 @@ public void fixedRateJob() {
public void fixedRateJobWithInitialDelay() {
logger.info("> fixedRateJobWithInitialDelay");

counterService
.increment("method.invoked.greetingBatchBean.fixedRateJobWithInitialDelay");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
Expand All @@ -98,6 +110,9 @@ public void fixedRateJobWithInitialDelay() {
public void fixedDelayJob() {
logger.info("> fixedDelayJob");

counterService
.increment("method.invoked.greetingBatchBean.fixedDelayJob");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
Expand All @@ -120,6 +135,9 @@ public void fixedDelayJob() {
public void fixedDelayJobWithInitialDelay() {
logger.info("> fixedDelayJobWithInitialDelay");

counterService
.increment("method.invoked.greetingBatchBean.fixedDelayJobWithInitialDelay");

// Add scheduled logic here

Collection<Greeting> greetings = greetingService.findAll();
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/leanstacks/ws/service/GreetingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ public interface GreetingService {
*/
void delete(Long id);

/**
* Evicts all members of the "greetings" cache.
*/
void evictCache();

}
35 changes: 33 additions & 2 deletions src/main/java/com/leanstacks/ws/service/GreetingServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.Collection;

import javax.persistence.EntityExistsException;
import javax.persistence.NoResultException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
Expand All @@ -25,13 +29,18 @@ public class GreetingServiceBean implements GreetingService {

private Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired
private CounterService counterService;

@Autowired
private GreetingRepository greetingRepository;

@Override
public Collection<Greeting> findAll() {
logger.info("> findAll");

counterService.increment("method.invoked.greetingServiceBean.findAll");

Collection<Greeting> greetings = greetingRepository.findAll();

logger.info("< findAll");
Expand All @@ -45,6 +54,8 @@ public Collection<Greeting> findAll() {
public Greeting findOne(Long id) {
logger.info("> findOne {}", id);

counterService.increment("method.invoked.greetingServiceBean.findOne");

Greeting greeting = greetingRepository.findOne(id);

logger.info("< findOne {}", id);
Expand All @@ -59,13 +70,16 @@ public Greeting findOne(Long id) {
public Greeting create(Greeting greeting) {
logger.info("> create");

counterService.increment("method.invoked.greetingServiceBean.create");

// Ensure the entity object to be created does NOT exist in the
// repository. Prevent the default behavior of save() which will update
// an existing entity if the entity matching the supplied id exists.
if (greeting.getId() != null) {
logger.error("Attempted to create a Greeting, but id attribute was not null.");
logger.info("< create");
return null;
throw new EntityExistsException(
"Cannot create new Greeting with supplied id. The id attribute must be null to create an entity.");
}

Greeting savedGreeting = greetingRepository.save(greeting);
Expand All @@ -82,14 +96,16 @@ public Greeting create(Greeting greeting) {
public Greeting update(Greeting greeting) {
logger.info("> update {}", greeting.getId());

counterService.increment("method.invoked.greetingServiceBean.update");

// Ensure the entity object to be updated exists in the repository to
// prevent the default behavior of save() which will persist a new
// entity if the entity matching the id does not exist
Greeting greetingToUpdate = findOne(greeting.getId());
if (greetingToUpdate == null) {
logger.error("Attempted to update a Greeting, but the entity does not exist.");
logger.info("< update {}", greeting.getId());
return null;
throw new NoResultException("Requested Greeting not found.");
}

Greeting updatedGreeting = greetingRepository.save(greeting);
Expand All @@ -106,9 +122,24 @@ public Greeting update(Greeting greeting) {
public void delete(Long id) {
logger.info("> delete {}", id);

counterService.increment("method.invoked.greetingServiceBean.delete");

greetingRepository.delete(id);

logger.info("< delete {}", id);
}

@CacheEvict(
value = "greetings",
allEntries = true)
@Override
public void evictCache() {
logger.info("> evictCache");

counterService
.increment("method.invoked.greetingServiceBean.evictCache");

logger.info("< evictCache");
}

}
33 changes: 33 additions & 0 deletions src/main/java/com/leanstacks/ws/web/api/BaseController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.leanstacks.ws.web.api;

import javax.persistence.NoResultException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

public class BaseController {

protected Logger logger = LoggerFactory.getLogger(this.getClass());

@ExceptionHandler(NoResultException.class)
public ResponseEntity<Exception> handleNoResultException(
NoResultException nre) {
logger.error("> handleNoResultException");
logger.error("- NoResultException: ", nre);
logger.error("< handleNoResultException");
return new ResponseEntity<Exception>(HttpStatus.NOT_FOUND);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<Exception> handleException(Exception e) {
logger.error("> handleException");
logger.error("- Exception: ", e);
logger.error("< handleException");
return new ResponseEntity<Exception>(e,
HttpStatus.INTERNAL_SERVER_ERROR);
}

}
33 changes: 4 additions & 29 deletions src/main/java/com/leanstacks/ws/web/api/GreetingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -26,22 +23,11 @@
* @author Matt Warman
*/
@RestController
public class GreetingController {

private Logger logger = LoggerFactory.getLogger(this.getClass());
public class GreetingController extends BaseController {

@Autowired
private GreetingService greetingService;

@ExceptionHandler(Exception.class)
public ResponseEntity<Exception> handleException(Exception e) {
logger.error("> handleException");
logger.error("- Exception: ", e);
logger.error("< handleException");
return new ResponseEntity<Exception>(e,
HttpStatus.INTERNAL_SERVER_ERROR);
}

@RequestMapping(
value = "/api/greetings",
method = RequestMethod.GET,
Expand Down Expand Up @@ -84,11 +70,6 @@ public ResponseEntity<Greeting> createGreeting(
logger.info("> createGreeting");

Greeting savedGreeting = greetingService.create(greeting);
if (savedGreeting == null) {
logger.info("< createGreeting");
return new ResponseEntity<Greeting>(
HttpStatus.INTERNAL_SERVER_ERROR);
}

logger.info("< createGreeting");
return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
Expand All @@ -104,22 +85,16 @@ public ResponseEntity<Greeting> updateGreeting(
logger.info("> updateGreeting");

Greeting updatedGreeting = greetingService.update(greeting);
if (updatedGreeting == null) {
logger.info("< updateGreeting");
return new ResponseEntity<Greeting>(
HttpStatus.INTERNAL_SERVER_ERROR);
}

logger.info("< updateGreeting");
return new ResponseEntity<Greeting>(updatedGreeting, HttpStatus.OK);
}

@RequestMapping(
value = "/api/greetings/{id}",
method = RequestMethod.DELETE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> deleteGreeting(@PathVariable("id") Long id,
@RequestBody Greeting greeting) throws Exception {
method = RequestMethod.DELETE)
public ResponseEntity<Greeting> deleteGreeting(@PathVariable("id") Long id)
throws Exception {
logger.info("> deleteGreeting");

greetingService.delete(id);
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ server.port=
# Initialization
spring.datasource.data=classpath:/data/hsqldb/data.sql

##
# Actuator Configuration
##
management.context-path=/actuators

info.build.groupId=@project.groupId@
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@

##
# Batch Configuration
##
Expand Down
Loading

0 comments on commit 949c275

Please sign in to comment.