Initial commit

parents
# Eclipse files
.classpath
.project
.settings
/target
# IDEA
.idea
*.iml
*.ipr
*.iws
out
# Gradle
.gradle
build
/build
\ No newline at end of file
# Spring Boot Workshop #
*Spring Boot Workshop template project*
You are given a **GRADLE** ready **Spring Boot** project which will run embedded Tomcat within your IDE.
You are also given a basic unit test for a rest controller.
There is no need to provide a database, the application will use everything in the package DOMAIN to provide
you with a working H2 embedded development database.
On the classpath you will also find a file named **classpath:import.sql**. This has nothing to do with Spring, but is a
Hibernate import file for SQL data. It contains randomly generated PRODUCER entities.
## The objective of the workshop ##
1. Implement the repository and services needed to:
- List one PRODUCER
- List all PRODUCERS
- Save a new PRODUCER
- Delete a PRODUCER
2. Write Unit tests to make sure that the service works. You do not need to write tests for the
repository as this is tested extensively by the Spring team.
3. Implement controllers in the CONTROLLER package.
- A REST controller listing PRODUCER by id
- A REST controller listing all PRODUCERS
- A view and form based controller for adding, deleting and listing PRODUCER from **classpath:templates/producer.html**
Implement a form based controller to enable both posting and fetching of data from Tomcat and the application.
## Hints ##
When implementing the repository, try extending it with **CrudRepository<Producer, Long>** to make sure that your repository only
fetches producers and used Long for the magic.
**@Autowired** will give you access to all **@Service**, **@Component** and **@Bean** classes. Use it to fetch your repository from the service.
Implement the provided Service interfaces and annotate the implementation with **@Service("producerService")**
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE")
}
}
''
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'bootworkshop'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.spring.io/libs-release" }
}
dependencies {
compile(
"org.springframework.boot:spring-boot-starter-thymeleaf",
"org.springframework.boot:spring-boot-starter-data-jpa",
"com.h2database:h2:1.4.181",
"org.hibernate:hibernate-validator"
)
testCompile(
"junit:junit",
"org.springframework.boot:spring-boot-starter-test"
)
}
task wrapper(type: Wrapper) {
gradleVersion = '2.1'
distributionUrl = 'http://services.gradle.org/distributions/gradle-2.1-all.zip'
}
\ No newline at end of file
#Mon Sep 29 13:55:49 CEST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-2.1-all.zip
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
APP_HOME="`pwd -P`"
cd "$SAVED" >&-
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = 'BootWorkshop'
package no.nsd.bootworkshop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class BootWorkshop extends SpringBootServletInitializer {
/**
* Create the Application
* @param applicationBuilder spring application builder
* @return the sources used to build the application
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(BootWorkshop.class);
}
/**
* Main method used to bootstrap the Spring Boot Jar.
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(BootWorkshop.class);
}
/**
* A Bean to provide access Spring's {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter} *
* @return {@link org.springframework.http.converter.json.MappingJackson2HttpMessageConverter} with human readable json.
*/
@Bean
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter messageConverter =
new MappingJackson2HttpMessageConverter();
messageConverter.setPrettyPrint(true);
return messageConverter;
}
}
package no.nsd.bootworkshop.domain;
import org.hibernate.validator.constraints.Email;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Table(name = "producer")
@Entity
public class Producer {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull(message = "validation.field.NotNull.message")
@Size(max = 128, min = 3, message = "validation.field.Name.message")
@Column(name = "name")
private String name;
@NotNull(message = "validation.field.NotNull.message")
@Email(message = "validation.field.Email.message")
@Size(max = 64, min = 5)
@Column(name = "email")
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Producer producer = (Producer) o;
if (email != null ? !email.equals(producer.email) : producer.email != null) return false;
if (id != null ? !id.equals(producer.id) : producer.id != null) return false;
if (name != null ? !name.equals(producer.name) : producer.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (email != null ? email.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Producer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
package no.nsd.bootworkshop.service;
import no.nsd.bootworkshop.domain.Producer;
import java.util.List;
public interface ProducerService {
public Producer findById(Long id);
public List<Producer> findAll();
public Producer save(Producer producer);
public void delete(Producer producer);
}
\ No newline at end of file
package no.nsd.bootworkshop.utils;
import java.util.ArrayList;
import java.util.List;
public class Lists<T> {
/**
* Produce an {@link java.util.ArrayList} from a {@link java.lang.Iterable}
* @param iterable from anywhere.
* @return an {@link java.util.ArrayList} representation of a {@link java.lang.Iterable}
*/
public List<T> iterableToList(Iterable<T> iterable) {
List<T> aList = new ArrayList<>();
iterable.forEach(aList::add);
return aList;
}
}
\ No newline at end of file
package no.nsd.bootworkshop.utils;
public class Message {
private String type;
private String text;
/**
* Empty default constructor. Should not be called.
*/
public Message() {
}
/**
* Constructor for actual messages. Should be used when ever
* a proper message will be sent across the system.
* @param messageType
* @param text
*/
public Message(String messageType, String text) {
this.type = messageType;
this.text = text;
}
/**
* @return type
*/
public String getType() {
return type;
}
/**
* @param type
*/
public void setType(String type) {
this.type = type;
}
/**
*
* @return text
*/
public String getText() {
return text;
}
/**
* Set the text message, including a JQuery snippet to remove
* it after a certain time.
*
* @param text
*/
public void setText(String text) {
this.text += text;
}
}
-- These are our import statements used by the H2 database during development.
-- Must never be used in producion.
INSERT INTO producer (name, email) VALUES('Carlos', 'car73@example.com');
INSERT INTO producer (name, email) VALUES('Eleonora', 'ele2@example.com');
INSERT INTO producer (name, email) VALUES('Leana', 'lea32@example.com');
INSERT INTO producer (name, email) VALUES('Marcos', 'mar87@example.com');
INSERT INTO producer (name, email) VALUES('Alison', 'ali24@example.com');
INSERT INTO producer (name, email) VALUES('Keshia', 'kes98@example.com');
INSERT INTO producer (name, email) VALUES('Russ', 'rus18@example.com');
INSERT INTO producer (name, email) VALUES('Tiffiny', 'tif21@example.com');
INSERT INTO producer (name, email) VALUES('Yuette', 'yue47@example.com');
INSERT INTO producer (name, email) VALUES('Beverlee', '54bev@example.com');
INSERT INTO producer (name, email) VALUES('Sheryll', '31she@example.com');
INSERT INTO producer (name, email) VALUES('Tish', 'tis48@example.com');
INSERT INTO producer (name, email) VALUES('Alisha', 'ali12@example.com');
INSERT INTO producer (name, email) VALUES('Marisol', 'mar98@example.com');
INSERT INTO producer (name, email) VALUES('Hiedi', 'hei77@example.com');
INSERT INTO producer (name, email) VALUES('Nedra', 'ned65@example.com');
INSERT INTO producer (name, email) VALUES('Barbera', 'bar43@example.com');
INSERT INTO producer (name, email) VALUES('Elma', 'elm82@example.com');
INSERT INTO producer (name, email) VALUES('Dario', 'dar1@example.com');
INSERT INTO producer (name, email) VALUES('Agatha', 'aga2@example.com');
INSERT INTO producer (name, email) VALUES('Inga', 'ing66@example.com');
INSERT INTO producer (name, email) VALUES('Adell', 'ade21@example.com');
INSERT INTO producer (name, email) VALUES('Parthenia', 'par39@example.com');
INSERT INTO producer (name, email) VALUES('Jerrell', 'jar76@example.com');
INSERT INTO producer (name, email) VALUES('Meri', '77mer@example.com');
INSERT INTO producer (name, email) VALUES('Inger', 'ing00@example.com');
INSERT INTO producer (name, email) VALUES('Chi', 'chi22@example.com');
INSERT INTO producer (name, email) VALUES('Kirsten', 'kir51@example.com');
INSERT INTO producer (name, email) VALUES('Preston', 'pre91@example.com');
INSERT INTO producer (name, email) VALUES('Adolfo ', '97ado@example.com');
\ No newline at end of file
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'The time is, ' + ${date} + '!'" />
</body>
</html>
\ No newline at end of file
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Procuer</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Add new producer</h1>
<form action="#" th:action="@{/producer}" th:object="${producer}" method="post">
<input type="text" id="name" name="name" th:value="*{name}" />
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
<input type="email" id="email" name="email" th:value="*{email}" />
<div th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></div>
<br />
<button type="submit">Add Producer</button>
</form>
<div th:if="${message} != null">
<div id="${message.type}" th:text="${message.text}">Message:</div>
</div>
<hr />
<h1>Existing producers</h1>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Command</th>
</tr>
<tr th:each="producer : ${producers}">
<td th:text="${producer.name}"></td>
<td th:text="${producer.email}"></td>
<td>
<a th:href="@{/producer/delete/{id}(id=${producer.id})}">Delete</a>
</td>
</tr>
</table>
</body>
</html>
\ No newline at end of file
package controller;
import no.nsd.bootworkshop.BootWorkshop;
import no.nsd.bootworkshop.domain.Producer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
@IntegrationTest
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BootWorkshop.class)
public class RestDemoControllerTest {
@Test
public void testProducers() throws Exception {
RestTemplate template = new RestTemplate();
ResponseEntity<Producer[]> producersEntity = template.getForEntity(
"http://localhost:8080/rest/producers", Producer[].class);
Producer[] producers = producersEntity.getBody();
assertThat(producersEntity.getStatusCode().toString(), is("200"));