Merge pull request #5932 from eugenp/BAEL-10808
add readmes, remove extra modules
This commit is contained in:
commit
997587095a
|
@ -0,0 +1,5 @@
|
|||
### Relevant articles
|
||||
|
||||
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
|
||||
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
Binary file not shown.
|
@ -1 +0,0 @@
|
|||
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
|
|
@ -1,22 +0,0 @@
|
|||
Setting up the Maven Wrapper on an Application
|
||||
==============================================
|
||||
|
||||
This is the code that shows the configurations of maven wrapper on a SpringBoot project.
|
||||
|
||||
### Requirements
|
||||
|
||||
- Maven
|
||||
- JDK 7
|
||||
|
||||
### Running
|
||||
|
||||
To build and start the server simply type
|
||||
|
||||
```bash
|
||||
$ ./mvn clean install
|
||||
$ ./mvn spring-boot:run
|
||||
```
|
||||
|
||||
Now with default configurations it will be available at: [http://localhost:8080](http://localhost:8080)
|
||||
|
||||
Enjoy it :)
|
|
@ -1,227 +0,0 @@
|
|||
#!/bin/sh
|
||||
# ----------------------------------------------------------------------------
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
# ----------------------------------------------------------------------------
|
||||
# Maven2 Start Up Batch script
|
||||
#
|
||||
# Required ENV vars:
|
||||
# ------------------
|
||||
# JAVA_HOME - location of a JDK home dir
|
||||
#
|
||||
# Optional ENV vars
|
||||
# -----------------
|
||||
# M2_HOME - location of maven2's installed home dir
|
||||
# MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
# e.g. to debug Maven itself, use
|
||||
# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
# MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
# ----------------------------------------------------------------------------
|
||||
|
||||
if [ -z "$MAVEN_SKIP_RC" ] ; then
|
||||
|
||||
if [ -f /etc/mavenrc ] ; then
|
||||
. /etc/mavenrc
|
||||
fi
|
||||
|
||||
if [ -f "$HOME/.mavenrc" ] ; then
|
||||
. "$HOME/.mavenrc"
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
# OS specific support. $var _must_ be set to either true or false.
|
||||
cygwin=false;
|
||||
darwin=false;
|
||||
mingw=false
|
||||
case "`uname`" in
|
||||
CYGWIN*) cygwin=true ;;
|
||||
MINGW*) mingw=true;;
|
||||
Darwin*) darwin=true
|
||||
# Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
|
||||
# See https://developer.apple.com/library/mac/qa/qa1170/_index.html
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
if [ -x "/usr/libexec/java_home" ]; then
|
||||
export JAVA_HOME="`/usr/libexec/java_home`"
|
||||
else
|
||||
export JAVA_HOME="/Library/Java/Home"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
if [ -r /etc/gentoo-release ] ; then
|
||||
JAVA_HOME=`java-config --jre-home`
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$M2_HOME" ] ; then
|
||||
## resolve links - $0 may be a link to maven's home
|
||||
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
|
||||
|
||||
saveddir=`pwd`
|
||||
|
||||
M2_HOME=`dirname "$PRG"`/..
|
||||
|
||||
# make it fully qualified
|
||||
M2_HOME=`cd "$M2_HOME" && pwd`
|
||||
|
||||
cd "$saveddir"
|
||||
# echo Using m2 at $M2_HOME
|
||||
fi
|
||||
|
||||
# For Cygwin, ensure paths are in UNIX format before anything is touched
|
||||
if $cygwin ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --unix "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
|
||||
fi
|
||||
|
||||
# For Mingw, ensure paths are in UNIX format before anything is touched
|
||||
if $mingw ; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME="`(cd "$M2_HOME"; pwd)`"
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
|
||||
# TODO classpath?
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ]; then
|
||||
javaExecutable="`which javac`"
|
||||
if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
|
||||
# readlink(1) is not available as standard on Solaris 10.
|
||||
readLink=`which readlink`
|
||||
if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
|
||||
if $darwin ; then
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
|
||||
else
|
||||
javaExecutable="`readlink -f \"$javaExecutable\"`"
|
||||
fi
|
||||
javaHome="`dirname \"$javaExecutable\"`"
|
||||
javaHome=`expr "$javaHome" : '\(.*\)/bin'`
|
||||
JAVA_HOME="$javaHome"
|
||||
export JAVA_HOME
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$JAVACMD" ] ; then
|
||||
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
|
||||
else
|
||||
JAVACMD="`which java`"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
echo "Error: JAVA_HOME is not defined correctly." >&2
|
||||
echo " We cannot execute $JAVACMD" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$JAVA_HOME" ] ; then
|
||||
echo "Warning: JAVA_HOME environment variable is not set."
|
||||
fi
|
||||
|
||||
CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
|
||||
|
||||
# traverses directory structure from process work directory to filesystem root
|
||||
# first directory with .mvn subdirectory is considered project base directory
|
||||
find_maven_basedir() {
|
||||
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
echo "Path not specified to find_maven_basedir"
|
||||
return 1
|
||||
fi
|
||||
|
||||
basedir="$1"
|
||||
wdir="$1"
|
||||
while [ "$wdir" != '/' ] ; do
|
||||
if [ -d "$wdir"/.mvn ] ; then
|
||||
basedir=$wdir
|
||||
break
|
||||
fi
|
||||
# workaround for JBEAP-8937 (on Solaris 10/Sparc)
|
||||
if [ -d "${wdir}" ]; then
|
||||
wdir=`cd "$wdir/.."; pwd`
|
||||
fi
|
||||
# end of workaround
|
||||
done
|
||||
echo "${basedir}"
|
||||
}
|
||||
|
||||
# concatenates all lines of a file
|
||||
concat_lines() {
|
||||
if [ -f "$1" ]; then
|
||||
echo "$(tr -s '\n' ' ' < "$1")"
|
||||
fi
|
||||
}
|
||||
|
||||
BASE_DIR=`find_maven_basedir "$(pwd)"`
|
||||
if [ -z "$BASE_DIR" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
|
||||
export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
|
||||
if [ "$MVNW_VERBOSE" = true ]; then
|
||||
echo $MAVEN_PROJECTBASEDIR
|
||||
fi
|
||||
MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin; then
|
||||
[ -n "$M2_HOME" ] &&
|
||||
M2_HOME=`cygpath --path --windows "$M2_HOME"`
|
||||
[ -n "$JAVA_HOME" ] &&
|
||||
JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
|
||||
[ -n "$CLASSPATH" ] &&
|
||||
CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
|
||||
[ -n "$MAVEN_PROJECTBASEDIR" ] &&
|
||||
MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
|
||||
fi
|
||||
|
||||
WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
exec "$JAVACMD" \
|
||||
$MAVEN_OPTS \
|
||||
-classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
|
||||
"-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
|
||||
${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
|
|
@ -1,145 +0,0 @@
|
|||
@REM ----------------------------------------------------------------------------
|
||||
@REM Licensed to the Apache Software Foundation (ASF) under one
|
||||
@REM or more contributor license agreements. See the NOTICE file
|
||||
@REM distributed with this work for additional information
|
||||
@REM regarding copyright ownership. The ASF licenses this file
|
||||
@REM to you under the Apache License, Version 2.0 (the
|
||||
@REM "License"); you may not use this file except in compliance
|
||||
@REM with the License. You may obtain a copy of the License at
|
||||
@REM
|
||||
@REM http://www.apache.org/licenses/LICENSE-2.0
|
||||
@REM
|
||||
@REM Unless required by applicable law or agreed to in writing,
|
||||
@REM software distributed under the License is distributed on an
|
||||
@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
@REM KIND, either express or implied. See the License for the
|
||||
@REM specific language governing permissions and limitations
|
||||
@REM under the License.
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM ----------------------------------------------------------------------------
|
||||
@REM Maven2 Start Up Batch script
|
||||
@REM
|
||||
@REM Required ENV vars:
|
||||
@REM JAVA_HOME - location of a JDK home dir
|
||||
@REM
|
||||
@REM Optional ENV vars
|
||||
@REM M2_HOME - location of maven2's installed home dir
|
||||
@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
|
||||
@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
|
||||
@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
|
||||
@REM e.g. to debug Maven itself, use
|
||||
@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
|
||||
@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
|
||||
@REM ----------------------------------------------------------------------------
|
||||
|
||||
@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
|
||||
@echo off
|
||||
@REM set title of command window
|
||||
title %0
|
||||
@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
|
||||
@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
|
||||
|
||||
@REM set %HOME% to equivalent of $HOME
|
||||
if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
|
||||
|
||||
@REM Execute a user defined script before this one
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
|
||||
@REM check for pre script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
|
||||
if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
|
||||
:skipRcPre
|
||||
|
||||
@setlocal
|
||||
|
||||
set ERROR_CODE=0
|
||||
|
||||
@REM To isolate internal variables from possible post scripts, we use another setlocal
|
||||
@setlocal
|
||||
|
||||
@REM ==== START VALIDATION ====
|
||||
if not "%JAVA_HOME%" == "" goto OkJHome
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME not found in your environment. >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
:OkJHome
|
||||
if exist "%JAVA_HOME%\bin\java.exe" goto init
|
||||
|
||||
echo.
|
||||
echo Error: JAVA_HOME is set to an invalid directory. >&2
|
||||
echo JAVA_HOME = "%JAVA_HOME%" >&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the >&2
|
||||
echo location of your Java installation. >&2
|
||||
echo.
|
||||
goto error
|
||||
|
||||
@REM ==== END VALIDATION ====
|
||||
|
||||
:init
|
||||
|
||||
@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
|
||||
@REM Fallback to current working directory if not found.
|
||||
|
||||
set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
|
||||
IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
|
||||
|
||||
set EXEC_DIR=%CD%
|
||||
set WDIR=%EXEC_DIR%
|
||||
:findBaseDir
|
||||
IF EXIST "%WDIR%"\.mvn goto baseDirFound
|
||||
cd ..
|
||||
IF "%WDIR%"=="%CD%" goto baseDirNotFound
|
||||
set WDIR=%CD%
|
||||
goto findBaseDir
|
||||
|
||||
:baseDirFound
|
||||
set MAVEN_PROJECTBASEDIR=%WDIR%
|
||||
cd "%EXEC_DIR%"
|
||||
goto endDetectBaseDir
|
||||
|
||||
:baseDirNotFound
|
||||
set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
|
||||
cd "%EXEC_DIR%"
|
||||
|
||||
:endDetectBaseDir
|
||||
|
||||
IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
|
||||
|
||||
@setlocal EnableExtensions EnableDelayedExpansion
|
||||
for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
|
||||
@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
|
||||
|
||||
:endReadAdditionalConfig
|
||||
|
||||
SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
|
||||
|
||||
set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
|
||||
set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
|
||||
|
||||
%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
|
||||
if ERRORLEVEL 1 goto error
|
||||
goto end
|
||||
|
||||
:error
|
||||
set ERROR_CODE=1
|
||||
|
||||
:end
|
||||
@endlocal & set ERROR_CODE=%ERROR_CODE%
|
||||
|
||||
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
|
||||
@REM check for post script, once with legacy .bat ending and once with .cmd ending
|
||||
if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
|
||||
if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
|
||||
:skipRcPost
|
||||
|
||||
@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
|
||||
if "%MAVEN_BATCH_PAUSE%" == "on" pause
|
||||
|
||||
if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
|
||||
|
||||
exit /B %ERROR_CODE%
|
|
@ -1,38 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>mvn-wrapper</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>mvn-wrapper</name>
|
||||
<description>Setting up the Maven Wrapper</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -1,11 +0,0 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MvnWrapperApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MvnWrapperApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
This is a parent module for all projects using Spring Boot 1.
|
|
@ -1 +1,2 @@
|
|||
## Relevant articles:
|
||||
|
||||
This is a parent module for all projects using Spring Boot 2.
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
Parent module for Kotlin projects.
|
|
@ -0,0 +1,3 @@
|
|||
# Relevant Articles
|
||||
|
||||
- [Auto-Generated Field for MongoDB using Spring Boot](https://www.baeldung.com/spring-boot-mongodb-auto-generated-field)
|
|
@ -0,0 +1,3 @@
|
|||
#Relevant Articles
|
||||
|
||||
- [Spring Data with Reactive Cassandra](https://www.baeldung.com/spring-data-cassandra-reactive)
|
|
@ -13,10 +13,10 @@
|
|||
<description>Spring Data Cassandra reactive</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.0.RELEASE</version>
|
||||
<relativePath/>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
|
@ -57,14 +57,5 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
11
pom.xml
11
pom.xml
|
@ -490,7 +490,6 @@
|
|||
<module>msf4j</module>
|
||||
<!-- <module>muleesb</module> --> <!-- Fixing in BAEL-10878 -->
|
||||
<module>mustache</module>
|
||||
<module>mvn-wrapper</module>
|
||||
<module>mybatis</module>
|
||||
|
||||
<module>noexception</module>
|
||||
|
@ -678,7 +677,6 @@
|
|||
|
||||
<module>spring-mobile</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mustache</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-forms-thymeleaf</module>
|
||||
<module>spring-mvc-java</module>
|
||||
|
@ -687,7 +685,6 @@
|
|||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-mybatis</module>
|
||||
|
||||
<module>spring-protobuf</module>
|
||||
|
||||
|
@ -698,7 +695,6 @@
|
|||
<module>spring-remoting</module>
|
||||
<module>spring-rest</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-embedded-tomcat</module>
|
||||
<module>spring-rest-full</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
|
@ -756,7 +752,6 @@
|
|||
|
||||
<module>spring-zuul</module>
|
||||
|
||||
<module>sse-jaxrs</module>
|
||||
<module>static-analysis</module>
|
||||
<module>stripe</module>
|
||||
<module>structurizr</module>
|
||||
|
@ -907,7 +902,6 @@
|
|||
<module>spring-remoting/remoting-rmi/remoting-rmi-server</module>
|
||||
<module>spring-rest</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-embedded-tomcat</module>
|
||||
<module>spring-rest-full</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
|
@ -1201,7 +1195,6 @@
|
|||
<module>msf4j</module>
|
||||
<!-- <module>muleesb</module> --> <!-- Fixing in BAEL-10878 -->
|
||||
<module>mustache</module>
|
||||
<module>mvn-wrapper</module>
|
||||
<module>mybatis</module>
|
||||
|
||||
<module>noexception</module>
|
||||
|
@ -1385,7 +1378,6 @@
|
|||
|
||||
<module>spring-mobile</module>
|
||||
<module>spring-mockito</module>
|
||||
<module>spring-mustache</module>
|
||||
<module>spring-mvc-forms-jsp</module>
|
||||
<module>spring-mvc-forms-thymeleaf</module>
|
||||
<module>spring-mvc-java</module>
|
||||
|
@ -1394,7 +1386,6 @@
|
|||
<module>spring-mvc-velocity</module>
|
||||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-mvc-xml</module>
|
||||
<module>spring-mybatis</module>
|
||||
|
||||
<module>spring-protobuf</module>
|
||||
|
||||
|
@ -1405,7 +1396,6 @@
|
|||
<module>spring-remoting</module>
|
||||
<module>spring-rest</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-embedded-tomcat</module>
|
||||
<module>spring-rest-full</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
|
@ -1463,7 +1453,6 @@
|
|||
|
||||
<module>spring-zuul</module>
|
||||
|
||||
<module>sse-jaxrs</module>
|
||||
<module>static-analysis</module>
|
||||
<module>stripe</module>
|
||||
<module>structurizr</module>
|
||||
|
|
|
@ -15,19 +15,8 @@
|
|||
### Relevant Articles:
|
||||
- [Intro to Spring Cloud Netflix - Hystrix](http://www.baeldung.com/spring-cloud-netflix-hystrix)
|
||||
- [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application)
|
||||
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
|
||||
- [A Quick Guide to Spring Cloud Consul](http://www.baeldung.com/spring-cloud-consul)
|
||||
- [An Introduction to Spring Cloud Zookeeper](http://www.baeldung.com/spring-cloud-zookeeper)
|
||||
- [Using a Spring Cloud App Starter](http://www.baeldung.com/using-a-spring-cloud-app-starter)
|
||||
- [Spring Cloud Connectors and Heroku](http://www.baeldung.com/spring-cloud-heroku)
|
||||
- [An Example of Load Balancing with Zuul and Eureka](http://www.baeldung.com/zuul-load-balancing)
|
||||
- [An Intro to Spring Cloud Contract](http://www.baeldung.com/spring-cloud-contract)
|
||||
- [Using a Spring Cloud App Starter](http://www.baeldung.com/spring-cloud-app-starter)
|
||||
- [Instance Profile Credentials using Spring Cloud](http://www.baeldung.com/spring-cloud-instance-profiles)
|
||||
- [An Intro to Spring Cloud Security](http://www.baeldung.com/spring-cloud-security)
|
||||
- [An Intro to Spring Cloud Task](http://www.baeldung.com/spring-cloud-task)
|
||||
- [Running Spring Boot Applications With Minikube](http://www.baeldung.com/spring-boot-minikube)
|
||||
- [Introduction to Netflix Archaius with Spring Cloud](https://www.baeldung.com/netflix-archaius-spring-cloud-integration)
|
||||
- [An Intro to Spring Cloud Vault](https://www.baeldung.com/spring-cloud-vault)
|
||||
- [Netflix Archaius with Various Database Configurations](https://www.baeldung.com/netflix-archaius-database-configurations)
|
||||
- [Rate Limiting in Spring Cloud Netflix Zuul](https://www.baeldung.com/spring-cloud-zuul-rate-limit)
|
||||
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
# Relevant Articles
|
||||
|
||||
- [Introduction to Netflix Archaius with Spring Cloud](https://www.baeldung.com/netflix-archaius-spring-cloud-integration)
|
||||
- [Netflix Archaius with Various Database Configurations](https://www.baeldung.com/netflix-archaius-database-configurations)
|
||||
|
||||
# Spring Cloud Archaius
|
||||
|
||||
#### Basic Config
|
||||
|
@ -11,4 +16,4 @@ These properties are set up on the main method, since Archaius uses System prope
|
|||
#### Additional Sources
|
||||
In this service we create a new AbstractConfiguration bean, setting up a new Configuration Properties source.
|
||||
|
||||
These properties have precedence over all the other properties in the Archaius Composite Configuration.
|
||||
These properties have precedence over all the other properties in the Archaius Composite Configuration.
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
# Spring Cloud AWS
|
||||
|
||||
# Relevant Articles
|
||||
- [Spring Cloud AWS – S3](https://www.baeldung.com/spring-cloud-aws-s3)
|
||||
- [Spring Cloud AWS – EC2](https://www.baeldung.com/spring-cloud-aws-ec2)
|
||||
- [Spring Cloud AWS – RDS](https://www.baeldung.com/spring-cloud-aws-rds)
|
||||
- [Spring Cloud AWS – Messaging Support](https://www.baeldung.com/spring-cloud-aws-messaging)
|
||||
|
||||
#### Running the Integration Tests
|
||||
|
||||
To run the Integration Tests, we need to have an AWS account and have API keys generated for programmatic access. Edit
|
||||
|
@ -23,4 +29,4 @@ Multiple application classes are available under this project. To launch Instan
|
|||
|
||||
```
|
||||
<start-class>com.baeldung.spring.cloud.aws.InstanceProfileAwsApplication</start-class>
|
||||
```
|
||||
```
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
aaa
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Spring Cloud Connectors and Heroku](http://www.baeldung.com/spring-cloud-heroku)
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
### Relevant Articles:
|
||||
- [A Quick Guide to Spring Cloud Consul](http://www.baeldung.com/spring-cloud-consul)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [An Intro to Spring Cloud Contract](http://www.baeldung.com/spring-cloud-contract)
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
|
||||
- [Running Spring Boot Applications With Minikube](https://www.baeldung.com/spring-boot-minikube)
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
Code for an ebook - "A REST API with Spring Boot and Spring Cloud"
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [An Intro to Spring Cloud Security](http://www.baeldung.com/spring-cloud-security)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [An Intro to Spring Cloud Task](http://www.baeldung.com/spring-cloud-task)
|
|
@ -0,0 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [An Intro to Spring Cloud Vault](https://www.baeldung.com/spring-cloud-vault)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [An Introduction to Spring Cloud Zookeeper](http://www.baeldung.com/spring-cloud-zookeeper)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [An Example of Load Balancing with Zuul and Eureka](http://www.baeldung.com/zuul-load-balancing)
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Rate Limiting in Spring Cloud Netflix Zuul](https://www.baeldung.com/spring-cloud-zuul-rate-limit)
|
|
@ -1,24 +0,0 @@
|
|||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
|
@ -1 +0,0 @@
|
|||
## Relevant articles:
|
|
@ -1,55 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>spring-mustache</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>spring-mustache</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mustache</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>bootstrap</artifactId>
|
||||
<version>${bootstrap.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.fluttercode.datafactory</groupId>
|
||||
<artifactId>datafactory</artifactId>
|
||||
<version>${datafactory.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<bootstrap.version>3.3.7</bootstrap.version>
|
||||
<datafactory.version>0.8</datafactory.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,32 +0,0 @@
|
|||
package com.baeldung.springmustache;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.mustache.MustacheEnvironmentCollector;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = {"com.baeldung"})
|
||||
public class SpringMustacheApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringMustacheApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, Environment environment) {
|
||||
|
||||
MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector();
|
||||
collector.setEnvironment(environment);
|
||||
|
||||
return Mustache.compiler()
|
||||
.defaultValue("Some Default Value")
|
||||
.withLoader(templateLoader)
|
||||
.withCollector(collector);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
package com.baeldung.springmustache.controller;
|
||||
|
||||
import com.baeldung.springmustache.model.Article;
|
||||
import org.fluttercode.datafactory.impl.DataFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Controller
|
||||
public class ArticleController {
|
||||
|
||||
@GetMapping("/article")
|
||||
public ModelAndView displayArticle(Map<String, Object> model) {
|
||||
|
||||
List<Article> articles = IntStream.range(0, 10)
|
||||
.mapToObj(i -> generateArticle("Article Title " + i))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Map<String, Object> modelMap = new HashMap<>();
|
||||
modelMap.put("articles", articles);
|
||||
|
||||
return new ModelAndView("index", modelMap);
|
||||
}
|
||||
|
||||
private Article generateArticle(String title) {
|
||||
Article article = new Article();
|
||||
DataFactory factory = new DataFactory();
|
||||
article.setTitle(title);
|
||||
article.setBody(
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur faucibus tempor diam. In molestie arcu eget ante facilisis sodales. Maecenas porta tellus sapien, eget rutrum nisi blandit in. Mauris tempor auctor ante, ut blandit velit venenatis id. Ut varius, augue aliquet feugiat congue, arcu ipsum finibus purus, dapibus semper velit sapien venenatis magna. Nunc quam ex, aliquet at rutrum sed, vestibulum quis libero. In laoreet libero cursus maximus vulputate. Nullam in fermentum sem. Duis aliquam ullamcorper dui, et dictum justo placerat id. Aliquam pretium orci quis sapien convallis, non blandit est tempus.");
|
||||
article.setPublishDate(factory.getBirthDate().toString());
|
||||
article.setAuthor(factory.getName());
|
||||
return article;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
package com.baeldung.springmustache.model;
|
||||
|
||||
public class Article {
|
||||
private String title;
|
||||
private String body;
|
||||
private String author;
|
||||
private String publishDate;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getPublishDate() {
|
||||
return publishDate;
|
||||
}
|
||||
|
||||
public void setPublishDate(String publishDate) {
|
||||
this.publishDate = publishDate;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,9 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
|
||||
<body>
|
||||
Something went wrong: {{status}} {{error}}
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,9 +0,0 @@
|
|||
{{>layout/header}}
|
||||
<body>
|
||||
<div class="container">{{>layout/article}}</div>
|
||||
|
||||
<script type="text/javascript"
|
||||
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
||||
|
||||
</body>
|
||||
{{>layout/footer}}
|
|
@ -1,8 +0,0 @@
|
|||
<div class="starter-template">
|
||||
{{#articles}}
|
||||
<h1>{{title}}</h1>
|
||||
<h3>{{publishDate}}</h3>
|
||||
<h3>{{author}}</h3>
|
||||
<p>{{body}}</p>
|
||||
{{/articles}}
|
||||
</div>
|
|
@ -1,2 +0,0 @@
|
|||
<!-- this is footer -->
|
||||
</html>
|
|
@ -1,11 +0,0 @@
|
|||
<!DOCTYPE HTML>
|
||||
<head>
|
||||
|
||||
<title></title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
|
||||
<link rel="stylesheet" href="css/main.css" />
|
||||
|
||||
</head>
|
||||
<!-- this is header -->
|
|
@ -1,30 +0,0 @@
|
|||
package com.baeldung.springmustache;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class SpringMustacheApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenIndexPageWhenContainsArticleThenTrue() {
|
||||
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity("/article", String.class);
|
||||
|
||||
Assert.assertTrue(entity.getStatusCode().equals(HttpStatus.OK));
|
||||
Assert.assertTrue(entity.getBody().contains("Article Title 0"));
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
## Relevant articles:
|
|
@ -1,73 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-mybatis</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-mybatis</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
<version>${mybatis.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis-spring</artifactId>
|
||||
<version>${mybatis-spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-webmvc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
<version>${servlet.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>spring-mybatis</finalName>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<mybatis.version>3.1.1</mybatis.version>
|
||||
<mybatis-spring.version>1.1.1</mybatis-spring.version>
|
||||
<spring.version>3.1.1.RELEASE</spring.version>
|
||||
<spring-webmvc.version>3.2.4.RELEASE</spring-webmvc.version>
|
||||
<mysql-connector.version>5.1.40</mysql-connector.version>
|
||||
<jstl.version>1.2</jstl.version>
|
||||
<servlet.version>2.5</servlet.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -1,48 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.application;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.baeldung.spring.mybatis.model.Student;
|
||||
import com.baeldung.spring.mybatis.service.StudentService;
|
||||
|
||||
public class SpringMyBatisApplication {
|
||||
|
||||
public static void main(String[] args){
|
||||
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("mybatis-spring.xml");
|
||||
|
||||
StudentService studentService = (StudentService) ctx.getBean("studentService");
|
||||
Student student = new Student();
|
||||
student.setFirstName("Santosh");
|
||||
student.setLastName("B S");
|
||||
student.setEmailAddress("santosh.bse@gmail.com");
|
||||
student.setPassword("Test123");
|
||||
student.setDateOfBirth(new Date());
|
||||
student.setUserName("santoshbs1");
|
||||
|
||||
boolean result = studentService.insertStudent(student);
|
||||
if(result){
|
||||
System.out.println("Student record saved successfully");
|
||||
}
|
||||
else{
|
||||
System.out.println("Encountered an error while saving student data");
|
||||
}
|
||||
|
||||
final String userName = "santosh";
|
||||
Student matchingStudent = studentService.getStudentByUserName(userName);
|
||||
if(matchingStudent == null){
|
||||
System.out.println("No matching student found for User Name - " + userName);
|
||||
}
|
||||
else{
|
||||
System.out.println("Student Details are as follows : ");
|
||||
System.out.println("First Name : " + matchingStudent.getFirstName());
|
||||
System.out.println("Last Name : " + matchingStudent.getLastName());
|
||||
System.out.println("EMail : " + matchingStudent.getEmailAddress());
|
||||
System.out.println("DOB : " + matchingStudent.getDateOfBirth());
|
||||
System.out.println("User Name : " + matchingStudent.getUserName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
|
||||
import com.baeldung.spring.mybatis.model.Student;
|
||||
import com.baeldung.spring.mybatis.model.StudentLogin;
|
||||
import com.baeldung.spring.mybatis.service.StudentService;
|
||||
|
||||
@Controller
|
||||
@SessionAttributes("student")
|
||||
public class StudentController {
|
||||
|
||||
@Autowired
|
||||
private StudentService studentService;
|
||||
|
||||
@RequestMapping(value = "/signup", method = RequestMethod.GET)
|
||||
public String signup(Model model) {
|
||||
Student student = new Student();
|
||||
model.addAttribute("student", student);
|
||||
return "signup";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/signup", method = RequestMethod.POST)
|
||||
public String signup(@Validated @ModelAttribute("student") Student student, BindingResult result, ModelMap model) {
|
||||
if (studentService.getStudentByUserName(student.getUserName()) != null) {
|
||||
model.addAttribute("message", "User Name exists. Try another user name");
|
||||
return "signup";
|
||||
} else {
|
||||
studentService.insertStudent(student);
|
||||
model.addAttribute("message", "Saved student details");
|
||||
return "redirect:login.html";
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
||||
public String login(Model model) {
|
||||
StudentLogin studentLogin = new StudentLogin();
|
||||
model.addAttribute("studentLogin", studentLogin);
|
||||
return "login";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
||||
public String login(@ModelAttribute("studentLogin") StudentLogin studentLogin, BindingResult result, ModelMap model) {
|
||||
boolean found = studentService.getStudentByLogin(studentLogin.getUserName(), studentLogin.getPassword()) != null;
|
||||
if (found) {
|
||||
return "success";
|
||||
} else {
|
||||
return "failure";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.mappers;
|
||||
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Options;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import com.baeldung.spring.mybatis.model.Student;
|
||||
|
||||
public interface StudentMapper {
|
||||
@Insert("INSERT INTO student(userName, password, firstName,lastName, dateOfBirth, emailAddress) VALUES"
|
||||
+ "(#{userName},#{password}, #{firstName}, #{lastName}, #{dateOfBirth}, #{emailAddress})")
|
||||
@Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true, keyColumn = "id")
|
||||
public void insertStudent(Student student);
|
||||
|
||||
@Select("SELECT USERNAME as userName, PASSWORD as password, FIRSTNAME as firstName, LASTNAME as lastName, "
|
||||
+ "DATEOFBIRTH as dateOfBirth, EMAILADDRESS as emailAddress " + "FROM student WHERE userName = #{userName}")
|
||||
public Student getStudentByUserName(String userName);
|
||||
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Student {
|
||||
private Long id;
|
||||
private String userName;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String password;
|
||||
private String emailAddress;
|
||||
private Date dateOfBirth;
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
public Date getDateOfBirth() {
|
||||
return dateOfBirth;
|
||||
}
|
||||
public void setDateOfBirth(Date dateOfBirth) {
|
||||
this.dateOfBirth = dateOfBirth;
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.model;
|
||||
|
||||
public class StudentLogin {
|
||||
private String userName;
|
||||
private String password;
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.service;
|
||||
|
||||
import com.baeldung.spring.mybatis.model.Student;
|
||||
|
||||
public interface StudentService {
|
||||
public boolean insertStudent(Student student);
|
||||
public Student getStudentByLogin(String userName, String password);
|
||||
public Student getStudentByUserName(String userName);
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.spring.mybatis.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.spring.mybatis.mappers.StudentMapper;
|
||||
import com.baeldung.spring.mybatis.model.Student;
|
||||
|
||||
@Service("studentService")
|
||||
public class StudentServiceImpl implements StudentService {
|
||||
|
||||
@Autowired
|
||||
private StudentMapper studentMapper;
|
||||
|
||||
@Transactional
|
||||
public boolean insertStudent(Student student) {
|
||||
boolean result=false;
|
||||
try{
|
||||
studentMapper.insertStudent(student);
|
||||
result = true;
|
||||
}
|
||||
catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Student getStudentByLogin(String userName, String password) {
|
||||
Student student = studentMapper.getStudentByUserName(userName);
|
||||
return student;
|
||||
}
|
||||
|
||||
public Student getStudentByUserName(String userName) {
|
||||
Student student = studentMapper.getStudentByUserName(userName);
|
||||
return student;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,54 +0,0 @@
|
|||
<?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"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
|
||||
<mvc:annotation-driven />
|
||||
<context:component-scan base-package="com.baeldung.spring.mybatis" />
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/jsp/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
<!-- <bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
||||
<property name="url" value="jdbc:mysql//localhost:3306/baeldung;" />
|
||||
<property name="username" value="root" />
|
||||
<property name="password" value="admin" />
|
||||
</bean> -->
|
||||
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
|
||||
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
||||
<property name="url" value="jdbc:mysql://localhost:3306/baeldung" />
|
||||
<property name="username" value="root" />
|
||||
<property name="password" value="admin" />
|
||||
</bean>
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager" />
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="typeAliasesPackage" value="com.baeldung.spring.mybatis.model" />
|
||||
<property name="mapperLocations" value="classpath*:com/baeldung/spring/mappers/*.xml" />
|
||||
</bean>
|
||||
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
|
||||
<constructor-arg index="0" ref="sqlSessionFactory" />
|
||||
</bean>
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<property name="basePackage" value="com.baeldung.spring.mybatis.mappers" />
|
||||
</bean>
|
||||
|
||||
<bean id="studentService" class="com.baeldung.spring.mybatis.service.StudentServiceImpl"></bean>
|
||||
|
||||
</beans>
|
|
@ -1,51 +0,0 @@
|
|||
<?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"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
|
||||
|
||||
<mvc:annotation-driven />
|
||||
<context:component-scan base-package="com.baeldung.spring.mybatis" />
|
||||
<bean
|
||||
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
|
||||
<property name="prefix" value="/WEB-INF/jsp/" />
|
||||
<property name="suffix" value=".jsp" />
|
||||
</bean>
|
||||
<!-- <bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
||||
<property name="url" value="jdbc:mysql//localhost:3306/baeldung;" />
|
||||
<property name="username" value="root" />
|
||||
<property name="password" value="admin" />
|
||||
</bean> -->
|
||||
|
||||
|
||||
<bean id="dataSource"
|
||||
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
|
||||
|
||||
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
||||
<property name="url" value="jdbc:mysql://localhost:3306/baeldung" />
|
||||
<property name="username" value="root" />
|
||||
<property name="password" value="admin" />
|
||||
</bean>
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager" />
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
</bean>
|
||||
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="typeAliasesPackage" value="com.baeldung.spring.mybatis.model" />
|
||||
<property name="mapperLocations" value="classpath*:com/baeldung/spring/mappers/*.xml" />
|
||||
</bean>
|
||||
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
|
||||
<constructor-arg index="0" ref="sqlSessionFactory" />
|
||||
</bean>
|
||||
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
|
||||
<property name="basePackage" value="com.baeldung.spring.mybatis.mappers" />
|
||||
</bean>
|
||||
</beans>
|
|
@ -1,36 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Login Failure</title>
|
||||
<link href="assets/css/bootstrap-united.css" rel="stylesheet" />
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 100%">
|
||||
<a href="login.html">Login</a> <a
|
||||
href="signup.html">Signup</a>
|
||||
</div>
|
||||
<div class="panel panel-danger">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Student Enrollment Login failure</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div class="alert alert-dismissable alert-danger">
|
||||
<button type="button" class="close" data-dismiss="alert">×</button>
|
||||
<strong>Oh snap!</strong> Something is wrong. Change a few things up
|
||||
and try submitting again.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div></div>
|
||||
<div></div>
|
||||
|
||||
<a class="btn btn-primary" href="<spring:url value="login.html"/>">Try
|
||||
again?</a>
|
||||
</body>
|
||||
</html>
|
|
@ -1,81 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
|
||||
<style>
|
||||
.error {
|
||||
color: #ff0000;
|
||||
font-size: 0.9em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.errorblock {
|
||||
color: #000;
|
||||
background-color: #ffEEEE;
|
||||
border: 3px solid #ff0000;
|
||||
padding: 8px;
|
||||
margin: 16px;
|
||||
}
|
||||
</style>
|
||||
<title>Student Login</title>
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 100%">
|
||||
<a href="login.html">Login</a> <a
|
||||
href="signup.html">Signup</a>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
<h1>Welcome to Online Student Enrollment Application</h1>
|
||||
<p>Login to explore the complete features!</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div></div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<fieldset>
|
||||
<legend>Student Enrollment Login Form</legend>
|
||||
<form:form id="myForm" method="post"
|
||||
class="bs-example form-horizontal" modelAttribute="studentLogin" action="login.html">
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="userNameInput" class="col-lg-3 control-label">User
|
||||
Name : </label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="userName" id="userNameInput" placeholder="User Name" /> <form:errors
|
||||
path="userName" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="passwordInput" class="col-lg-3 control-label">Password</label>
|
||||
</td>
|
||||
<td><form:input type="password" class="form-control"
|
||||
path="password" id="passwordInput" placeholder="Password" /> <form:errors
|
||||
path="password" cssClass="error" /></td>
|
||||
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<button class="btn btn-default">Cancel</button>
|
||||
|
||||
<button class="btn btn-primary">Login</button>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
||||
</form:form>
|
||||
</fieldset>
|
||||
</div>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,130 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Student Signup</title>
|
||||
|
||||
<style>
|
||||
.green {
|
||||
font-weight: bold;
|
||||
color: green;
|
||||
}
|
||||
|
||||
.message {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #ff0000;
|
||||
font-size: 0.9em;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.errorblock {
|
||||
color: #000;
|
||||
background-color: #ffEEEE;
|
||||
border: 3px solid #ff0000;
|
||||
padding: 8px;
|
||||
margin: 16px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div style="width: 100%">
|
||||
<a href="login.html">Login</a> <a
|
||||
href="signup.html">Signup</a>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
<h1>Welcome to Online Student Enrollment Application</h1>
|
||||
<p>Login to explore the complete features!</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div></div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<c:if test="${not empty message}">
|
||||
<div class="message green">${message}</div>
|
||||
</c:if>
|
||||
</div>
|
||||
|
||||
<div style="width: 100%;">
|
||||
<form:form id="myForm" method="post"
|
||||
class="bs-example form-horizontal" action="signup.html" modelAttribute="student">
|
||||
<fieldset>
|
||||
<legend>Student Enrollment Signup Form</legend>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="userNameInput" class="col-lg-3 control-label">User
|
||||
Name</label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="userName" id="userNameInput" placeholder="User Name" /> <form:errors
|
||||
path="userName" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="passwordInput" class="col-lg-3 control-label">Password</label>
|
||||
</td>
|
||||
<td><form:input type="password" class="form-control"
|
||||
path="password" id="passwordInput" placeholder="Password" /> <form:errors
|
||||
path="password" cssClass="error" /></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><label for="firstNameInput"
|
||||
class="col-lg-3 control-label">First Name</label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="firstName" id="firstNameInput" placeholder="First Name" />
|
||||
<form:errors path="firstName" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="lastNameInput" class="col-lg-3 control-label">Last
|
||||
Name</label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="lastName" id="lastNameInput" placeholder="Last Name" /> <form:errors
|
||||
path="lastName" cssClass="error" /></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><label for="dateOfBirthInput"
|
||||
class="col-lg-3 control-label">Date of Birth</label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="dateOfBirth" id="dateOfBirthInput"
|
||||
placeholder="Date of Birth" /> <form:errors path="dateOfBirth"
|
||||
cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="emailAddressInput"
|
||||
class="col-lg-3 control-label">Email Address</label></td>
|
||||
<td><form:input type="text" class="form-control"
|
||||
path="emailAddress" id="emailAddressInput"
|
||||
placeholder="Email Address" /> <form:errors path="emailAddress"
|
||||
cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2">
|
||||
<button class="btn btn-default">Cancel</button>
|
||||
|
||||
<button class="btn btn-primary" data-toggle="modal"
|
||||
data-target="#themodal">Submit</button>
|
||||
</td>
|
||||
|
||||
</tr>
|
||||
</table>
|
||||
</fieldset>
|
||||
</form:form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -1,35 +0,0 @@
|
|||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Login Success</title>
|
||||
<link href="assets/css/bootstrap-united.css" rel="stylesheet" />
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 100%">
|
||||
<a href="login.html">Login</a> <a
|
||||
href="signup.html">Signup</a>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-success">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">Student Enrollment Login success</h3>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<div class="alert alert-dismissable alert-success">
|
||||
<button type="button" class="close" data-dismiss="alert">×</button>
|
||||
<strong>Well done!</strong> You successfully logged-into the system.
|
||||
Now you can explore the complete features!
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div></div>
|
||||
<div></div>
|
||||
<a class="btn btn-primary" href="<spring:url value="login.html"/>">Login
|
||||
as different user?</a>
|
||||
</body>
|
||||
</html>
|
Binary file not shown.
|
@ -1,21 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
|
||||
|
||||
<servlet>
|
||||
<servlet-name>myBatisSpringServlet</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<init-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>/WEB-INF/conf/mybatis-spring.xml</param-value>
|
||||
</init-param>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>myBatisSpringServlet</servlet-name>
|
||||
<url-pattern>*.html</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<display-name>MyBatis-Spring Integration</display-name>
|
||||
</web-app>
|
|
@ -1,34 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<link href="assets/css/bootstrap-united.css" rel="stylesheet" />
|
||||
<link href="bootstrap/css/bootstrap-responsive.css" rel="stylesheet" />
|
||||
<style>
|
||||
body {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
background: url(assets/img/books.jpg);
|
||||
background-size: 1440px 800px;
|
||||
background-repeat: no-repeat;
|
||||
display: compact;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div style="width: 100%">
|
||||
<a href="login.html">Login</a> <a
|
||||
href="signup.html">Signup</a>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
<h1>Welcome to Online Student Enrollment Application</h1>
|
||||
<p>Login to explore the complete features!</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div></div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,79 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.baeldung.embedded</groupId>
|
||||
<artifactId>spring-rest-embedded-tomcat</artifactId>
|
||||
<name>spring-rest-embedded-tomcat</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${servlet.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.library}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat.embed</groupId>
|
||||
<artifactId>tomcat-embed-core</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>tomcat-jasper</artifactId>
|
||||
<version>${tomcat.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpcore</artifactId>
|
||||
<version>${httpcore.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>1.5.10.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jackson.library>2.9.2</jackson.library>
|
||||
<servlet.version>4.0.0</servlet.version>
|
||||
<tomcat.version>9.0.1</tomcat.version>
|
||||
<httpclient.version>4.5.3</httpclient.version>
|
||||
<httpcore.version>4.4.8</httpcore.version>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,22 +0,0 @@
|
|||
package org.baeldung.embedded.configuration;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return new Class[] { UserConfiguration.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
package org.baeldung.embedded.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = "org.baeldung.embedded")
|
||||
public class UserConfiguration {
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package org.baeldung.embedded.controller;
|
||||
|
||||
import org.baeldung.embedded.domain.User;
|
||||
import org.baeldung.embedded.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String welcome() {
|
||||
return "Hello World!";
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/user/{userName}")
|
||||
@ResponseBody
|
||||
public User user(@PathVariable String userName) {
|
||||
return this.userService.getUser(userName);
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
package org.baeldung.embedded.domain;
|
||||
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private String hobby;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getHobby() {
|
||||
return hobby;
|
||||
}
|
||||
|
||||
public void setHobby(String hobby) {
|
||||
this.hobby = hobby;
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
package org.baeldung.embedded.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.embedded.domain.User;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
private List<User> users = new ArrayList<>();
|
||||
|
||||
public void addUser(String name) {
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
if (name == "HarryPotter") {
|
||||
user.setHobby("Quidditch");
|
||||
} else {
|
||||
user.setHobby("MuggleActivity");
|
||||
}
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
public User getUser(String name) {
|
||||
for (User user : users) {
|
||||
if (user.getName()
|
||||
.equalsIgnoreCase(name)) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.setName(name);
|
||||
user.setHobby("None");
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,18 +0,0 @@
|
|||
package org.baeldung;
|
||||
|
||||
import org.baeldung.embedded.configuration.UserConfiguration;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { UserConfiguration.class })
|
||||
@WebAppConfiguration
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package org.baeldung.embedded;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
|
||||
public class EmbeddedTomcatApp {
|
||||
|
||||
private Tomcat tomcatInstance;
|
||||
private WebApplicationContext webApplicationContext;
|
||||
private CountDownLatch started = new CountDownLatch(1);
|
||||
|
||||
public void start() throws Exception {
|
||||
tomcatInstance = new Tomcat();
|
||||
tomcatInstance.setBaseDir(new File(getClass().getResource(".")
|
||||
.toURI()).getAbsolutePath()); // Tomcat's temporary directory
|
||||
tomcatInstance.setPort(0);
|
||||
|
||||
Context webapp = tomcatInstance.addWebapp("", new File("src/main/webapp/").getAbsolutePath());
|
||||
|
||||
webapp.addLifecycleListener(event -> {
|
||||
if (event.getType()
|
||||
.equals("after_stop")) {
|
||||
started.countDown();
|
||||
} else if (event.getType()
|
||||
.equals("after_start")) {
|
||||
webApplicationContext = WebApplicationContextUtils
|
||||
.findWebApplicationContext(webapp.getServletContext());
|
||||
|
||||
((ConfigurableListableBeanFactory) webApplicationContext
|
||||
.getAutowireCapableBeanFactory()).registerSingleton("baseUrl", getBaseUrl());
|
||||
|
||||
started.countDown();
|
||||
}
|
||||
});
|
||||
|
||||
tomcatInstance.start();
|
||||
started.await();
|
||||
}
|
||||
|
||||
public Tomcat getTomcatInstance() {
|
||||
return this.tomcatInstance;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return String.format("http://localhost:%d%s", getLocalPort(), getWebApplicationContext().getServletContext()
|
||||
.getContextPath());
|
||||
}
|
||||
|
||||
public int getLocalPort() {
|
||||
return tomcatInstance.getConnector()
|
||||
.getLocalPort();
|
||||
}
|
||||
|
||||
public WebApplicationContext getWebApplicationContext() {
|
||||
return webApplicationContext;
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return started.getCount() == 0;
|
||||
}
|
||||
|
||||
public boolean isStartedSucessfully() {
|
||||
return webApplicationContext != null;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package org.baeldung.embedded;
|
||||
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public class EmbeddedTomcatRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
public EmbeddedTomcatRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
}
|
||||
|
||||
// use one static Tomcat instance shared across all tests
|
||||
private static EmbeddedTomcatApp embeddedTomcatApp = new EmbeddedTomcatApp();
|
||||
|
||||
@Override
|
||||
protected Statement classBlock(RunNotifier notifier) {
|
||||
ensureSharedTomcatStarted();
|
||||
Statement result = super.classBlock(notifier);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ensureSharedTomcatStarted() {
|
||||
if (!embeddedTomcatApp.isStarted()) {
|
||||
try {
|
||||
embeddedTomcatApp.start();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error while starting embedded Tomcat server", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createTest() throws Exception {
|
||||
if (!embeddedTomcatApp.isStartedSucessfully()) {
|
||||
throw new RuntimeException("Tomcat server not started successfully. Skipping test");
|
||||
}
|
||||
Object testInstance = super.createTest();
|
||||
embeddedTomcatApp.getWebApplicationContext()
|
||||
.getAutowireCapableBeanFactory()
|
||||
.autowireBean(testInstance);
|
||||
return testInstance;
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package org.baeldung.embedded;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.baeldung.embedded.service.UserService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
@RunWith(EmbeddedTomcatRunner.class)
|
||||
public class UserIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
protected String baseUrl;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
private String userName = "HarryPotter";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
userService.addUser(userName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserName_whenSendGetForHarryPotter_thenHobbyQuidditch() throws IOException {
|
||||
String url = baseUrl + "/user/" + userName;
|
||||
|
||||
HttpClient httpClient = HttpClientBuilder.create()
|
||||
.build();
|
||||
HttpGet getUserRequest = new HttpGet(url);
|
||||
getUserRequest.addHeader("Content-type", "application/json");
|
||||
HttpResponse response = httpClient.execute(getUserRequest);
|
||||
|
||||
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine()
|
||||
.getStatusCode());
|
||||
|
||||
HttpEntity responseEntity = response.getEntity();
|
||||
|
||||
Assert.assertNotNull(responseEntity);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String retSrc = EntityUtils.toString(responseEntity);
|
||||
Map<String, Object> result = mapper.readValue(retSrc, Map.class);
|
||||
|
||||
Assert.assertEquals("Quidditch", result.get("hobby"));
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.sse</groupId>
|
||||
<artifactId>sse-jaxrs</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>sse-jaxrs</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>sse-jaxrs-server</module>
|
||||
<module>sse-jaxrs-client</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
|
@ -1,62 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>sse-jaxrs-client</artifactId>
|
||||
<name>sse-jaxrs-client</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.sse</groupId>
|
||||
<artifactId>sse-jaxrs</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<cxf-version>3.2.0</cxf-version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>singleEvent</id>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.sse.jaxrs.client.SseClientApp</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>broadcast</id>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.sse.jaxrs.client.SseClientBroadcastApp</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-sse</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,48 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs.client;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.sse.InboundSseEvent;
|
||||
import javax.ws.rs.sse.SseEventSource;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class SseClientApp {
|
||||
|
||||
private static final String url = "http://127.0.0.1:9080/sse-jaxrs-server/sse/stock/prices";
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(url);
|
||||
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
|
||||
|
||||
eventSource.register(onEvent, onError, onComplete);
|
||||
eventSource.open();
|
||||
|
||||
//Consuming events for one hour
|
||||
Thread.sleep(60 * 60 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
client.close();
|
||||
System.out.println("End");
|
||||
}
|
||||
|
||||
// A new event is received
|
||||
private static Consumer<InboundSseEvent> onEvent = (inboundSseEvent) -> {
|
||||
String data = inboundSseEvent.readData();
|
||||
System.out.println(data);
|
||||
};
|
||||
|
||||
//Error
|
||||
private static Consumer<Throwable> onError = (throwable) -> {
|
||||
throwable.printStackTrace();
|
||||
};
|
||||
|
||||
//Connection close and there is nothing to receive
|
||||
private static Runnable onComplete = () -> {
|
||||
System.out.println("Done!");
|
||||
};
|
||||
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs.client;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.sse.InboundSseEvent;
|
||||
import javax.ws.rs.sse.SseEventSource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class SseClientBroadcastApp {
|
||||
|
||||
private static final String subscribeUrl = "http://localhost:9080/sse-jaxrs-server/sse/stock/subscribe";
|
||||
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(subscribeUrl);
|
||||
try (final SseEventSource eventSource = SseEventSource.target(target)
|
||||
.reconnectingEvery(5, TimeUnit.SECONDS)
|
||||
.build()) {
|
||||
eventSource.register(onEvent, onError, onComplete);
|
||||
eventSource.open();
|
||||
System.out.println("Wainting for incoming event ...");
|
||||
|
||||
//Consuming events for one hour
|
||||
Thread.sleep(60 * 60 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
client.close();
|
||||
System.out.println("End");
|
||||
}
|
||||
|
||||
// A new event is received
|
||||
private static Consumer<InboundSseEvent> onEvent = (inboundSseEvent) -> {
|
||||
String data = inboundSseEvent.readData();
|
||||
System.out.println(data);
|
||||
};
|
||||
|
||||
//Error
|
||||
private static Consumer<Throwable> onError = (throwable) -> {
|
||||
throwable.printStackTrace();
|
||||
};
|
||||
|
||||
//Connection close and there is nothing to receive
|
||||
private static Runnable onComplete = () -> {
|
||||
System.out.println("Done!");
|
||||
};
|
||||
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,85 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>sse-jaxrs-server</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<name>sse-jaxrs-server</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.sse</groupId>
|
||||
<artifactId>sse-jaxrs</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<liberty-maven-plugin.version>2.4.2</liberty-maven-plugin.version>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
<openliberty-version>18.0.0.2</openliberty-version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>${artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.wasdev.wlp.maven.plugins</groupId>
|
||||
<artifactId>liberty-maven-plugin</artifactId>
|
||||
<version>${liberty-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<assemblyArtifact>
|
||||
<groupId>io.openliberty</groupId>
|
||||
<artifactId>openliberty-webProfile8</artifactId>
|
||||
<version>${openliberty-version}</version>
|
||||
<type>zip</type>
|
||||
</assemblyArtifact>
|
||||
<installAppPackages>project</installAppPackages>
|
||||
<looseApplication>true</looseApplication>
|
||||
<configFile>src/main/liberty/config/server.xml</configFile>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install-server</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>install-server</goal>
|
||||
<goal>create-server</goal>
|
||||
<goal>install-feature</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>install-apps</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>install-apps</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.ws.rs</groupId>
|
||||
<artifactId>javax.ws.rs-api</artifactId>
|
||||
<version>2.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.enterprise</groupId>
|
||||
<artifactId>cdi-api</artifactId>
|
||||
<version>2.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.json.bind</groupId>
|
||||
<artifactId>javax.json.bind-api</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,8 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs;
|
||||
|
||||
import javax.ws.rs.ApplicationPath;
|
||||
import javax.ws.rs.core.Application;
|
||||
|
||||
@ApplicationPath("sse")
|
||||
public class AppConfig extends Application {
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.sse.OutboundSseEvent;
|
||||
import javax.ws.rs.sse.Sse;
|
||||
import javax.ws.rs.sse.SseBroadcaster;
|
||||
import javax.ws.rs.sse.SseEventSink;
|
||||
|
||||
@ApplicationScoped
|
||||
@Path("stock")
|
||||
public class SseResource {
|
||||
|
||||
@Inject
|
||||
private StockService stockService;
|
||||
|
||||
private Sse sse;
|
||||
private SseBroadcaster sseBroadcaster;
|
||||
private OutboundSseEvent.Builder eventBuilder;
|
||||
|
||||
@Context
|
||||
public void setSse(Sse sse) {
|
||||
this.sse = sse;
|
||||
this.eventBuilder = sse.newEventBuilder();
|
||||
this.sseBroadcaster = sse.newBroadcaster();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("prices")
|
||||
@Produces("text/event-stream")
|
||||
public void getStockPrices(@Context SseEventSink sseEventSink,
|
||||
@HeaderParam(HttpHeaders.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastReceivedId) {
|
||||
|
||||
int lastEventId = 1;
|
||||
if (lastReceivedId != -1) {
|
||||
lastEventId = ++lastReceivedId;
|
||||
}
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
Stock stock = stockService.getNextTransaction(lastEventId);
|
||||
if (stock != null) {
|
||||
OutboundSseEvent sseEvent = this.eventBuilder
|
||||
.name("stock")
|
||||
.id(String.valueOf(lastEventId))
|
||||
.mediaType(MediaType.APPLICATION_JSON_TYPE)
|
||||
.data(Stock.class, stock)
|
||||
.reconnectDelay(3000)
|
||||
.comment("price change")
|
||||
.build();
|
||||
sseEventSink.send(sseEvent);
|
||||
lastEventId++;
|
||||
}
|
||||
//Simulate connection close
|
||||
if (lastEventId % 5 == 0) {
|
||||
sseEventSink.close();
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
//Wait 5 seconds
|
||||
Thread.sleep(5 * 1000);
|
||||
} catch (InterruptedException ex) {
|
||||
// ...
|
||||
}
|
||||
//Simulatae a while boucle break
|
||||
running = lastEventId <= 2000;
|
||||
}
|
||||
sseEventSink.close();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("subscribe")
|
||||
@Produces(MediaType.SERVER_SENT_EVENTS)
|
||||
public void listen(@Context SseEventSink sseEventSink) {
|
||||
sseEventSink.send(sse.newEvent("Welcome !"));
|
||||
this.sseBroadcaster.register(sseEventSink);
|
||||
sseEventSink.send(sse.newEvent("You are registred !"));
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("publish")
|
||||
public void broadcast() {
|
||||
Runnable r = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int lastEventId = 0;
|
||||
boolean running = true;
|
||||
while (running) {
|
||||
lastEventId++;
|
||||
Stock stock = stockService.getNextTransaction(lastEventId);
|
||||
if (stock != null) {
|
||||
OutboundSseEvent sseEvent = eventBuilder
|
||||
.name("stock")
|
||||
.id(String.valueOf(lastEventId))
|
||||
.mediaType(MediaType.APPLICATION_JSON_TYPE)
|
||||
.data(Stock.class, stock)
|
||||
.reconnectDelay(3000)
|
||||
.comment("price change")
|
||||
.build();
|
||||
sseBroadcaster.broadcast(sseEvent);
|
||||
}
|
||||
try {
|
||||
//Wait 5 seconds
|
||||
Thread.currentThread().sleep(5 * 1000);
|
||||
} catch (InterruptedException ex) {
|
||||
// ...
|
||||
}
|
||||
//Simulatae a while boucle break
|
||||
running = lastEventId <= 2000;
|
||||
}
|
||||
}
|
||||
};
|
||||
new Thread(r).start();
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class Stock {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private BigDecimal price;
|
||||
LocalDateTime dateTime;
|
||||
|
||||
public Stock(Integer id, String name, BigDecimal price, LocalDateTime dateTime) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.dateTime = dateTime;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public LocalDateTime getDateTime() {
|
||||
return dateTime;
|
||||
}
|
||||
|
||||
public void setDateTime(LocalDateTime dateTime) {
|
||||
this.dateTime = dateTime;
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package com.baeldung.sse.jaxrs;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Event;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@ApplicationScoped
|
||||
@Named
|
||||
public class StockService {
|
||||
|
||||
private static final BigDecimal UP = BigDecimal.valueOf(1.05f);
|
||||
private static final BigDecimal DOWN = BigDecimal.valueOf(0.95f);
|
||||
|
||||
List<String> stockNames = Arrays.asList("GOOG", "IBM", "MS", "GOOG", "YAHO");
|
||||
List<Stock> stocksDB = new ArrayList<>();
|
||||
private AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
public void init(@Observes @Initialized(ApplicationScoped.class) Object init) {
|
||||
//Open price
|
||||
System.out.println("@Start Init ...");
|
||||
stockNames.forEach(stockName -> {
|
||||
stocksDB.add(new Stock(counter.incrementAndGet(), stockName, generateOpenPrice(), LocalDateTime.now()));
|
||||
});
|
||||
|
||||
Runnable runnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
//Simulate Change price and put every x seconds
|
||||
while (true) {
|
||||
int indx = new Random().nextInt(stockNames.size());
|
||||
String stockName = stockNames.get(indx);
|
||||
BigDecimal price = getLastPrice(stockName);
|
||||
BigDecimal newprice = changePrice(price);
|
||||
Stock stock = new Stock(counter.incrementAndGet(), stockName, newprice, LocalDateTime.now());
|
||||
stocksDB.add(stock);
|
||||
|
||||
int r = new Random().nextInt(30);
|
||||
try {
|
||||
Thread.currentThread().sleep(r*1000);
|
||||
} catch (InterruptedException ex) {
|
||||
// ...
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
new Thread(runnable).start();
|
||||
System.out.println("@End Init ...");
|
||||
}
|
||||
|
||||
public Stock getNextTransaction(Integer lastEventId) {
|
||||
return stocksDB.stream().filter(s -> s.getId().equals(lastEventId)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
BigDecimal generateOpenPrice() {
|
||||
float min = 70;
|
||||
float max = 120;
|
||||
return BigDecimal.valueOf(min + new Random().nextFloat() * (max - min)).setScale(4,RoundingMode.CEILING);
|
||||
}
|
||||
|
||||
BigDecimal changePrice(BigDecimal price) {
|
||||
return Math.random() >= 0.5 ? price.multiply(UP).setScale(4,RoundingMode.CEILING) : price.multiply(DOWN).setScale(4,RoundingMode.CEILING);
|
||||
}
|
||||
|
||||
private BigDecimal getLastPrice(String stockName) {
|
||||
return stocksDB.stream().filter(stock -> stock.getName().equals(stockName)).findFirst().get().getPrice();
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
<server description="OpenLiberty Server">
|
||||
<featureManager>
|
||||
<feature>jaxrs-2.1</feature>
|
||||
<feature>cdi-2.0</feature>
|
||||
</featureManager>
|
||||
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint" host="*"/>
|
||||
</server>
|
|
@ -1,6 +0,0 @@
|
|||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
|
||||
bean-discovery-mode="all">
|
||||
</beans>
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,11 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
|
||||
version="4.0">
|
||||
<display-name>Hello Servlet</display-name>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
|
@ -1 +0,0 @@
|
|||
index
|
|
@ -1,38 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Server-Sent Event Broadcasting</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Stock prices :</h2>
|
||||
<div>
|
||||
<ul id="data">
|
||||
</ul>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
var source = new EventSource('sse/stock/subscribe');
|
||||
source.onopen = function(event) {
|
||||
console.log(event);
|
||||
};
|
||||
source.addEventListener("stock", function(event) {
|
||||
append(event.data);
|
||||
}, false);
|
||||
|
||||
source.onmessage = function(event) {
|
||||
append(event.data);
|
||||
};
|
||||
source.onerror = function(event) {
|
||||
console.log(event);
|
||||
};
|
||||
|
||||
function append(data) {
|
||||
var ul = document.getElementById("data");
|
||||
var li = document.createElement("li");
|
||||
li.appendChild(document.createTextNode(data));
|
||||
ul.insertBefore(li, ul.childNodes[0]);
|
||||
};
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -1,38 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Server-Sent Event</title>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Stock prices :</h2>
|
||||
<div>
|
||||
<ul id="data">
|
||||
</ul>
|
||||
</div>
|
||||
<script>
|
||||
|
||||
var source = new EventSource('sse/stock/prices');
|
||||
source.onopen = function(event) {
|
||||
console.log(event);
|
||||
};
|
||||
source.addEventListener("stock", function(event) {
|
||||
append(event.data);
|
||||
}, false);
|
||||
|
||||
source.onmessage = function(event) {
|
||||
append(event.data);
|
||||
};
|
||||
source.onerror = function(event) {
|
||||
console.log(event);
|
||||
};
|
||||
|
||||
function append(data) {
|
||||
var ul = document.getElementById("data");
|
||||
var li = document.createElement("li");
|
||||
li.appendChild(document.createTextNode(data));
|
||||
ul.insertBefore(li, ul.childNodes[0]);
|
||||
};
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue