diff --git a/pom.xml b/pom.xml index f34c1936df..92794d33e2 100644 --- a/pom.xml +++ b/pom.xml @@ -14,21 +14,14 @@ org.springframework.boot spring-boot-starter-parent - 2.1.2.RELEASE + 3.0.5 - UTF-8 - UTF-8 - 1.8 + 20 - - - org.springframework.boot - spring-boot-starter-actuator - org.springframework.boot spring-boot-starter-data-jpa @@ -63,43 +56,4 @@ - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - diff --git a/spring-6-rest-mvc/.gitignore b/spring-6-rest-mvc/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/spring-6-rest-mvc/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.jar b/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000000..bf82ff01c6 Binary files /dev/null and b/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.jar differ diff --git a/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.properties b/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000..ca5ab4bab1 --- /dev/null +++ b/spring-6-rest-mvc/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# 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 +# +# https://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. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar diff --git a/spring-6-rest-mvc/mvnw b/spring-6-rest-mvc/mvnw new file mode 100644 index 0000000000..8a8fb2282d --- /dev/null +++ b/spring-6-rest-mvc/mvnw @@ -0,0 +1,316 @@ +#!/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 +# +# https://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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven 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 /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + 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)`" +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="`\\unset -f command; \\command -v 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 + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +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 + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring-6-rest-mvc/mvnw.cmd b/spring-6-rest-mvc/mvnw.cmd new file mode 100644 index 0000000000..1d8ab018ea --- /dev/null +++ b/spring-6-rest-mvc/mvnw.cmd @@ -0,0 +1,188 @@ +@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 https://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 Maven 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 keystroke 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 by 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 "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\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 + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%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 "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\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% + +cmd /C exit /B %ERROR_CODE% diff --git a/spring-6-rest-mvc/pom.xml b/spring-6-rest-mvc/pom.xml new file mode 100644 index 0000000000..f204a315f1 --- /dev/null +++ b/spring-6-rest-mvc/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.6 + + + guru.sprinframework + spring-6-rest-mvc + 0.0.1-SNAPSHOT + spring-6-rest-mvc + spring-6-rest-mvc + + 20 + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplication.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplication.java new file mode 100644 index 0000000000..8dfdf99038 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplication.java @@ -0,0 +1,13 @@ +package guru.sprinframework.spring6restmvc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Spring6RestMvcApplication { + + public static void main(String[] args) { + SpringApplication.run(Spring6RestMvcApplication.class, args); + } + +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/BeerController.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/BeerController.java new file mode 100644 index 0000000000..acf74e283a --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/BeerController.java @@ -0,0 +1,77 @@ +package guru.sprinframework.spring6restmvc.controller; + +import guru.sprinframework.spring6restmvc.model.Beer; +import guru.sprinframework.spring6restmvc.service.BeerService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.RequestEntity; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@Slf4j +@AllArgsConstructor +@RestController +@RequestMapping("/api/v1/beer") +public class BeerController { + + private final BeerService beerService; + + + @PatchMapping("{beerId}") + public ResponseEntity updateBeerPatchById (@PathVariable("beerId") UUID beerId, @RequestBody Beer beer){ + + beerService.patchBeerById(beerId, beer); + + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @DeleteMapping("{beerId}") + public ResponseEntity deleteById(@PathVariable("beerId") UUID beerId){ + + beerService.deleteById(beerId); + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @PutMapping("{beerId}") + public ResponseEntity updateById (@PathVariable("beerId") UUID beerId, @RequestBody Beer beer){ + + beerService.updateBeerById(beerId, beer); + + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + // @RequestMapping(method = RequestMethod.POST) + @PostMapping + public ResponseEntity handlePost(@RequestBody Beer beer){ + + Beer savedBeer = beerService.saveNewBeer(beer); + + HttpHeaders headers = new HttpHeaders(); + headers.add("Location", "api/v1/beer/" + savedBeer.getId().toString()); + + return new ResponseEntity(headers, HttpStatus.CREATED); + +} + + @RequestMapping(method = RequestMethod.GET) + public List listBeers(){ + return beerService.listBeer(); + + } + + @RequestMapping(value="/{beerId}", method = RequestMethod.GET) + public Beer getBeerById(@PathVariable("beerId") UUID beerId){ + + log.debug("Get Beer by Id - in controller"); + + return beerService.getBeerById(beerId); + } + +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/CustomerController.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/CustomerController.java new file mode 100644 index 0000000000..0bff324ed8 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/controller/CustomerController.java @@ -0,0 +1,71 @@ +package guru.sprinframework.spring6restmvc.controller; + +import guru.sprinframework.spring6restmvc.model.Beer; +import guru.sprinframework.spring6restmvc.model.Customer; +import guru.sprinframework.spring6restmvc.service.CustomerService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.UUID; + +@Slf4j +@AllArgsConstructor +@RestController +@RequestMapping("/api/v1/customer") +public class CustomerController { + + private final CustomerService customerService; + + @PatchMapping("{customerId}") + public ResponseEntity updateCustomerPatchById (@PathVariable("customerId") UUID customerId, @RequestBody Customer customer){ + + customerService.patchCustomerById(customerId, customer); + + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @DeleteMapping("{customerId}") + public ResponseEntity deleteById(@PathVariable("customerId") UUID customerId){ + + customerService.deleteById(customerId); + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @PutMapping("{customerId}") + public ResponseEntity updateById (@PathVariable("customerId") UUID customerId, @RequestBody Customer customer){ + + customerService.updateCustomerById(customerId, customer); + + return new ResponseEntity(HttpStatus.NO_CONTENT); + } + + @PostMapping + public ResponseEntity handlePost(@RequestBody Customer customer){ + + Customer savedCustomer = customerService.saveNewCustomer(customer); + + HttpHeaders headers = new HttpHeaders(); + headers.add("Location", "api/v1/customer/" + savedCustomer.getId().toString()); + + return new ResponseEntity(headers, HttpStatus.CREATED); + + } + + @RequestMapping(method = RequestMethod.GET) + public List customerList(){ + return customerService.customerList(); + } + @RequestMapping(value = "/{customerId}", method = RequestMethod.GET) + public Customer getCustomerById (@PathVariable("customerId")UUID customerId){ + log.debug("Get Customer by Id - in controller 042 "); + + return customerService.getCustomerById(customerId); + + } + +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Beer.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Beer.java new file mode 100644 index 0000000000..176958f178 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Beer.java @@ -0,0 +1,244 @@ +package guru.sprinframework.spring6restmvc.model; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.UUID; + +public class Beer { + private UUID id; + private Integer version; + private String beerName; + private BeerStyle beerStyle; + private String upc; + private Integer quantityOnHand; + private BigDecimal price; + private LocalDateTime createdDate; + private LocalDateTime updateDate; + + Beer(UUID id, Integer version, String beerName, BeerStyle beerStyle, String upc, Integer quantityOnHand, BigDecimal price, LocalDateTime createdDate, LocalDateTime updateDate) { + this.id = id; + this.version = version; + this.beerName = beerName; + this.beerStyle = beerStyle; + this.upc = upc; + this.quantityOnHand = quantityOnHand; + this.price = price; + this.createdDate = createdDate; + this.updateDate = updateDate; + } + + public static BeerBuilder builder() { + return new BeerBuilder(); + } + + public UUID getId() { + return this.id; + } + + public Integer getVersion() { + return this.version; + } + + public String getBeerName() { + return this.beerName; + } + + public BeerStyle getBeerStyle() { + return this.beerStyle; + } + + public String getUpc() { + return this.upc; + } + + public Integer getQuantityOnHand() { + return this.quantityOnHand; + } + + public BigDecimal getPrice() { + return this.price; + } + + public LocalDateTime getCreatedDate() { + return this.createdDate; + } + + public LocalDateTime getUpdateDate() { + return this.updateDate; + } + + public void setId(UUID id) { + this.id = id; + } + + public void setVersion(Integer version) { + this.version = version; + } + + public void setBeerName(String beerName) { + this.beerName = beerName; + } + + public void setBeerStyle(BeerStyle beerStyle) { + this.beerStyle = beerStyle; + } + + public void setUpc(String upc) { + this.upc = upc; + } + + public void setQuantityOnHand(Integer quantityOnHand) { + this.quantityOnHand = quantityOnHand; + } + + public void setPrice(BigDecimal price) { + this.price = price; + } + + public void setCreatedDate(LocalDateTime createdDate) { + this.createdDate = createdDate; + } + + public void setUpdateDate(LocalDateTime updateDate) { + this.updateDate = updateDate; + } + + public boolean equals(final Object o) { + if (o == this) return true; + if (!(o instanceof Beer)) return false; + final Beer other = (Beer) o; + if (!other.canEqual((Object) this)) return false; + final Object this$id = this.getId(); + final Object other$id = other.getId(); + if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false; + final Object this$version = this.getVersion(); + final Object other$version = other.getVersion(); + if (this$version == null ? other$version != null : !this$version.equals(other$version)) return false; + final Object this$beerName = this.getBeerName(); + final Object other$beerName = other.getBeerName(); + if (this$beerName == null ? other$beerName != null : !this$beerName.equals(other$beerName)) return false; + final Object this$beerStyle = this.getBeerStyle(); + final Object other$beerStyle = other.getBeerStyle(); + if (this$beerStyle == null ? other$beerStyle != null : !this$beerStyle.equals(other$beerStyle)) return false; + final Object this$upc = this.getUpc(); + final Object other$upc = other.getUpc(); + if (this$upc == null ? other$upc != null : !this$upc.equals(other$upc)) return false; + final Object this$quantityOnHand = this.getQuantityOnHand(); + final Object other$quantityOnHand = other.getQuantityOnHand(); + if (this$quantityOnHand == null ? other$quantityOnHand != null : !this$quantityOnHand.equals(other$quantityOnHand)) + return false; + final Object this$price = this.getPrice(); + final Object other$price = other.getPrice(); + if (this$price == null ? other$price != null : !this$price.equals(other$price)) return false; + final Object this$createdDate = this.getCreatedDate(); + final Object other$createdDate = other.getCreatedDate(); + if (this$createdDate == null ? other$createdDate != null : !this$createdDate.equals(other$createdDate)) + return false; + final Object this$updateDate = this.getUpdateDate(); + final Object other$updateDate = other.getUpdateDate(); + if (this$updateDate == null ? other$updateDate != null : !this$updateDate.equals(other$updateDate)) + return false; + return true; + } + + protected boolean canEqual(final Object other) { + return other instanceof Beer; + } + + public int hashCode() { + final int PRIME = 59; + int result = 1; + final Object $id = this.getId(); + result = result * PRIME + ($id == null ? 43 : $id.hashCode()); + final Object $version = this.getVersion(); + result = result * PRIME + ($version == null ? 43 : $version.hashCode()); + final Object $beerName = this.getBeerName(); + result = result * PRIME + ($beerName == null ? 43 : $beerName.hashCode()); + final Object $beerStyle = this.getBeerStyle(); + result = result * PRIME + ($beerStyle == null ? 43 : $beerStyle.hashCode()); + final Object $upc = this.getUpc(); + result = result * PRIME + ($upc == null ? 43 : $upc.hashCode()); + final Object $quantityOnHand = this.getQuantityOnHand(); + result = result * PRIME + ($quantityOnHand == null ? 43 : $quantityOnHand.hashCode()); + final Object $price = this.getPrice(); + result = result * PRIME + ($price == null ? 43 : $price.hashCode()); + final Object $createdDate = this.getCreatedDate(); + result = result * PRIME + ($createdDate == null ? 43 : $createdDate.hashCode()); + final Object $updateDate = this.getUpdateDate(); + result = result * PRIME + ($updateDate == null ? 43 : $updateDate.hashCode()); + return result; + } + + public String toString() { + return "Beer(id=" + this.getId() + ", version=" + this.getVersion() + ", beerName=" + this.getBeerName() + ", beerStyle=" + this.getBeerStyle() + ", upc=" + this.getUpc() + ", quantityOnHand=" + this.getQuantityOnHand() + ", price=" + this.getPrice() + ", createdDate=" + this.getCreatedDate() + ", updateDate=" + this.getUpdateDate() + ")"; + } + + + public static class BeerBuilder { + private UUID id; + private Integer version; + private String beerName; + private BeerStyle beerStyle; + private String upc; + private Integer quantityOnHand; + private BigDecimal price; + private LocalDateTime createdDate; + private LocalDateTime updateDate; + + BeerBuilder() { + } + + public BeerBuilder id(UUID id) { + this.id = id; + return this; + } + + public BeerBuilder version(Integer version) { + this.version = version; + return this; + } + + public BeerBuilder beerName(String beerName) { + this.beerName = beerName; + return this; + } + + public BeerBuilder beerStyle(BeerStyle beerStyle) { + this.beerStyle = beerStyle; + return this; + } + + public BeerBuilder upc(String upc) { + this.upc = upc; + return this; + } + + public BeerBuilder quantityOnHand(Integer quantityOnHand) { + this.quantityOnHand = quantityOnHand; + return this; + } + + public BeerBuilder price(BigDecimal price) { + this.price = price; + return this; + } + + public BeerBuilder createdDate(LocalDateTime createdDate) { + this.createdDate = createdDate; + return this; + } + + public BeerBuilder updateDate(LocalDateTime updateDate) { + this.updateDate = updateDate; + return this; + } + + public Beer build() { + return new Beer(id, version, beerName, beerStyle, upc, quantityOnHand, price, createdDate, updateDate); + } + + public String toString() { + return "Beer.BeerBuilder(id=" + this.id + ", version=" + this.version + ", beerName=" + this.beerName + ", beerStyle=" + this.beerStyle + ", upc=" + this.upc + ", quantityOnHand=" + this.quantityOnHand + ", price=" + this.price + ", createdDate=" + this.createdDate + ", updateDate=" + this.updateDate + ")"; + } + } +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/BeerStyle.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/BeerStyle.java new file mode 100644 index 0000000000..5d8a6d1ea3 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/BeerStyle.java @@ -0,0 +1,6 @@ +package guru.sprinframework.spring6restmvc.model; + + +public enum BeerStyle { + LAGER, PILSNER, STOUT, GOSE, PORTER, ALE, WHEAT, IPA, PALE_ALE, SAISON +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Customer.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Customer.java new file mode 100644 index 0000000000..f85f003594 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/model/Customer.java @@ -0,0 +1,23 @@ +package guru.sprinframework.spring6restmvc.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Builder +@Slf4j +@AllArgsConstructor +@Data +public class Customer { + private String customerName; + private UUID id; + private Integer version; + private LocalDateTime createdDate; + private LocalDateTime lastModifiedDate; + + +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerService.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerService.java new file mode 100644 index 0000000000..07ca85b68b --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerService.java @@ -0,0 +1,20 @@ +package guru.sprinframework.spring6restmvc.service; + +import guru.sprinframework.spring6restmvc.model.Beer; + +import java.util.List; +import java.util.UUID; + +public interface BeerService { + List listBeer(); + + Beer getBeerById(UUID id); + + Beer saveNewBeer(Beer beer); + + void updateBeerById(UUID beerId, Beer beer); + + void deleteById(UUID beerId); + + void patchBeerById(UUID beerId, Beer beer); +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerServiceImpl.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerServiceImpl.java new file mode 100644 index 0000000000..5b096c1ba9 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/BeerServiceImpl.java @@ -0,0 +1,148 @@ +package guru.sprinframework.spring6restmvc.service; + +import guru.sprinframework.spring6restmvc.model.Beer; +import guru.sprinframework.spring6restmvc.model.BeerStyle; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.util.*; + +@Slf4j // log in annotation +@Service +public class BeerServiceImpl implements BeerService { + private Map beerMap; + + public BeerServiceImpl() { + this.beerMap = new HashMap<>(); + + Beer beer1 = Beer.builder() + .id(UUID.randomUUID()) + .version(1) + .beerName("Heini") + .beerStyle(BeerStyle.LAGER) + .upc("12335") + .price(new BigDecimal(12.19)) + .quantityOnHand(122) + .createdDate(LocalDateTime.now()) + .updateDate(LocalDateTime.now()) + .build(); + + Beer beer2 = Beer.builder() + .id(UUID.randomUUID()) + .version(1) + .beerName("Becks") + .beerStyle(BeerStyle.LAGER) + .upc("53226") + .price(new BigDecimal(10.19)) + .quantityOnHand(142) + .createdDate(LocalDateTime.now()) + .updateDate(LocalDateTime.now()) + .build(); + + Beer beer3 = Beer.builder() + .id(UUID.randomUUID()) + .version(1) + .beerName("Zuja") + .beerStyle(BeerStyle.LAGER) + .upc("56163") + .price(new BigDecimal(9.19)) + .quantityOnHand(92) + .createdDate(LocalDateTime.now()) + .updateDate(LocalDateTime.now()) + .build(); + + Beer beer4 = Beer.builder() + .id(UUID.randomUUID()) + .version(1) + .beerName("Karlo") + .beerStyle(BeerStyle.LAGER) + .upc("879321") + .price(new BigDecimal(8.19)) + .quantityOnHand(182) + .createdDate(LocalDateTime.now()) + .updateDate(LocalDateTime.now()) + .build(); + + beerMap.put(beer1.getId(), beer1); + beerMap.put(beer2.getId(), beer2); + beerMap.put(beer3.getId(), beer3); + beerMap.put(beer4.getId(), beer4); + + } + + @Override + public List listBeer(){ + return new ArrayList<>(beerMap.values()); + } + @Override + public Beer getBeerById(UUID id) { + + log.debug("Get Beer Id - in service. Id: " + id.toString()); + + + return beerMap.get(id); + } + + @Override + public Beer saveNewBeer(Beer beer) { + Beer savedBeer = Beer.builder() + .id(UUID.randomUUID()) + .version(1) + .createdDate(LocalDateTime.now()) + .updateDate(LocalDateTime.now()) + .beerName(beer.getBeerName()) + .beerStyle(beer.getBeerStyle()) + .quantityOnHand(beer.getQuantityOnHand()) + .upc(beer.getUpc()) + .price(beer.getPrice()) + .build(); + + beerMap.put(savedBeer.getId(),savedBeer); + + return savedBeer; + } + + @Override + public void updateBeerById(UUID beerId, Beer beer) { + + Beer existingBeer = beerMap.get(beerId); + existingBeer.setBeerName(beer.getBeerName()); + existingBeer.setBeerStyle(beer.getBeerStyle()); + existingBeer.setPrice(beer.getPrice()); + existingBeer.setQuantityOnHand(beer.getQuantityOnHand()); + existingBeer.setUpc(beer.getUpc()); + + + } + + @Override + public void deleteById(UUID beerId) { + beerMap.remove(beerId); + } + + @Override + public void patchBeerById(UUID beerId, Beer beer) { + + Beer existingBeer = beerMap.get(beerId); + + if (StringUtils.hasText(beer.getBeerName())) { + existingBeer.setBeerName(beer.getBeerName()); + } + if (beer.getBeerStyle() != null) { + existingBeer.setBeerStyle(beer.getBeerStyle()); + } + if (beer.getPrice() != null) { + existingBeer.setPrice(beer.getPrice()); + } + if (beer.getQuantityOnHand() != null) { + existingBeer.setQuantityOnHand(beer.getQuantityOnHand()); + } + if (StringUtils.hasText(beer.getUpc())) { + existingBeer.setUpc(beer.getUpc()); + } + + } +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerService.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerService.java new file mode 100644 index 0000000000..ecc70bcaed --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerService.java @@ -0,0 +1,22 @@ +package guru.sprinframework.spring6restmvc.service; + +import guru.sprinframework.spring6restmvc.model.Customer; + +import java.util.List; +import java.util.UUID; + +public interface CustomerService { + + List customerList(); + + Customer getCustomerById(UUID id); + + Customer saveNewCustomer(Customer customer); + + void updateCustomerById(UUID customerId, Customer customer); + + void deleteById(UUID customerId); + + + void patchCustomerById(UUID customerId, Customer customer); +} diff --git a/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerServiceImpl.java b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerServiceImpl.java new file mode 100644 index 0000000000..70e7d44b15 --- /dev/null +++ b/spring-6-rest-mvc/src/main/java/guru/sprinframework/spring6restmvc/service/CustomerServiceImpl.java @@ -0,0 +1,87 @@ +package guru.sprinframework.spring6restmvc.service; + +import guru.sprinframework.spring6restmvc.model.Customer; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.time.LocalDateTime; +import java.util.*; + +@Slf4j +@Service +public class CustomerServiceImpl implements CustomerService { + + private Map customerMap; + public CustomerServiceImpl(){ + this.customerMap = new HashMap<>(); + + Customer customer1 = Customer.builder() + .id(UUID.randomUUID()) + .customerName("Matej Gace") + .version(1) + .createdDate(LocalDateTime.now()) + .lastModifiedDate(LocalDateTime.now()) + .build(); + + Customer customer2 = Customer.builder() + .id(UUID.randomUUID()) + .customerName("Lana Pavic") + .version(2) + .createdDate(LocalDateTime.now()) + .lastModifiedDate(LocalDateTime.now()) + .build(); + + customerMap.put(customer1.getId(), customer1); + customerMap.put(customer2.getId(), customer2); + } + @Override + public List customerList(){ + return new ArrayList<>(customerMap.values()); + } + @Override + public Customer getCustomerById(UUID id) { + + log.debug("Get customer Id - in service. ID: " + id.toString()); + return customerMap.get(id); + } + + @Override + public Customer saveNewCustomer(Customer customer) { + Customer savedCustomer = Customer.builder() + .id(UUID.randomUUID()) + .version(1) + .createdDate(LocalDateTime.now()) + .lastModifiedDate(LocalDateTime.now()) + .customerName(customer.getCustomerName()) + .build(); + + customerMap.put(savedCustomer.getId(), savedCustomer); + return savedCustomer; + } + + @Override + public void updateCustomerById(UUID customerId, Customer customer) { + + Customer existingCustomer = customerMap.get(customerId); + existingCustomer.setCustomerName(customer.getCustomerName()); + + } + + @Override + public void deleteById(UUID customerId) { + customerMap.remove(customerId); + } + + @Override + public void patchCustomerById(UUID customerId, Customer customer) { + + Customer existingCustomer = customerMap.get(customerId); + + if (StringUtils.hasText(customer.getCustomerName())) { + existingCustomer.setCustomerName(customer.getCustomerName()); + } + } + + +} diff --git a/spring-6-rest-mvc/src/main/resources/application.properties b/spring-6-rest-mvc/src/main/resources/application.properties new file mode 100644 index 0000000000..ce3c61be7f --- /dev/null +++ b/spring-6-rest-mvc/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.guru.sprinframework=debug diff --git a/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplicationTests.java b/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplicationTests.java new file mode 100644 index 0000000000..29cfce909c --- /dev/null +++ b/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/Spring6RestMvcApplicationTests.java @@ -0,0 +1,13 @@ +package guru.sprinframework.spring6restmvc; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Spring6RestMvcApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/controller/BeerControllerTest.java b/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/controller/BeerControllerTest.java new file mode 100644 index 0000000000..5aed948e3f --- /dev/null +++ b/spring-6-rest-mvc/src/test/java/guru/sprinframework/spring6restmvc/controller/BeerControllerTest.java @@ -0,0 +1,23 @@ +package guru.sprinframework.spring6restmvc.controller; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class BeerControllerTest { + + @Autowired + BeerController beerController; + + @Test + void getBeerById() { + + System.out.println(beerController.getBeerById(UUID.randomUUID())); + } +} \ No newline at end of file diff --git a/spring6di/.gitignore b/spring6di/.gitignore new file mode 100644 index 0000000000..549e00a2a9 --- /dev/null +++ b/spring6di/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/spring6di/.mvn/wrapper/maven-wrapper.jar b/spring6di/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 0000000000..bf82ff01c6 Binary files /dev/null and b/spring6di/.mvn/wrapper/maven-wrapper.jar differ diff --git a/spring6di/.mvn/wrapper/maven-wrapper.properties b/spring6di/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000..ca5ab4bab1 --- /dev/null +++ b/spring6di/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# 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 +# +# https://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. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.7/apache-maven-3.8.7-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar diff --git a/spring6di/mvnw b/spring6di/mvnw new file mode 100644 index 0000000000..8a8fb2282d --- /dev/null +++ b/spring6di/mvnw @@ -0,0 +1,316 @@ +#!/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 +# +# https://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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven 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 /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + 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)`" +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="`\\unset -f command; \\command -v 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 + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found .mvn/wrapper/maven-wrapper.jar" + fi +else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." + fi + if [ -n "$MVNW_REPOURL" ]; then + jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + else + jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + fi + while IFS="=" read key value; do + case "$key" in (wrapperUrl) jarUrl="$value"; break ;; + esac + done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" + if [ "$MVNW_VERBOSE" = true ]; then + echo "Downloading from: $jarUrl" + fi + wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + if $cygwin; then + wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + fi + + if command -v wget > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found wget ... using wget" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + if [ "$MVNW_VERBOSE" = true ]; then + echo "Found curl ... using curl" + fi + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl -o "$wrapperJarPath" "$jarUrl" -f + else + curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f + fi + + else + if [ "$MVNW_VERBOSE" = true ]; then + echo "Falling back to using Java to download" + fi + javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaClass=`cygpath --path --windows "$javaClass"` + fi + if [ -e "$javaClass" ]; then + if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Compiling MavenWrapperDownloader.java ..." + fi + # Compiling the Java class + ("$JAVA_HOME/bin/javac" "$javaClass") + fi + if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then + # Running the downloader + if [ "$MVNW_VERBOSE" = true ]; then + echo " - Running MavenWrapperDownloader.java ..." + fi + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +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 + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring6di/mvnw.cmd b/spring6di/mvnw.cmd new file mode 100644 index 0000000000..1d8ab018ea --- /dev/null +++ b/spring6di/mvnw.cmd @@ -0,0 +1,188 @@ +@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 https://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 Maven 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 keystroke 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 by 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 "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\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 + +set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %DOWNLOAD_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%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 "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\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% + +cmd /C exit /B %ERROR_CODE% diff --git a/spring6di/pom.xml b/spring6di/pom.xml new file mode 100644 index 0000000000..20556ebcae --- /dev/null +++ b/spring6di/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.6 + + + com.springframework + Spring6di + 0.0.1-SNAPSHOT + Spring6di + Spring6di + + 20 + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring6di/src/main/java/com/springframework/spring6di/Spring6diApplication.java b/spring6di/src/main/java/com/springframework/spring6di/Spring6diApplication.java new file mode 100644 index 0000000000..b8c0492c16 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/Spring6diApplication.java @@ -0,0 +1,21 @@ +package com.springframework.spring6di; + +import com.springframework.spring6di.controllers.MyController; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +@SpringBootApplication +public class Spring6diApplication { + + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(Spring6diApplication.class, args); + + MyController controller = ctx.getBean(MyController.class); + + System.out.println("in Main method"); + + System.out.println(controller.sayHello()); + } + +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/ConstructorInjectedController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/ConstructorInjectedController.java new file mode 100644 index 0000000000..8ca1a4015d --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/ConstructorInjectedController.java @@ -0,0 +1,18 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingService; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + +@Controller +public class ConstructorInjectedController { + + private final GreetingService greetingService; + + public ConstructorInjectedController(@Qualifier("greetingServiceImpl") GreetingService greetingService) { + this.greetingService = greetingService; + } + public String sayHello() { + return greetingService.sayGreeting(); + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/EnvironmentController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/EnvironmentController.java new file mode 100644 index 0000000000..9bd74997e1 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/EnvironmentController.java @@ -0,0 +1,19 @@ +package com.springframework.spring6di.controllers; + + +import com.springframework.spring6di.services.EnvironmentService; +import org.springframework.stereotype.Controller; + +@Controller +public class EnvironmentController { + + private final EnvironmentService environmentService; + + public EnvironmentController(EnvironmentService environmentService) { + this.environmentService = environmentService; + } + + public String getEnvironment(){ + return "You are in " + environmentService.getEnv() + " Environment"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/MyController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/MyController.java new file mode 100644 index 0000000000..12b9fa335d --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/MyController.java @@ -0,0 +1,29 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingService; +import com.springframework.spring6di.services.GreetingServiceImpl; +import org.springframework.stereotype.Controller; + +@Controller +public class MyController { + + private final GreetingService greetingService; + + public MyController() { + this.greetingService = new GreetingServiceImpl(); + } + + public String sayHello(){ + System.out.println("I'm in the controller"); + + return greetingService.sayGreeting(); + } + + public void beforeInit(){ + System.out.println("## - Before Init - Called by Bean Post Processor"); + } + + public void afterInit(){ + System.out.println("## - After init called by Bean Post Processor"); + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/PropertyInjectedController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/PropertyInjectedController.java new file mode 100644 index 0000000000..6f4f2da98e --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/PropertyInjectedController.java @@ -0,0 +1,18 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + +@Controller +public class PropertyInjectedController { + + @Qualifier("propertyGreetingService") + @Autowired + GreetingService greetingService; + + public String sayHello(){ + return greetingService.sayGreeting(); + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/SetterInjectedController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/SetterInjectedController.java new file mode 100644 index 0000000000..b9b78ec238 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/SetterInjectedController.java @@ -0,0 +1,24 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingService; +import com.springframework.spring6di.services.GreetingServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + +@Controller +public class SetterInjectedController { + + + private GreetingService greetingService; + + @Qualifier("setterGreetingBean") + @Autowired + public void setGreetingService(GreetingService greetingService) { + this.greetingService = greetingService; + } + + public String sayHello() { + return greetingService.sayGreeting(); + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/controllers/i18n/Myi18NController.java b/spring6di/src/main/java/com/springframework/spring6di/controllers/i18n/Myi18NController.java new file mode 100644 index 0000000000..9b1c5fcdbb --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/controllers/i18n/Myi18NController.java @@ -0,0 +1,21 @@ +package com.springframework.spring6di.controllers.i18n; + +import com.springframework.spring6di.services.GreetingService; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; + + +@Controller +public class Myi18NController { + + private final GreetingService greetingService; + + public Myi18NController(@Qualifier("i18NService") GreetingService greetingService) { + this.greetingService = greetingService; + } + + public String sayHello(){ + return greetingService.sayGreeting(); + } + +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/EnvironmentService.java b/spring6di/src/main/java/com/springframework/spring6di/services/EnvironmentService.java new file mode 100644 index 0000000000..6819924b42 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/EnvironmentService.java @@ -0,0 +1,5 @@ +package com.springframework.spring6di.services; + +public interface EnvironmentService { + String getEnv(); +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/GreetingService.java b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingService.java new file mode 100644 index 0000000000..c089d12006 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingService.java @@ -0,0 +1,5 @@ +package com.springframework.spring6di.services; + +public interface GreetingService { + String sayGreeting(); +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceImpl.java b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceImpl.java new file mode 100644 index 0000000000..9a7b760d0f --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceImpl.java @@ -0,0 +1,11 @@ +package com.springframework.spring6di.services; + +import org.springframework.stereotype.Service; + +@Service +public class GreetingServiceImpl implements GreetingService { + @Override + public String sayGreeting() { + return "hello everybody from cacak"; + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePrimary.java b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePrimary.java new file mode 100644 index 0000000000..5001126d23 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePrimary.java @@ -0,0 +1,15 @@ +package com.springframework.spring6di.services; + +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Service; + +@Primary +@Service +public class GreetingServicePrimary implements GreetingService{ + + + @Override + public String sayGreeting() { + return "Hello Form the Primary Bean"; + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePropertyInjected.java b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePropertyInjected.java new file mode 100644 index 0000000000..e3791fb693 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServicePropertyInjected.java @@ -0,0 +1,11 @@ +package com.springframework.spring6di.services; + +import org.springframework.stereotype.Service; + +@Service("propertyGreetingService") +public class GreetingServicePropertyInjected implements GreetingService{ + @Override + public String sayGreeting() { + return "Friends dont let friends do property injection"; + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceSetterInjection.java b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceSetterInjection.java new file mode 100644 index 0000000000..fe0acf96b5 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/GreetingServiceSetterInjection.java @@ -0,0 +1,13 @@ +package com.springframework.spring6di.services; + +import org.springframework.stereotype.Service; + + +@Service("setterGreetingBean") +public class GreetingServiceSetterInjection implements GreetingService { + + @Override + public String sayGreeting() { + return "Hey I'm Setting a Greeting!!"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/LifeCycleDemoBean.java b/spring6di/src/main/java/com/springframework/spring6di/services/LifeCycleDemoBean.java new file mode 100644 index 0000000000..18d90f15b3 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/LifeCycleDemoBean.java @@ -0,0 +1,92 @@ +package com.springframework.spring6di.services; + + +import com.springframework.spring6di.controllers.MyController; +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class LifeCycleDemoBean implements InitializingBean, DisposableBean, BeanNameAware, + BeanFactoryAware, ApplicationContextAware, BeanPostProcessor { + + public LifeCycleDemoBean() { + System.out.println("## I'm in the LifeCycleBean Constructor ##"); + } + + private String javaVer; + + @Value("${java.specification.version}") + public void setJavaVer(String javaVer) { + this.javaVer = javaVer; + System.out.println("## 1 Properties Set. Java Ver: " + this.javaVer); + } + + @Override + public void setBeanName(String name) { + System.out.println("## 2 BeanNameAware My Bean Name is: " + name); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + System.out.println("## 3 BeanFactoryAware - Bean Factory has been set"); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + System.out.println("## 4 ApplicationContextAware - Application context has been set"); + } + + @PostConstruct + public void postConstruct() { + System.out.println("## 5 postConstruct The Post Construct annotated method has been called"); + } + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println("## 6 afterPropertiesSet Populate Properties The LifeCycleBean has its properties set!"); + } + + @PreDestroy + public void preDestroy() { + System.out.println("## 7 The @PreDestroy annotated method has been called"); + } + + @Override + public void destroy() throws Exception { + System.out.println("## 8 DisposableBean.destroy The Lifecycle bean has been terminated"); + } + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + System.out.println("## postProcessBeforeInitialization: " + beanName); + + if (bean instanceof MyController) { + MyController myController = (MyController) bean; + System.out.println("Calling before init"); + myController.beforeInit(); + } + + return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + System.out.println("## postProcessAfterInitialization: " + beanName); + + if (bean instanceof MyController) { + MyController myController = (MyController) bean; + System.out.println("Calling after init"); + myController.afterInit(); + } + + return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); + } + +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnglishGreetingService.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnglishGreetingService.java new file mode 100644 index 0000000000..078d285885 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnglishGreetingService.java @@ -0,0 +1,16 @@ +package com.springframework.spring6di.services.i18n; + + +import com.springframework.spring6di.services.GreetingService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + + +@Profile({"EN", "default"}) +@Service("i18NService") +public class EnglishGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Hello World - EN"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceDev.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceDev.java new file mode 100644 index 0000000000..a61311b0d5 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceDev.java @@ -0,0 +1,14 @@ +package com.springframework.spring6di.services.i18n; + +import com.springframework.spring6di.services.EnvironmentService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Profile({"dev", "default"}) +@Service +public class EnvironmentServiceDev implements EnvironmentService { + @Override + public String getEnv() { + return "dev"; + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceProd.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceProd.java new file mode 100644 index 0000000000..b8e822d407 --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceProd.java @@ -0,0 +1,14 @@ +package com.springframework.spring6di.services.i18n; + +import com.springframework.spring6di.services.EnvironmentService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Profile("prod") +@Service +public class EnvironmentServiceProd implements EnvironmentService { + @Override + public String getEnv() { + return "prod"; + } +} diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceQA.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceQA.java new file mode 100644 index 0000000000..7383d6395a --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceQA.java @@ -0,0 +1,15 @@ +package com.springframework.spring6di.services.i18n; + +import com.springframework.spring6di.services.EnvironmentService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Profile("qa") +@Service +public class EnvironmentServiceQA implements EnvironmentService { + + @Override + public String getEnv() { + return "qa"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceUat.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceUat.java new file mode 100644 index 0000000000..e5fdf4a31d --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/EnvironmentServiceUat.java @@ -0,0 +1,15 @@ +package com.springframework.spring6di.services.i18n; + + +import com.springframework.spring6di.services.EnvironmentService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Profile("uat") +@Service +public class EnvironmentServiceUat implements EnvironmentService { + @Override + public String getEnv() { + return "uat"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/java/com/springframework/spring6di/services/i18n/SpanishGreetingService.java b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/SpanishGreetingService.java new file mode 100644 index 0000000000..b2d7bf7dec --- /dev/null +++ b/spring6di/src/main/java/com/springframework/spring6di/services/i18n/SpanishGreetingService.java @@ -0,0 +1,14 @@ +package com.springframework.spring6di.services.i18n; + +import com.springframework.spring6di.services.GreetingService; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Profile("ES") +@Service("i18NService") +public class SpanishGreetingService implements GreetingService { + @Override + public String sayGreeting() { + return "Hola Mundo - ES"; + } +} \ No newline at end of file diff --git a/spring6di/src/main/resources/application.properties b/spring6di/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/spring6di/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring6di/src/test/java/com/springframework/spring6di/Spring6diApplicationTests.java b/spring6di/src/test/java/com/springframework/spring6di/Spring6diApplicationTests.java new file mode 100644 index 0000000000..5106d52c51 --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/Spring6diApplicationTests.java @@ -0,0 +1,32 @@ +package com.springframework.spring6di; + +import com.springframework.spring6di.controllers.MyController; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +@SpringBootTest +class Spring6diApplicationTests { + + @Autowired + ApplicationContext applicationContext; + + @Autowired + MyController myController; + + @Test + void testAutowireOfController(){ + System.out.println(myController.sayHello()); + } + @Test + void testControllerFromCtx(){ + MyController myController = applicationContext.getBean(MyController.class); + + System.out.println(myController.sayHello()); + } + @Test + void contextLoads() { + } + +} diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/ConstructorInjectedControllerTest.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/ConstructorInjectedControllerTest.java new file mode 100644 index 0000000000..3582a07609 --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/ConstructorInjectedControllerTest.java @@ -0,0 +1,28 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class ConstructorInjectedControllerTest { + + @Autowired + ConstructorInjectedController constructorInjectedController; + + +// @BeforeEach +// void setUp() { +// +// constructorInjectedController = new ConstructorInjectedController(new GreetingServiceImpl()); +// } + + @Test + void sayHello() { + System.out.println(constructorInjectedController.sayHello()); + } +} \ No newline at end of file diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/EnvironmentControllerTest.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/EnvironmentControllerTest.java new file mode 100644 index 0000000000..a72a56b29b --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/EnvironmentControllerTest.java @@ -0,0 +1,22 @@ +package com.springframework.spring6di.controllers; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import static org.junit.jupiter.api.Assertions.*; + +@ActiveProfiles({"dev", "EN"}) +@SpringBootTest +class EnvironmentControllerTest { + + @Autowired + EnvironmentController controller; + + @Test + void getEnvironment() { + System.out.println(controller.getEnvironment()); + + } +} diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/MyControllerTest.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/MyControllerTest.java new file mode 100644 index 0000000000..eb4de5a1a3 --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/MyControllerTest.java @@ -0,0 +1,15 @@ +package com.springframework.spring6di.controllers; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MyControllerTest { + + @Test + void sayHello() { + + MyController myController = new MyController(); + System.out.println(myController.sayHello()); + } +} \ No newline at end of file diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/PropertyInjectedControllerTest.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/PropertyInjectedControllerTest.java new file mode 100644 index 0000000000..e233f1fdce --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/PropertyInjectedControllerTest.java @@ -0,0 +1,30 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.lang.management.GarbageCollectorMXBean; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class PropertyInjectedControllerTest { + + @Autowired + PropertyInjectedController propertyInjectedController; +// @BeforeEach +// void setUp(){ +// +// propertyInjectedController = new PropertyInjectedController(); +// propertyInjectedController.greetingService = new GreetingServiceImpl(); +// +// } + @Test + void sayHello() { + + System.out.println(propertyInjectedController.sayHello()); + } +} \ No newline at end of file diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/SetterInjectedControllerTest.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/SetterInjectedControllerTest.java new file mode 100644 index 0000000000..302dd246e4 --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/SetterInjectedControllerTest.java @@ -0,0 +1,29 @@ +package com.springframework.spring6di.controllers; + +import com.springframework.spring6di.services.GreetingServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class SetterInjectedControllerTest { + + @Autowired + SetterInjectedController setterInjectedController; + +// @BeforeEach +// void setUp() { +// +// setterInjectedController = new SetterInjectedController(); +// setterInjectedController.setGreetingService(new GreetingServiceImpl()); +// } + + @Test + void sayHello() { + + System.out.println(setterInjectedController.sayHello()); + } +} \ No newline at end of file diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestEN.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestEN.java new file mode 100644 index 0000000000..3913a927a8 --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestEN.java @@ -0,0 +1,23 @@ +package com.springframework.spring6di.controllers.i18n; + + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +import static org.junit.jupiter.api.Assertions.*; + +@ActiveProfiles("EN") +@SpringBootTest +class myi18NControllerTestEN { + + @Autowired + Myi18NController myi18NController; + + @Test + void sayHello() { + + System.out.println(myi18NController.sayHello()); + } +} diff --git a/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestES.java b/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestES.java new file mode 100644 index 0000000000..d25426189d --- /dev/null +++ b/spring6di/src/test/java/com/springframework/spring6di/controllers/i18n/myi18NControllerTestES.java @@ -0,0 +1,20 @@ +package com.springframework.spring6di.controllers.i18n; + + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; + +@ActiveProfiles("ES") +@SpringBootTest +class myi18NControllerTestES { + + @Autowired + Myi18NController myi18NController; + + @Test + void sayHello() { + System.out.println(myi18NController.sayHello()); + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/BootstrapData.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootstrapData.java new file mode 100644 index 0000000000..acdb7bf093 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/BootstrapData.java @@ -0,0 +1,77 @@ +package guru.springframework.spring5webapp.bootstrap; + +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.domain.Publisher; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import guru.springframework.spring5webapp.repositories.BookRepository; +import guru.springframework.spring5webapp.repositories.PublisherRepository; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + + +@Component +public class BootstrapData implements CommandLineRunner { + + private final AuthorRepository authorRepository; + private final BookRepository bookRepository; + private final PublisherRepository publisherRepository; + + public BootstrapData(AuthorRepository authorRepository, BookRepository bookRepository, PublisherRepository publisherRepository) { + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + this.publisherRepository = publisherRepository; + } + + @Override + public void run(String... args) throws Exception { + + Author matejAuthor = new Author(); + matejAuthor.setFirstName("Matej"); + matejAuthor.setLastName("Gace"); + + Book mgBook = new Book(); + mgBook.setTitle("MG11"); + mgBook.setIsbn("123456"); + + + Author matejSavedAuthor = authorRepository.save(matejAuthor); + Book mgSavedBook = bookRepository.save(mgBook); + + Author marijaAuthor = new Author(); + marijaAuthor.setFirstName("Marija"); + marijaAuthor.setLastName("Mamic"); + + Book mmBook = new Book(); + mmBook.setTitle("MM"); + mmBook.setIsbn("987654"); + + Author marijaSavedAuthor = authorRepository.save(marijaAuthor); + Book mmSavedBook = bookRepository.save(mmBook); + + matejSavedAuthor.getBooks().add(mgSavedBook); + marijaSavedAuthor.getBooks().add(mmSavedBook); + mgSavedBook.getAuthors().add(matejSavedAuthor); + mmSavedBook.getAuthors().add(marijaSavedAuthor); + + Publisher publisherMG = new Publisher(); + publisherMG.setPublisherName("publisherMG"); + publisherMG.setAddress("Radnicka 21"); + Publisher savedPublisher = publisherRepository.save(publisherMG); + + mmBook.setPublisher(savedPublisher); + mgBook.setPublisher(savedPublisher); + + bookRepository.save(mgSavedBook); + bookRepository.save(mmSavedBook); + authorRepository.save(matejSavedAuthor); + authorRepository.save(marijaSavedAuthor); + + + System.out.println("in Bootstrap"); + System.out.println("Author count " + +authorRepository.count()); + System.out.println("Book count " + bookRepository.count()); + + System.out.println("Publisher count: " + publisherRepository.count()); + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/AuthorController.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/AuthorController.java new file mode 100644 index 0000000000..4279aba513 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/AuthorController.java @@ -0,0 +1,22 @@ +package guru.springframework.spring5webapp.bootstrap.controller; + +import guru.springframework.spring5webapp.services.AuthorService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class AuthorController { + AuthorService authorService; + + public AuthorController(AuthorService authorService) { + this.authorService = authorService; + } + + @RequestMapping("/authors") + public String getAuthors(Model model){ + model.addAttribute("authors", authorService.findAll()); + + return "authors"; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/BookController.java b/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/BookController.java new file mode 100644 index 0000000000..984a6afe37 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/bootstrap/controller/BookController.java @@ -0,0 +1,24 @@ +package guru.springframework.spring5webapp.bootstrap.controller; + +import guru.springframework.spring5webapp.services.BookService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class BookController { + + private final BookService bookService; + + public BookController(BookService bookService) { + this.bookService = bookService; + } + + @RequestMapping("/books") + public String getBooks(Model model){ + + model.addAttribute("books", bookService.findAll()); + + return "books"; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Author.java b/src/main/java/guru/springframework/spring5webapp/domain/Author.java new file mode 100644 index 0000000000..c8832c29ab --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Author.java @@ -0,0 +1,77 @@ +package guru.springframework.spring5webapp.domain; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Author { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String firstName; + private String lastName; + + + @ManyToMany(mappedBy = "authors") + private Set books = new HashSet<>(); + + public Set getBooks() { + return books; + } + + public void setBooks(Set books) { + this.books = books; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + 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; + } + + @Override + public String toString() { + return "Author{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", books=" + books + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Author author = (Author) o; + + return Objects.equals(id, author.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Book.java b/src/main/java/guru/springframework/spring5webapp/domain/Book.java new file mode 100644 index 0000000000..42c504f95d --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Book.java @@ -0,0 +1,89 @@ +package guru.springframework.spring5webapp.domain; + +import jakarta.persistence.*; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String title; + private String isbn; + + @ManyToMany + @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"), + inverseJoinColumns = @JoinColumn(name = "author_id")) + private Set authors = new HashSet<>(); + + @ManyToOne + private Publisher publisher; + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + public Set getAuthors() { + return authors; + } + + public void setAuthors(Set authors) { + this.authors = authors; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", isbn='" + isbn + '\'' + + ", authors=" + authors + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Book book = (Book) o; + + return Objects.equals(id, book.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java new file mode 100644 index 0000000000..e4bb99fb86 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/domain/Publisher.java @@ -0,0 +1,100 @@ +package guru.springframework.spring5webapp.domain; + +import jakarta.persistence.*; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +@Entity +public class Publisher { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + private String publisherName; + private String address; + private String city; + private String state; + private int zip; + @OneToMany(mappedBy = "publisher") + private Set books = new HashSet<>(); + + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPublisherName() { + return publisherName; + } + + public void setPublisherName(String publisherName) { + this.publisherName = publisherName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public int getZip() { + return zip; + } + + public void setZip(int zip) { + this.zip = zip; + } + + @Override + public String toString() { + return "Publisher{" + + "id=" + id + + ", publisherName='" + publisherName + '\'' + + ", address='" + address + '\'' + + ", city='" + city + '\'' + + ", state='" + state + '\'' + + ", zip=" + zip + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Publisher publisher = (Publisher) o; + + return Objects.equals(id, publisher.id); + } + + @Override + public int hashCode() { + return id != null ? id.hashCode() : 0; + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java new file mode 100644 index 0000000000..a5aa1f8726 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/AuthorRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Author; +import org.springframework.data.repository.CrudRepository; + +public interface AuthorRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java new file mode 100644 index 0000000000..570726803e --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/BookRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Book; +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java new file mode 100644 index 0000000000..8445629922 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/repositories/PublisherRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.repositories; + +import guru.springframework.spring5webapp.domain.Publisher; +import org.springframework.data.repository.CrudRepository; + +public interface PublisherRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/spring5webapp/services/AuthorService.java b/src/main/java/guru/springframework/spring5webapp/services/AuthorService.java new file mode 100644 index 0000000000..4fd39fb76b --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/services/AuthorService.java @@ -0,0 +1,7 @@ +package guru.springframework.spring5webapp.services; + +import guru.springframework.spring5webapp.domain.Author; + +public interface AuthorService { + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring5webapp/services/AuthorServiceImpl.java b/src/main/java/guru/springframework/spring5webapp/services/AuthorServiceImpl.java new file mode 100644 index 0000000000..5ca9ea9b51 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/services/AuthorServiceImpl.java @@ -0,0 +1,19 @@ +package guru.springframework.spring5webapp.services; + +import guru.springframework.spring5webapp.domain.Author; +import guru.springframework.spring5webapp.repositories.AuthorRepository; +import org.springframework.stereotype.Service; + +@Service +public class AuthorServiceImpl implements AuthorService { + AuthorRepository authorRepository; + + public AuthorServiceImpl(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public Iterable findAll() { + return authorRepository.findAll(); + } +} diff --git a/src/main/java/guru/springframework/spring5webapp/services/BookService.java b/src/main/java/guru/springframework/spring5webapp/services/BookService.java new file mode 100644 index 0000000000..6d0332eb71 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/services/BookService.java @@ -0,0 +1,8 @@ +package guru.springframework.spring5webapp.services; + +import guru.springframework.spring5webapp.domain.Book; + +public interface BookService { + + Iterable findAll(); +} diff --git a/src/main/java/guru/springframework/spring5webapp/services/BookServiceImpl.java b/src/main/java/guru/springframework/spring5webapp/services/BookServiceImpl.java new file mode 100644 index 0000000000..99854fe7b3 --- /dev/null +++ b/src/main/java/guru/springframework/spring5webapp/services/BookServiceImpl.java @@ -0,0 +1,20 @@ +package guru.springframework.spring5webapp.services; + +import guru.springframework.spring5webapp.domain.Book; +import guru.springframework.spring5webapp.repositories.BookRepository; +import org.springframework.stereotype.Service; + +@Service +public class BookServiceImpl implements BookService{ + + private final BookRepository bookRepository; + + public BookServiceImpl(BookRepository bookRepository) { + this.bookRepository = bookRepository; + } + + @Override + public Iterable findAll() { + return bookRepository.findAll(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29bb2..69b89983cb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/main/resources/templates/authors.html b/src/main/resources/templates/authors.html new file mode 100644 index 0000000000..9a6a84229c --- /dev/null +++ b/src/main/resources/templates/authors.html @@ -0,0 +1,29 @@ + + + + + + Authors + + +

Author List

+ + + + + + + + + + + + + + +
IDFirst NameLast NameBook Name
123Spring in actionSpring in action
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/books.html b/src/main/resources/templates/books.html new file mode 100644 index 0000000000..9528665f47 --- /dev/null +++ b/src/main/resources/templates/books.html @@ -0,0 +1,27 @@ + + + + + + Books + + +

Book List

+ + + + + + + + + + + + +
IDTitlePublisher
123Spring in actionwrox
+ + + + + \ No newline at end of file