├── .gitignore ├── README.md ├── aop ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── aop │ │ │ ├── Application.java │ │ │ ├── DomainService.java │ │ │ └── LoggingAspect.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── aop │ └── AopSpringBootTest.java ├── batch-decorator ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── batch │ │ │ └── decorator │ │ │ ├── BatchApplication.java │ │ │ ├── aggregator │ │ │ └── CustomLineAggregator.java │ │ │ ├── classifier │ │ │ └── CustomerClassifier.java │ │ │ ├── config │ │ │ └── JobConfiguration.java │ │ │ ├── mapper │ │ │ └── CustomerRowMapper.java │ │ │ └── model │ │ │ └── Customer.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ └── schema.sql │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── batch │ └── decorator │ └── AppTest.java ├── batch-partitioner ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── batch │ │ ├── LocalPartitioningApplication.java │ │ ├── config │ │ └── JobConfiguration.java │ │ ├── mapper │ │ └── CustomerRowMapper.java │ │ ├── model │ │ └── Customer.java │ │ └── partitioner │ │ └── ColumnRangePartitioner.java │ └── resources │ ├── application.properties │ ├── data.sql │ └── schema.sql ├── boot-hateoas ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── rest │ │ ├── SpringBootHateoasApplication.java │ │ ├── assembers │ │ ├── ActorModelAssembler.java │ │ └── AlbumModelAssembler.java │ │ ├── entity │ │ ├── ActorEntity.java │ │ └── AlbumEntity.java │ │ ├── model │ │ ├── ActorModel.java │ │ └── AlbumModel.java │ │ ├── repository │ │ ├── ActorRepository.java │ │ └── AlbumRepository.java │ │ └── web │ │ └── WebController.java │ └── resources │ ├── application.properties │ ├── data.sql │ └── schema.sql ├── command-line-runner ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── Application.java │ │ ├── ScopeTesting.java │ │ └── config │ │ └── ApplicationStartupRunner.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── context-path-server-port ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── Application.java │ │ │ └── config │ │ │ └── CustomizationBean.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── custom-spring-boot-banner ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ └── Application.java │ └── resources │ │ ├── application.properties │ │ └── banner.txt │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── disable-spring-boot-banner ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ └── Application.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── email ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── demo │ │ └── email │ │ ├── Application.java │ │ ├── EmailConfig.java │ │ └── EmailService.java │ └── resources │ └── application.properties ├── embedded-jetty-server ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ └── Application.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── enable-scheduling ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ └── App.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── get-loaded-beans-info ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ └── Application.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── i18n ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── demo │ │ ├── Application.java │ │ └── web │ │ └── HelloController.java │ └── resources │ ├── messages.properties │ └── messages_es.properties ├── logger-configuration ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── Application.java │ │ │ └── web │ │ │ └── HelloController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── main-class ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── MainClassOne.java │ │ └── MainClassTwo.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── minimum-setup ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── Application.java │ │ └── web │ │ └── HelloController.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── config │ └── AppTest.java ├── oauth2 ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── demo │ │ │ ├── SpringOauth2ResourceServerApplication.java │ │ │ ├── config │ │ │ ├── OAuth2AuthorizationServer.java │ │ │ ├── OAuth2ResourceServer.java │ │ │ └── SecurityConfig.java │ │ │ └── resource │ │ │ ├── RestResource.java │ │ │ └── UserProfile.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── demo │ └── SpringOauth2ResourceServerApplicationTests.java ├── packaging-war ├── .gitignore ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── Application.java │ │ └── web │ │ └── HelloController.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── pom.xml ├── rest-crud-hibernate ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── demo │ │ │ ├── DemoApplication.java │ │ │ ├── exception │ │ │ └── RecordNotFoundException.java │ │ │ ├── model │ │ │ └── EmployeeEntity.java │ │ │ ├── repository │ │ │ └── EmployeeRepository.java │ │ │ ├── service │ │ │ └── EmployeeService.java │ │ │ └── web │ │ │ └── EmployeeController.java │ └── resources │ │ ├── application.properties │ │ ├── data.sql │ │ ├── import.sql │ │ ├── schema.sql │ │ └── templates │ │ ├── add-edit-employee.html │ │ └── list-employees.html │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── demo │ ├── DemoApplicationTests.java │ └── EmployeeRepositoryTest.java ├── rest-hateoas ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── JerseydemoApplication.java │ │ ├── UserResource.java │ │ ├── config │ │ └── JerseyConfig.java │ │ ├── dao │ │ └── UserDB.java │ │ └── model │ │ ├── Report.java │ │ ├── User.java │ │ └── Users.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── rest-jersey-example ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── JerseydemoApplication.java │ │ ├── UserResource.java │ │ ├── config │ │ └── JerseyConfig.java │ │ ├── dao │ │ └── UserDB.java │ │ └── model │ │ ├── User.java │ │ └── Users.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── resttemplate ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── Application.java │ │ │ ├── config │ │ │ ├── HttpClientConfig.java │ │ │ └── RestTemplateConfig.java │ │ │ ├── model │ │ │ ├── EmployeeListVO.java │ │ │ └── EmployeeVO.java │ │ │ ├── service │ │ │ ├── UserService.java │ │ │ └── impl │ │ │ │ └── UserServiceImpl.java │ │ │ └── web │ │ │ └── EmployeeController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ ├── EmployeeControllerTest.java │ └── UserServiceTest.java ├── server-customization ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ └── App.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── spring-boot-actuator-example ├── .mvn │ └── wrapper │ │ ├── MavenWrapperDownloader.java │ │ └── maven-wrapper.properties ├── HELP.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── demo │ │ ├── SimpleRestController.java │ │ └── SpringBootActuatorExampleApplication.java │ └── resources │ └── application.properties ├── spring-boot-hateoas ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── hateoas │ │ │ └── demo │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── EmployeeController.java │ │ │ ├── dao │ │ │ └── EmployeeRepository.java │ │ │ └── model │ │ │ ├── Employee.java │ │ │ ├── EmployeeList.java │ │ │ └── EmployeeReportResult.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── hateoas │ └── demo │ └── AppTest.java ├── spring-reactive ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── reactive │ │ └── demo │ │ ├── WebfluxFunctionalApp.java │ │ ├── config │ │ ├── AppConfig.java │ │ └── WebFluxConfig.java │ │ ├── controller │ │ └── EmployeeController.java │ │ ├── model │ │ └── Employee.java │ │ └── service │ │ ├── EmployeeService.java │ │ └── IEmployeeService.java │ └── resources │ ├── application.properties │ └── logback.xml ├── spring-rmi ├── README.md ├── hessian-client │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── howtodoinjava │ │ │ │ ├── example │ │ │ │ └── hessianclient │ │ │ │ │ └── HessianClientApplication.java │ │ │ │ └── hessianserver │ │ │ │ └── hessian │ │ │ │ └── HelloWorld.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── howtodoinjava │ │ └── example │ │ └── hessianclient │ │ └── HessianClientApplicationTests.java ├── hessian-server │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── howtodoinjava │ │ │ │ └── hessianserver │ │ │ │ ├── HessianConfiguration.java │ │ │ │ ├── HessianServerApplication.java │ │ │ │ └── hessian │ │ │ │ ├── HelloWorld.java │ │ │ │ └── HelloWorldImpl.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── howtodoinjava │ │ └── hessianserver │ │ └── HessianServerApplicationTests.java ├── pom.xml ├── rmi-client │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ ├── springrmiclient │ │ │ │ └── SpringRmiClientApplication.java │ │ │ │ └── springrmiserver │ │ │ │ └── service │ │ │ │ └── HelloWorldRMI.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── com │ │ └── example │ │ └── springrmiclient │ │ └── SpringRmiClientApplicationTests.java └── rmi-server │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── springrmiserver │ │ │ ├── Config.java │ │ │ ├── SpringRmiServerApplication.java │ │ │ └── service │ │ │ ├── HelloWorldRMI.java │ │ │ └── HelloWorldRMIimpl.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── example │ └── springrmiserver │ └── SpringRmiServerApplicationTests.java ├── spring-webflux-demo ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── howtodoinjava │ │ │ └── demo │ │ │ ├── WebfluxFunctionalApp.java │ │ │ ├── config │ │ │ ├── AppConfig.java │ │ │ ├── MongoConfig.java │ │ │ └── WebFluxConfig.java │ │ │ ├── controller │ │ │ └── EmployeeController.java │ │ │ ├── dao │ │ │ └── EmployeeRepository.java │ │ │ ├── model │ │ │ └── Employee.java │ │ │ └── service │ │ │ ├── EmployeeService.java │ │ │ └── IEmployeeService.java │ └── resources │ │ ├── application.properties │ │ └── logback.xml │ └── test │ └── java │ └── com │ └── howtodoinjava │ └── demo │ └── EmployeeControllerTest.java ├── springrestexample ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── howtodoinjava │ │ └── demo │ │ ├── controller │ │ └── EmployeeRESTController.java │ │ └── model │ │ ├── EmployeeListVO.java │ │ └── EmployeeVO.java │ └── webapp │ ├── WEB-INF │ ├── spring-servlet.xml │ └── web.xml │ └── index.jsp ├── ssl-https ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── Application.java │ │ │ ├── config │ │ │ └── SSLConfig.java │ │ │ └── web │ │ │ └── HelloController.java │ └── resources │ │ ├── application.properties │ │ └── keystore.jks │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── swagger-ui ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── springexamples │ │ └── demo │ │ ├── Application.java │ │ ├── config │ │ └── Swagger2UiConfiguration.java │ │ └── web │ │ └── HelloController.java │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── template-freemarker ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── App.java │ │ │ └── rest │ │ │ └── HomeController.java │ └── resources │ │ ├── application.properties │ │ └── ftl │ │ ├── error.ftl │ │ └── home.ftl │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── template-jsp ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── App.java │ │ │ └── rest │ │ │ └── HomeController.java │ ├── resources │ │ └── application.properties │ └── webapp │ │ └── WEB-INF │ │ └── jsp │ │ ├── error.jsp │ │ └── home.jsp │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── template-mustache ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── App.java │ │ │ └── rest │ │ │ └── HomeController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── error.mustache │ │ └── home.mustache │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java ├── template-thymeleaf ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── springexamples │ │ │ └── demo │ │ │ ├── App.java │ │ │ └── rest │ │ │ └── HomeController.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── error.html │ │ └── home.html │ └── test │ └── java │ └── com │ └── springexamples │ └── demo │ └── AppTest.java └── unit-testing ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── howtodoinjava │ │ └── rest │ │ ├── SpringBootDemoApplication.java │ │ ├── controller │ │ └── EmployeeController.java │ │ ├── dao │ │ └── EmployeeRepository.java │ │ ├── exception │ │ ├── CustomExceptionHandler.java │ │ └── ErrorResponse.java │ │ └── model │ │ ├── Employee.java │ │ └── Employees.java └── resources │ ├── application.properties │ ├── data.sql │ └── schema.sql └── test └── java └── com └── howtodoinjava └── rest ├── EmployeeControllerIntegrationTests.java ├── EmployeeControllerUnitTests.java └── RestTemplatePostApiExamples.java /.gitignore: -------------------------------------------------------------------------------- 1 | tomcat 2 | 3 | # Binaries 4 | *.7z 5 | *.dmg 6 | *.gz 7 | *.iso 8 | *.jar 9 | *.rar 10 | *.tar 11 | *.zip 12 | *.war 13 | *.ear 14 | *.sar 15 | *.class 16 | 17 | # Maven 18 | target/ 19 | 20 | # eclipse project file 21 | .settings/ 22 | .metadata/ 23 | .classpath 24 | .project 25 | 26 | # intelliJ IDEA 27 | .idea/ 28 | *.iml 29 | 30 | # OS 31 | .DS_Store 32 | 33 | # Misc 34 | *.swp 35 | release.properties 36 | pom.xml.releaseBackup 37 | pom.xml.tag 38 | .factorypath 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot Examples by HowToDoInJava.com 2 | 3 | This repository hosts the projects and their source codes written for various tutorials written in [howtodoinjava.com](https://howtodoinjava.com/). -------------------------------------------------------------------------------- /aop/README.md: -------------------------------------------------------------------------------- 1 | # Related Tutorials 2 | 3 | 1. [AOP with Spring Boot](https://howtodoinjava.com/spring-boot2/aop-aspectj/) -------------------------------------------------------------------------------- /aop/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | aop 13 | aop 14 | http://maven.apache.org 15 | 16 | UTF-8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-aop 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | 31 | 32 | org.apache.commons 33 | commons-lang3 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /aop/src/main/java/com/howtodoinjava/aop/Application.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.aop; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /aop/src/main/java/com/howtodoinjava/aop/DomainService.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.aop; 2 | 3 | import java.util.Random; 4 | 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service 8 | public class DomainService 9 | { 10 | public Object getDomainObjectById(Long id) 11 | { 12 | try { 13 | Thread.sleep(new Random().nextInt(2000)); 14 | } catch (InterruptedException e) { 15 | //do some logging 16 | } 17 | return id; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /aop/src/main/java/com/howtodoinjava/aop/LoggingAspect.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.aop; 2 | 3 | import org.apache.logging.log4j.LogManager; 4 | import org.apache.logging.log4j.Logger; 5 | import org.aspectj.lang.ProceedingJoinPoint; 6 | import org.aspectj.lang.annotation.Around; 7 | import org.aspectj.lang.annotation.Aspect; 8 | import org.aspectj.lang.reflect.MethodSignature; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.util.StopWatch; 11 | 12 | @Aspect 13 | @Component 14 | public class LoggingAspect 15 | { 16 | private static final Logger LOGGER = LogManager.getLogger(LoggingAspect.class); 17 | 18 | @Around("execution(* com.howtodoinjava.aop..*(..)))") 19 | public Object profileAllMethods(ProceedingJoinPoint proceedingJoinPoint) throws Throwable 20 | { 21 | MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature(); 22 | 23 | //Get intercepted method details 24 | String className = methodSignature.getDeclaringType().getSimpleName(); 25 | String methodName = methodSignature.getName(); 26 | 27 | final StopWatch stopWatch = new StopWatch(); 28 | 29 | //Measure method execution time 30 | stopWatch.start(); 31 | Object result = proceedingJoinPoint.proceed(); 32 | stopWatch.stop(); 33 | 34 | //Log method execution time 35 | LOGGER.info("Execution time of " + className + "." + methodName + " " 36 | + ":: " + stopWatch.getTotalTimeMillis() + " ms"); 37 | 38 | return result; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /aop/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.pattern.console=%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n -------------------------------------------------------------------------------- /aop/src/test/java/com/howtodoinjava/aop/AopSpringBootTest.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.aop; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | 9 | @SpringBootTest 10 | public class AopSpringBootTest 11 | { 12 | @Autowired 13 | private DomainService service; 14 | 15 | @Test 16 | public void testGetDomainObjectById() { 17 | service.getDomainObjectById(10L); 18 | } 19 | } -------------------------------------------------------------------------------- /batch-decorator/README.md: -------------------------------------------------------------------------------- 1 | # Related tutorials 2 | 3 | 1. [Spring Batch - Writing to Multiple Destinations with Classifier](https://howtodoinjava.com/spring-batch/classifier-multi-destinations/) -------------------------------------------------------------------------------- /batch-decorator/src/main/java/com/howtodoinjava/batch/decorator/BatchApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator; 2 | 3 | import java.util.Date; 4 | import org.springframework.batch.core.Job; 5 | import org.springframework.batch.core.JobExecution; 6 | import org.springframework.batch.core.JobParameters; 7 | import org.springframework.batch.core.JobParametersBuilder; 8 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 9 | import org.springframework.batch.core.launch.JobLauncher; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.boot.CommandLineRunner; 12 | import org.springframework.boot.SpringApplication; 13 | import org.springframework.boot.autoconfigure.SpringBootApplication; 14 | 15 | @SpringBootApplication 16 | @EnableBatchProcessing 17 | public class BatchApplication 18 | implements CommandLineRunner { 19 | @Autowired 20 | private JobLauncher jobLauncher; 21 | 22 | @Autowired 23 | private Job job; 24 | 25 | public static void main(String[] args) { 26 | SpringApplication.run(BatchApplication.class, args); 27 | } 28 | 29 | @Override 30 | public void run(String... args) throws Exception { 31 | JobParameters jobParameters = new JobParametersBuilder() 32 | .addString("JobId", String.valueOf(System.currentTimeMillis())) 33 | .addDate("date", new Date()) 34 | .addLong("time",System.currentTimeMillis()).toJobParameters(); 35 | 36 | JobExecution execution = jobLauncher.run(job, jobParameters); 37 | System.out.println("STATUS :: "+execution.getStatus()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /batch-decorator/src/main/java/com/howtodoinjava/batch/decorator/aggregator/CustomLineAggregator.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator.aggregator; 2 | 3 | import org.springframework.batch.item.file.transform.LineAggregator; 4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | import com.howtodoinjava.batch.decorator.model.Customer; 7 | 8 | public class CustomLineAggregator implements LineAggregator { 9 | 10 | private ObjectMapper objectMapper = new ObjectMapper(); 11 | 12 | @Override 13 | public String aggregate(Customer item) { 14 | try { 15 | return objectMapper.writeValueAsString(item); 16 | } catch (Exception e) { 17 | throw new RuntimeException("Unable to serialize Customer", e); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /batch-decorator/src/main/java/com/howtodoinjava/batch/decorator/classifier/CustomerClassifier.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator.classifier; 2 | 3 | import org.springframework.batch.item.ItemWriter; 4 | import org.springframework.classify.Classifier; 5 | 6 | import com.howtodoinjava.batch.decorator.model.Customer; 7 | 8 | public class CustomerClassifier implements Classifier> { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | private ItemWriter evenItemWriter; 13 | private ItemWriter oddItemWriter; 14 | 15 | public CustomerClassifier(ItemWriter evenItemWriter, ItemWriter oddItemWriter) { 16 | this.evenItemWriter = evenItemWriter; 17 | this.oddItemWriter = oddItemWriter; 18 | } 19 | 20 | @Override 21 | public ItemWriter classify(Customer customer) { 22 | return customer.getId() % 2 == 0 ? evenItemWriter : oddItemWriter; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /batch-decorator/src/main/java/com/howtodoinjava/batch/decorator/mapper/CustomerRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator.mapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | import org.springframework.jdbc.core.RowMapper; 6 | import com.howtodoinjava.batch.decorator.model.Customer; 7 | 8 | public class CustomerRowMapper implements RowMapper { 9 | 10 | @Override 11 | public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { 12 | return Customer.builder().id(rs.getLong("id")) 13 | .firstName(rs.getString("firstName")) 14 | .lastName(rs.getString("lastName")) 15 | .birthdate(rs.getString("birthdate")).build(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /batch-decorator/src/main/java/com/howtodoinjava/batch/decorator/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator.model; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @Builder 11 | @NoArgsConstructor 12 | public class Customer { 13 | 14 | private Long id; 15 | private String firstName; 16 | private String lastName; 17 | private String birthdate; 18 | } 19 | -------------------------------------------------------------------------------- /batch-decorator/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/test 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | spring.batch.initialize-schema=always -------------------------------------------------------------------------------- /batch-decorator/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE 'test'.'customer' ( 2 | 'id' MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, 3 | 'firstName' VARCHAR(255) NULL, 4 | 'lastName' VARCHAR(255) NULL, 5 | 'birthdate' VARCHAR(255) NULL, 6 | PRIMARY KEY ('id') 7 | ) AUTO_INCREMENT=1; -------------------------------------------------------------------------------- /batch-decorator/src/test/java/com/howtodoinjava/batch/decorator/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.decorator; 2 | 3 | public class AppTest 4 | { 5 | } 6 | -------------------------------------------------------------------------------- /batch-partitioner/README.md: -------------------------------------------------------------------------------- 1 | # Related tutorials 2 | 3 | 1. [Spring Batch - Partitioning](https://howtodoinjava.com/spring-batch/spring-batch-step-partitioning/) -------------------------------------------------------------------------------- /batch-partitioner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | 9 | com.springexamples.demo 10 | SpringExamples 11 | 0.0.1-SNAPSHOT 12 | 13 | 14 | com.howtodoinjava 15 | batch-partitioner 16 | batch-partitioner 17 | http://maven.apache.org 18 | 19 | 20 | UTF-8 21 | 21 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-batch 28 | 29 | 30 | 31 | com.h2database 32 | h2 33 | runtime 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /batch-partitioner/src/main/java/com/howtodoinjava/batch/LocalPartitioningApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.batch.core.Job; 6 | import org.springframework.batch.core.JobExecution; 7 | import org.springframework.batch.core.JobParameters; 8 | import org.springframework.batch.core.JobParametersBuilder; 9 | import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 10 | import org.springframework.batch.core.launch.JobLauncher; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.boot.CommandLineRunner; 13 | import org.springframework.boot.SpringApplication; 14 | import org.springframework.boot.autoconfigure.SpringBootApplication; 15 | 16 | @SpringBootApplication 17 | @EnableBatchProcessing 18 | public class LocalPartitioningApplication implements CommandLineRunner 19 | { 20 | @Autowired 21 | private JobLauncher jobLauncher; 22 | 23 | @Autowired 24 | private Job job; 25 | 26 | public static void main(String[] args) { 27 | SpringApplication.run(LocalPartitioningApplication.class, args); 28 | } 29 | 30 | @Override 31 | public void run(String... args) throws Exception 32 | { 33 | System.out.println("STATUS STARTED==================="); 34 | 35 | JobParameters jobParameters = new JobParametersBuilder() 36 | .addString("JobId", String.valueOf(System.currentTimeMillis())) 37 | .addDate("date", new Date()) 38 | .addLong("time",System.currentTimeMillis()).toJobParameters(); 39 | 40 | JobExecution execution = jobLauncher.run(job, jobParameters); 41 | 42 | System.out.println("STATUS :: "+execution.getStatus()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /batch-partitioner/src/main/java/com/howtodoinjava/batch/mapper/CustomerRowMapper.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.mapper; 2 | 3 | import java.sql.ResultSet; 4 | import java.sql.SQLException; 5 | import org.springframework.jdbc.core.RowMapper; 6 | import com.howtodoinjava.batch.model.Customer; 7 | 8 | public class CustomerRowMapper implements RowMapper { 9 | 10 | @Override 11 | public Customer mapRow(ResultSet rs, int rowNum) throws SQLException { 12 | return Customer.builder() 13 | .id(rs.getLong("id")) 14 | .firstName(rs.getString("firstName")) 15 | .lastName(rs.getString("lastName")) 16 | .birthdate(rs.getString("birthdate")) 17 | .build(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /batch-partitioner/src/main/java/com/howtodoinjava/batch/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.model; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Builder; 5 | import lombok.Data; 6 | import lombok.NoArgsConstructor; 7 | 8 | @Data 9 | @AllArgsConstructor 10 | @Builder 11 | @NoArgsConstructor 12 | public class Customer 13 | { 14 | private Long id; 15 | private String firstName; 16 | private String lastName; 17 | private String birthdate; 18 | } -------------------------------------------------------------------------------- /batch-partitioner/src/main/java/com/howtodoinjava/batch/partitioner/ColumnRangePartitioner.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.batch.partitioner; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import javax.sql.DataSource; 6 | import org.springframework.batch.core.partition.support.Partitioner; 7 | import org.springframework.batch.item.ExecutionContext; 8 | import org.springframework.jdbc.core.JdbcOperations; 9 | import org.springframework.jdbc.core.JdbcTemplate; 10 | 11 | public class ColumnRangePartitioner implements Partitioner 12 | { 13 | private JdbcOperations jdbcTemplate; 14 | private String table; 15 | private String column; 16 | 17 | public void setTable(String table) { 18 | this.table = table; 19 | } 20 | 21 | public void setColumn(String column) { 22 | this.column = column; 23 | } 24 | 25 | public void setDataSource(DataSource dataSource) { 26 | jdbcTemplate = new JdbcTemplate(dataSource); 27 | } 28 | 29 | @Override 30 | public Map partition(int gridSize) 31 | { 32 | int min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") FROM " + table, Integer.class); 33 | 34 | int max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") FROM " + table, Integer.class); 35 | 36 | int targetSize = (max - min) / gridSize + 1; 37 | 38 | Map result = new HashMap<>(); 39 | 40 | int number = 0; 41 | int start = min; 42 | int end = start + targetSize - 1; 43 | 44 | while (start <= max) 45 | { 46 | ExecutionContext value = new ExecutionContext(); 47 | result.put("partition" + number, value); 48 | 49 | if(end >= max) { 50 | end = max; 51 | } 52 | 53 | value.putInt("minValue", start); 54 | value.putInt("maxValue", end); 55 | 56 | start += targetSize; 57 | end += targetSize; 58 | 59 | number++; 60 | } 61 | return result; 62 | } 63 | } -------------------------------------------------------------------------------- /batch-partitioner/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:test 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | 7 | #Prevents running the job during application context creation 8 | spring.batch.job.enabled=false -------------------------------------------------------------------------------- /batch-partitioner/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO customer VALUES ('1', 'John', 'Doe', '10-10-1952 10:10:10'); 2 | INSERT INTO customer VALUES ('2', 'Amy', 'Eugene', '05-07-1985 17:10:00'); 3 | INSERT INTO customer VALUES ('3', 'Laverne', 'Mann', '11-12-1988 10:10:10'); 4 | INSERT INTO customer VALUES ('4', 'Janice', 'Preston', '19-02-1960 10:10:10'); 5 | INSERT INTO customer VALUES ('5', 'Pauline', 'Rios', '29-08-1977 10:10:10'); 6 | INSERT INTO customer VALUES ('6', 'Perry', 'Burnside', '10-03-1981 10:10:10'); 7 | INSERT INTO customer VALUES ('7', 'Todd', 'Kinsey', '14-12-1998 10:10:10'); 8 | INSERT INTO customer VALUES ('8', 'Jacqueline', 'Hyde', '20-03-1983 10:10:10'); 9 | INSERT INTO customer VALUES ('9', 'Rico', 'Hale', '10-10-2000 10:10:10'); 10 | INSERT INTO customer VALUES ('10', 'Samuel', 'Lamm', '11-11-1999 10:10:10'); 11 | INSERT INTO customer VALUES ('11', 'Robert', 'Coster', '10-10-1972 10:10:10'); 12 | INSERT INTO customer VALUES ('12', 'Tamara', 'Soler', '02-01-1978 10:10:10'); 13 | INSERT INTO customer VALUES ('13', 'Justin', 'Kramer', '19-11-1951 10:10:10'); 14 | INSERT INTO customer VALUES ('14', 'Andrea', 'Law', '14-10-1959 10:10:10'); 15 | INSERT INTO customer VALUES ('15', 'Laura', 'Porter', '12-12-2010 10:10:10'); 16 | INSERT INTO customer VALUES ('16', 'Michael', 'Cantu', '11-04-1999 10:10:10'); 17 | INSERT INTO customer VALUES ('17', 'Andrew', 'Thomas', '04-05-1967 10:10:10'); 18 | INSERT INTO customer VALUES ('18', 'Jose', 'Hannah', '16-09-1950 10:10:10'); 19 | INSERT INTO customer VALUES ('19', 'Valerie', 'Hilbert', '13-06-1966 10:10:10'); 20 | INSERT INTO customer VALUES ('20', 'Patrick', 'Durham', '12-10-1978 10:10:10'); -------------------------------------------------------------------------------- /batch-partitioner/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE customer ( 2 | id INT PRIMARY KEY, 3 | firstName VARCHAR(255) NULL, 4 | lastName VARCHAR(255) NULL, 5 | birthdate VARCHAR(255) NULL 6 | ); 7 | 8 | CREATE TABLE new_customer ( 9 | id INT PRIMARY KEY, 10 | firstName VARCHAR(255) NULL, 11 | lastName VARCHAR(255) NULL, 12 | birthdate VARCHAR(255) NULL 13 | ); -------------------------------------------------------------------------------- /boot-hateoas/README.md: -------------------------------------------------------------------------------- 1 | # Related tutorials 2 | 3 | 1. [Spring HATEOAS Tutorial](https://howtodoinjava.com/spring-boot/rest-with-spring-hateoas-example/) 4 | 2. [Spring HATEOAS - Embedded collection model name](https://howtodoinjava.com/spring5/hateoas/embedded-collection-name/) 5 | 3. [Spring HATEOAS - Pagination Links](https://howtodoinjava.com/spring5/hateoas/pagination-links/) -------------------------------------------------------------------------------- /boot-hateoas/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | 9 | com.springexamples.demo 10 | SpringExamples 11 | 0.0.1-SNAPSHOT 12 | 13 | 14 | com.springexamples 15 | boot-hateoas 16 | boot-hateoas 17 | http://maven.apache.org 18 | 19 | 20 | UTF-8 21 | 17 22 | 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-data-jpa 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-hateoas 39 | 40 | 41 | 42 | com.h2database 43 | h2 44 | runtime 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/SpringBootHateoasApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.web.filter.ForwardedHeaderFilter; 9 | 10 | @SpringBootApplication 11 | @EnableAutoConfiguration(exclude = HypermediaAutoConfiguration.class) 12 | public class SpringBootHateoasApplication { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(SpringBootHateoasApplication.class, args); 16 | } 17 | 18 | @Bean 19 | ForwardedHeaderFilter forwardedHeaderFilter() { 20 | return new ForwardedHeaderFilter(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/entity/ActorEntity.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import jakarta.persistence.CascadeType; 7 | import jakarta.persistence.Entity; 8 | import jakarta.persistence.GeneratedValue; 9 | import jakarta.persistence.GenerationType; 10 | import jakarta.persistence.Id; 11 | import jakarta.persistence.JoinColumn; 12 | import jakarta.persistence.JoinTable; 13 | import jakarta.persistence.ManyToMany; 14 | import jakarta.persistence.Table; 15 | 16 | import lombok.AllArgsConstructor; 17 | import lombok.Builder; 18 | import lombok.Data; 19 | import lombok.NoArgsConstructor; 20 | import lombok.ToString; 21 | 22 | @Data 23 | @Builder 24 | @AllArgsConstructor 25 | @NoArgsConstructor 26 | @ToString(exclude = "albums") 27 | @Entity 28 | @Table(name="actor") 29 | public class ActorEntity implements Serializable 30 | { 31 | private static final long serialVersionUID = 1L; 32 | 33 | @Id 34 | @GeneratedValue(strategy = GenerationType.IDENTITY) 35 | private Long id; 36 | private String firstName; 37 | private String lastName; 38 | private String birthDate; 39 | 40 | @ManyToMany(cascade=CascadeType.ALL) 41 | @JoinTable( 42 | name = "actor_album", 43 | joinColumns = @JoinColumn(name = "actor_id"), 44 | inverseJoinColumns = @JoinColumn(name = "album_id")) 45 | private List albums; 46 | } 47 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/entity/AlbumEntity.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import jakarta.persistence.Entity; 7 | import jakarta.persistence.FetchType; 8 | import jakarta.persistence.GeneratedValue; 9 | import jakarta.persistence.GenerationType; 10 | import jakarta.persistence.Id; 11 | import jakarta.persistence.ManyToMany; 12 | import jakarta.persistence.Table; 13 | 14 | import lombok.AllArgsConstructor; 15 | import lombok.Builder; 16 | import lombok.Data; 17 | import lombok.NoArgsConstructor; 18 | import lombok.ToString; 19 | 20 | @Data 21 | @Builder 22 | @AllArgsConstructor 23 | @NoArgsConstructor 24 | @Entity 25 | @ToString(exclude = "actors") 26 | @Table(name="album") 27 | public class AlbumEntity implements Serializable 28 | { 29 | private static final long serialVersionUID = 1L; 30 | 31 | @Id 32 | @GeneratedValue(strategy = GenerationType.IDENTITY) 33 | private Long id; 34 | private String title; 35 | private String description; 36 | private String releaseDate; 37 | 38 | @ManyToMany(mappedBy = "albums",fetch = FetchType.EAGER) 39 | private List actors; 40 | } -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/model/ActorModel.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.model; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.hateoas.RepresentationModel; 6 | import org.springframework.hateoas.server.core.Relation; 7 | 8 | import com.fasterxml.jackson.annotation.JsonInclude; 9 | import com.fasterxml.jackson.annotation.JsonInclude.Include; 10 | 11 | import lombok.AllArgsConstructor; 12 | import lombok.Builder; 13 | import lombok.Data; 14 | import lombok.EqualsAndHashCode; 15 | import lombok.NoArgsConstructor; 16 | 17 | @Data 18 | @Builder 19 | @NoArgsConstructor 20 | @AllArgsConstructor 21 | @EqualsAndHashCode(callSuper = false) 22 | @Relation(collectionRelation = "actors", itemRelation = "actor") 23 | @JsonInclude(Include.NON_NULL) 24 | public class ActorModel extends RepresentationModel 25 | { 26 | private Long id; 27 | private String firstName; 28 | private String lastName; 29 | private String birthDate; 30 | 31 | private List albums; 32 | } 33 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/model/AlbumModel.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.model; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.hateoas.RepresentationModel; 6 | import org.springframework.hateoas.server.core.Relation; 7 | 8 | import com.fasterxml.jackson.annotation.JsonInclude; 9 | import com.fasterxml.jackson.annotation.JsonInclude.Include; 10 | 11 | import lombok.AllArgsConstructor; 12 | import lombok.Builder; 13 | import lombok.Data; 14 | import lombok.EqualsAndHashCode; 15 | import lombok.NoArgsConstructor; 16 | 17 | @Data 18 | @Builder 19 | @NoArgsConstructor 20 | @AllArgsConstructor 21 | @EqualsAndHashCode(callSuper = false) 22 | @Relation(collectionRelation = "albums", itemRelation = "album") 23 | @JsonInclude(Include.NON_NULL) 24 | public class AlbumModel extends RepresentationModel 25 | { 26 | private Long id; 27 | private String title; 28 | private String description; 29 | private String releaseDate; 30 | 31 | private List actors; 32 | } 33 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/repository/ActorRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.repository.PagingAndSortingRepository; 5 | 6 | import com.howtodoinjava.rest.entity.ActorEntity; 7 | 8 | public interface ActorRepository extends JpaRepository { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/java/com/howtodoinjava/rest/repository/AlbumRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.data.repository.PagingAndSortingRepository; 5 | 6 | import com.howtodoinjava.rest.entity.AlbumEntity; 7 | 8 | public interface AlbumRepository extends JpaRepository { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /boot-hateoas/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:test 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | 7 | spring.jpa.hibernate.ddl-auto=none 8 | 9 | #spring.hateoas.use-hal-as-default-json-media-type=false -------------------------------------------------------------------------------- /boot-hateoas/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO actor VALUES ('1', 'John', 'Doe', '10-Jan-1952'); 2 | INSERT INTO actor VALUES ('2', 'Amy', 'Eugene', '05-07-1985'); 3 | INSERT INTO actor VALUES ('3', 'Laverne', 'Mann', '11-12-1988'); 4 | INSERT INTO actor VALUES ('4', 'Janice', 'Preston', '19-02-1960'); 5 | INSERT INTO actor VALUES ('5', 'Pauline', 'Rios', '29-08-1977'); 6 | 7 | INSERT INTO album VALUES ('1', 'Top Hits Vol 1', 'Top hits vol 1. description', '10-03-1981'); 8 | INSERT INTO album VALUES ('2', 'Top Hits Vol 2', 'Top hits vol 2. description', '10-03-1982'); 9 | INSERT INTO album VALUES ('3', 'Top Hits Vol 3', 'Top hits vol 3. description', '10-03-1983'); 10 | INSERT INTO album VALUES ('4', 'Top Hits Vol 4', 'Top hits vol 4. description', '10-03-1984'); 11 | INSERT INTO album VALUES ('5', 'Top Hits Vol 5', 'Top hits vol 5. description', '10-03-1985'); 12 | INSERT INTO album VALUES ('6', 'Top Hits Vol 6', 'Top hits vol 6. description', '10-03-1986'); 13 | INSERT INTO album VALUES ('7', 'Top Hits Vol 7', 'Top hits vol 7. description', '10-03-1987'); 14 | INSERT INTO album VALUES ('8', 'Top Hits Vol 8', 'Top hits vol 8. description', '10-03-1988'); 15 | INSERT INTO album VALUES ('9', 'Top Hits Vol 9', 'Top hits vol 9. description', '10-03-1989'); 16 | INSERT INTO album VALUES ('10', 'Top Hits Vol 10', 'Top hits vol 10. description', '10-03-1990'); 17 | 18 | INSERT INTO actor_album VALUES (1, 1); 19 | INSERT INTO actor_album VALUES (1, 2); 20 | INSERT INTO actor_album VALUES (2, 3); 21 | INSERT INTO actor_album VALUES (2, 4); 22 | INSERT INTO actor_album VALUES (3, 5); 23 | INSERT INTO actor_album VALUES (3, 6); 24 | INSERT INTO actor_album VALUES (4, 7); 25 | INSERT INTO actor_album VALUES (4, 8); 26 | INSERT INTO actor_album VALUES (5, 9); 27 | INSERT INTO actor_album VALUES (5, 10); -------------------------------------------------------------------------------- /boot-hateoas/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE actor ( 2 | id INT PRIMARY KEY, 3 | first_name VARCHAR(255) NULL, 4 | last_name VARCHAR(255) NULL, 5 | birth_date VARCHAR(255) NULL 6 | ); 7 | 8 | CREATE TABLE album ( 9 | id INT PRIMARY KEY, 10 | title VARCHAR(255) NULL, 11 | description VARCHAR(255) NULL, 12 | release_date VARCHAR(255) NULL 13 | ); 14 | 15 | CREATE TABLE actor_album ( 16 | actor_id INT, 17 | album_id INT 18 | ); -------------------------------------------------------------------------------- /command-line-runner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | command-line-runner 12 | command-line-runner 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /command-line-runner/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | @SpringBootApplication 10 | public class Application implements CommandLineRunner 11 | { 12 | protected final Log logger = LogFactory.getLog(getClass()); 13 | 14 | public static void main( String[] args ) 15 | { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | 19 | @Override 20 | public void run(String... args) throws Exception { 21 | logger.info("ApplicationStartupRunner run method Started !!"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /command-line-runner/src/main/java/com/springexamples/demo/ScopeTesting.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.beans.factory.annotation.Lookup; 4 | import org.springframework.beans.factory.config.ConfigurableBeanFactory; 5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Scope; 10 | import org.springframework.stereotype.Component; 11 | 12 | @Configuration 13 | @ComponentScan(basePackageClasses = SingletonBean.class) 14 | public class ScopeTesting { 15 | 16 | public static void main(String[] args) { 17 | AnnotationConfigApplicationContext context = 18 | new AnnotationConfigApplicationContext(ScopeTesting.class); 19 | 20 | SingletonBean bean = context.getBean(SingletonBean.class); 21 | 22 | bean.print(); 23 | } 24 | } 25 | 26 | @Component 27 | class SingletonBean { 28 | 29 | @Lookup 30 | private PrototypeBean getPrototypeBean(){ 31 | return null; 32 | } 33 | 34 | public void print() { 35 | System.out.println(getPrototypeBean().hashCode()); 36 | } 37 | } 38 | 39 | @Component 40 | @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 41 | class PrototypeBean { 42 | 43 | public void print() { 44 | System.out.println(this.hashCode()); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /command-line-runner/src/main/java/com/springexamples/demo/config/ApplicationStartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class ApplicationStartupRunner implements CommandLineRunner 10 | { 11 | protected final Log logger = LogFactory.getLog(getClass()); 12 | 13 | @Override 14 | public void run(String... args) throws Exception { 15 | logger.info("ApplicationStartupRunner run method Started !!"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /command-line-runner/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } -------------------------------------------------------------------------------- /context-path-server-port/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | context-path-server-port 12 | context-path-server-port 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /context-path-server-port/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) 9 | { 10 | SpringApplication app = new SpringApplication(Application.class); 11 | app.run(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /context-path-server-port/src/main/java/com/springexamples/demo/config/CustomizationBean.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.springframework.boot.web.server.WebServerFactoryCustomizer; 4 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class CustomizationBean implements WebServerFactoryCustomizer { 9 | 10 | @Override 11 | public void customize(ConfigurableServletWebServerFactory server) { 12 | server.setPort(9000); 13 | server.setContextPath("/springexamples"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /context-path-server-port/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #server.port=9000 2 | #server.contextPath=/springexamples -------------------------------------------------------------------------------- /context-path-server-port/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /custom-spring-boot-banner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | custom-spring-boot-banner 12 | custom-spring-boot-banner 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /custom-spring-boot-banner/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) 9 | { 10 | SpringApplication app = new SpringApplication(Application.class); 11 | app.run(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /custom-spring-boot-banner/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.main.banner-mode=CONSOLE -------------------------------------------------------------------------------- /custom-spring-boot-banner/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | _ _ _______ _ 2 | | | (_) (_______) | | 3 | \ \ ____ ____ _ ____ ____ _____ _ _ ____ ____ ____ | | ____ ___ 4 | \ \| _ \ / ___) | _ \ / _ | ___) ( \ / ) _ | \| _ \| |/ _ )/___) 5 | _____) ) | | | | | | | | ( ( | | |_____ ) X ( ( | | | | | | | | ( (/ /|___ | 6 | (______/| ||_/|_| |_|_| |_|\_|| |_______|_/ \_)_||_|_|_|_| ||_/|_|\____|___/ 7 | |_| (_____| |_| 8 | 9 | ::::::::::::::: Spring Boot${spring-boot.formatted-version} :::::::::::::::::: -------------------------------------------------------------------------------- /custom-spring-boot-banner/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /disable-spring-boot-banner/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | disable-spring-boot-banner 12 | disable-spring-boot-banner 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /disable-spring-boot-banner/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.Banner; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | @SpringBootApplication 8 | public class Application { 9 | public static void main(String[] args) 10 | { 11 | SpringApplication app = new SpringApplication(Application.class); 12 | app.setBannerMode(Banner.Mode.OFF); 13 | app.run(args); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /disable-spring-boot-banner/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.main.banner-mode=off -------------------------------------------------------------------------------- /disable-spring-boot-banner/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /email/README.md: -------------------------------------------------------------------------------- 1 | # Related tutorials 2 | 3 | 1. [Spring boot - Send email with attachment](https://howtodoinjava.com/spring-boot2/send-email-with-attachment/) -------------------------------------------------------------------------------- /email/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | email 13 | email 14 | http://maven.apache.org 15 | 16 | UTF-8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-mail 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /email/src/main/java/com/howtodoinjava/demo/email/Application.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.email; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | 8 | @SpringBootApplication 9 | public class Application implements CommandLineRunner 10 | { 11 | @Autowired 12 | private EmailService emailService; 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(Application.class, args); 16 | } 17 | 18 | @Override 19 | public void run(String... args) 20 | { 21 | emailService.sendMail("lokeshgupta1981@gmail.com", "Hi", "Ho ho ho"); 22 | 23 | emailService.sendPreConfiguredMail("Ho ho ho"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /email/src/main/java/com/howtodoinjava/demo/email/EmailConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.email; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.mail.SimpleMailMessage; 6 | 7 | @Configuration 8 | public class EmailConfig 9 | { 10 | @Bean 11 | public SimpleMailMessage emailTemplate() 12 | { 13 | SimpleMailMessage message = new SimpleMailMessage(); 14 | message.setTo("user@gmail.com"); 15 | message.setFrom("admin@gmail.com"); 16 | message.setSubject("Important email"); 17 | message.setText("FATAL - Application crash. Save your job !!"); 18 | return message; 19 | } 20 | } -------------------------------------------------------------------------------- /email/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | debug=true 2 | 3 | spring.mail.host=smtp.gmail.com 4 | spring.mail.port=25 5 | spring.mail.username=admin@gmail.com 6 | spring.mail.password=xxxxxx 7 | 8 | # Other properties 9 | spring.mail.properties.mail.debug=true 10 | spring.mail.properties.mail.transport.protocol=smtp 11 | spring.mail.properties.mail.smtp.auth=true 12 | spring.mail.properties.mail.smtp.connectiontimeout=5000 13 | spring.mail.properties.mail.smtp.timeout=5000 14 | spring.mail.properties.mail.smtp.writetimeout=5000 15 | 16 | # TLS , port 587 17 | spring.mail.properties.mail.smtp.starttls.enable=true 18 | 19 | # SSL, post 465 20 | #spring.mail.properties.mail.smtp.socketFactory.port = 465 21 | #spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory -------------------------------------------------------------------------------- /embedded-jetty-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | embedded-jetty-server 12 | embedded-jetty-server 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-tomcat 25 | 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-jetty 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-test 37 | test 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /embedded-jetty-server/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | public static void main(String[] args) 9 | { 10 | SpringApplication app = new SpringApplication(Application.class); 11 | app.run(args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /embedded-jetty-server/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /enable-scheduling/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | enable-scheduling 12 | enable-scheduling 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /enable-scheduling/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import java.util.Calendar; 4 | 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.scheduling.annotation.EnableScheduling; 8 | import org.springframework.scheduling.annotation.Scheduled; 9 | 10 | @SpringBootApplication 11 | @EnableScheduling 12 | public class App 13 | { 14 | public static void main( String[] args ) 15 | { 16 | SpringApplication app = new SpringApplication(App.class); 17 | app.run(args); 18 | } 19 | 20 | @Scheduled(initialDelay = 1000, fixedRate = 10000) 21 | public void run() { 22 | System.out.println("Current time is :: " + Calendar.getInstance().getTime()); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /enable-scheduling/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /get-loaded-beans-info/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.springexamples.demo 7 | SpringExamples 8 | 0.0.1-SNAPSHOT 9 | 10 | get-loaded-beans-info 11 | get-loaded-beans-info 12 | http://maven.apache.org 13 | 14 | UTF-8 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /get-loaded-beans-info/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import java.util.Arrays; 4 | 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.annotation.Bean; 10 | 11 | @SpringBootApplication 12 | public class Application 13 | { 14 | public static void main(String[] args) 15 | { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | 19 | @Bean 20 | public CommandLineRunner run(ApplicationContext appContext) { 21 | return args -> { 22 | 23 | String[] beans = appContext.getBeanDefinitionNames(); 24 | Arrays.stream(beans).sorted().forEach(System.out::println); 25 | }; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /get-loaded-beans-info/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /i18n/README.md: -------------------------------------------------------------------------------- 1 | # Spring boot i18n 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring boot i18n example](https://howtodoinjava.com/spring-boot2/rest/i18n-internationalization/) -------------------------------------------------------------------------------- /i18n/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | i18n 12 | i18n 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /i18n/src/main/java/com/howtodoinjava/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /i18n/src/main/java/com/howtodoinjava/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.web; 2 | 3 | import java.util.Locale; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.MessageSource; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | public class HelloController { 12 | 13 | @Autowired 14 | MessageSource messageSource; 15 | 16 | @GetMapping("/") 17 | public String index(Locale locale) { 18 | return messageSource.getMessage("error.notfound", null, locale); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /i18n/src/main/resources/messages.properties: -------------------------------------------------------------------------------- 1 | error.notfound=Resource not available -------------------------------------------------------------------------------- /i18n/src/main/resources/messages_es.properties: -------------------------------------------------------------------------------- 1 | error.notfound=Recurso no disponible -------------------------------------------------------------------------------- /logger-configuration/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | logger-configuration 12 | logger-configuration 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /logger-configuration/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.apache.commons.logging.Log; 4 | import org.apache.commons.logging.LogFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | @SpringBootApplication 10 | public class Application implements CommandLineRunner 11 | { 12 | protected final Log logger = LogFactory.getLog(getClass()); 13 | 14 | public static void main( String[] args ) 15 | { 16 | SpringApplication.run(Application.class, args); 17 | } 18 | 19 | @Override 20 | public void run(String... args) throws Exception { 21 | logger.info("INFO level log statement !!"); 22 | logger.debug("DEBUG level log statement !!"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /logger-configuration/src/main/java/com/springexamples/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @RequestMapping("/") 10 | public String index() { 11 | return "Greetings from Spring Examples !!"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /logger-configuration/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework=DEBUG 2 | logging.level.com.springexamples=DEBUG 3 | 4 | #output to a temp_folder/file 5 | logging.file=${java.io.tmpdir}/application.log 6 | 7 | # Logging pattern for the console 8 | logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n 9 | 10 | # Logging pattern for file 11 | logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg% -------------------------------------------------------------------------------- /logger-configuration/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /main-class/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | main-class 12 | main-class 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | com.springexamples.demo.MainClassOne 18 | 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-web 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-test 28 | test 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /main-class/src/main/java/com/springexamples/demo/MainClassOne.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MainClassOne 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(MainClassOne.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /main-class/src/main/java/com/springexamples/demo/MainClassTwo.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MainClassTwo 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(MainClassTwo.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /main-class/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest { 6 | 7 | @Test 8 | void contextLoads() { 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /minimum-setup/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | minimum-setup 12 | minimum-setup 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-test 27 | test 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /minimum-setup/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /minimum-setup/src/main/java/com/springexamples/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @RequestMapping("/") 10 | public String index() { 11 | return "Greetings from Spring Examples !!"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /minimum-setup/src/test/java/com/springexamples/demo/config/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest { 6 | 7 | @Test 8 | void contextLoads() { 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /oauth2/README.md: -------------------------------------------------------------------------------- 1 | # Oauth2 examples 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring boot oauth2 auth server](https://howtodoinjava.com/spring5/security5/oauth2-auth-server/) -------------------------------------------------------------------------------- /oauth2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | com.howtodoinjava 12 | oauth2 13 | oauth2 14 | Demo project for Spring Boot 15 | 16 | 17 | 17 18 | 19 | 20 | 21 | 22 | org.springframework.security.oauth.boot 23 | spring-security-oauth2-autoconfigure 24 | 2.1.8.RELEASE 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | org.springframework.security 37 | spring-security-test 38 | test 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /oauth2/src/main/java/com/howtodoinjava/demo/SpringOauth2ResourceServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringOauth2ResourceServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringOauth2ResourceServerApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /oauth2/src/main/java/com/howtodoinjava/demo/config/OAuth2AuthorizationServer.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 6 | import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; 7 | import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; 8 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; 9 | import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer; 10 | 11 | @Configuration 12 | @EnableAuthorizationServer 13 | public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter 14 | { 15 | @Autowired 16 | private BCryptPasswordEncoder passwordEncoder; 17 | 18 | @Override 19 | public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 20 | security 21 | .tokenKeyAccess("permitAll()") 22 | .checkTokenAccess("isAuthenticated()") 23 | .allowFormAuthenticationForClients(); 24 | } 25 | 26 | @Override 27 | public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 28 | clients 29 | .inMemory() 30 | .withClient("clientapp") 31 | .secret(passwordEncoder.encode("123456")) 32 | .authorizedGrantTypes("password", "authorization_code", "refresh_token") 33 | .authorities("READ_ONLY_CLIENT") 34 | .scopes("read_profile_info") 35 | .resourceIds("oauth2-resource") 36 | .redirectUris("http://localhost:8081/login") 37 | .accessTokenValiditySeconds(5000) 38 | .refreshTokenValiditySeconds(50000); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /oauth2/src/main/java/com/howtodoinjava/demo/config/OAuth2ResourceServer.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 5 | import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 6 | import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 7 | 8 | @Configuration 9 | @EnableResourceServer 10 | public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { 11 | @Override 12 | public void configure(HttpSecurity http) throws Exception { 13 | http.authorizeHttpRequests((authorizeHttpRequests) -> 14 | authorizeHttpRequests 15 | .requestMatchers("/**").permitAll() 16 | .requestMatchers("/api/v1/**").authenticated() 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /oauth2/src/main/java/com/howtodoinjava/demo/resource/RestResource.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.resource; 2 | 3 | import org.springframework.http.ResponseEntity; 4 | import org.springframework.security.core.context.SecurityContextHolder; 5 | import org.springframework.security.core.userdetails.User; 6 | import org.springframework.stereotype.Controller; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | 9 | @Controller 10 | public class RestResource 11 | { 12 | @RequestMapping("/api/users/me") 13 | public ResponseEntity profile() 14 | { 15 | //Build some dummy data to return for testing 16 | User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 17 | String email = user.getUsername() + "@howtodoinjava.com"; 18 | 19 | UserProfile profile = new UserProfile(); 20 | profile.setName(user.getUsername()); 21 | profile.setEmail(email); 22 | 23 | return ResponseEntity.ok(profile); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /oauth2/src/main/java/com/howtodoinjava/demo/resource/UserProfile.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.resource; 2 | 3 | public class UserProfile { 4 | private String name; 5 | private String email; 6 | 7 | public String getName() { 8 | return name; 9 | } 10 | 11 | public void setName(String name) { 12 | this.name = name; 13 | } 14 | 15 | public String getEmail() { 16 | return email; 17 | } 18 | 19 | public void setEmail(String email) { 20 | this.email = email; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return "UserProfile [name=" + name + ", email=" + email + "]"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /oauth2/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8080 2 | 3 | -------------------------------------------------------------------------------- /oauth2/src/test/java/com/howtodoinjava/demo/SpringOauth2ResourceServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class SpringOauth2ResourceServerApplicationTests { 7 | 8 | @Test 9 | public void contextLoads() { 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /packaging-war/.gitignore: -------------------------------------------------------------------------------- 1 | /.settings/ 2 | -------------------------------------------------------------------------------- /packaging-war/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | war 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | packaging-war 13 | packaging-war 14 | http://maven.apache.org 15 | 16 | UTF-8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-tomcat 27 | provided 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /packaging-war/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /packaging-war/src/main/java/com/springexamples/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @RequestMapping("/") 10 | public String index() { 11 | return "Greetings from Spring Examples !!"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /packaging-war/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /rest-crud-hibernate/README.md: -------------------------------------------------------------------------------- 1 | # REST CRUD 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring boot crud operations example with hibernate](https://howtodoinjava.com/spring-boot2/spring-boot-crud-hibernate/) 6 | 2. [Spring boot datasource configuration](https://howtodoinjava.com/spring-boot2/datasource-configuration/) 7 | 3. [@Repository annotation in Spring Boot](https://howtodoinjava.com/spring-boot/repository-annotation/) 8 | 4. [Spring Boot - @DataJpaTest](https://howtodoinjava.com/spring-boot2/testing/datajpatest-annotation/) -------------------------------------------------------------------------------- /rest-crud-hibernate/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | rest-crud-hibernate 14 | context-path-server-port 15 | http://maven.apache.org 16 | 17 | 18 | 17 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-data-jpa 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-thymeleaf 33 | 34 | 35 | com.h2database 36 | h2 37 | runtime 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-starter-test 42 | test 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/java/com/howtodoinjava/demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import com.howtodoinjava.demo.model.EmployeeEntity; 4 | import com.howtodoinjava.demo.repository.EmployeeRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | 10 | @SpringBootApplication 11 | public class DemoApplication implements CommandLineRunner { 12 | 13 | @Autowired 14 | EmployeeRepository repository; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(DemoApplication.class, args); 18 | } 19 | 20 | @Override 21 | public void run(String... args) throws Exception { 22 | repository.save(new EmployeeEntity(1L, "Lokesh", "Gupta", "admin@gmail.com")); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/java/com/howtodoinjava/demo/exception/RecordNotFoundException.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.exception; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.web.bind.annotation.ResponseStatus; 5 | 6 | @ResponseStatus(value = HttpStatus.NOT_FOUND) 7 | public class RecordNotFoundException extends Exception { 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | public RecordNotFoundException(String message) { 12 | super(message); 13 | } 14 | 15 | public RecordNotFoundException(String message, Throwable t) { 16 | super(message, t); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/java/com/howtodoinjava/demo/model/EmployeeEntity.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.model; 2 | 3 | import jakarta.persistence.Column; 4 | import jakarta.persistence.Entity; 5 | import jakarta.persistence.GeneratedValue; 6 | import jakarta.persistence.GenerationType; 7 | import jakarta.persistence.Id; 8 | import jakarta.persistence.Table; 9 | import lombok.AllArgsConstructor; 10 | import lombok.NoArgsConstructor; 11 | 12 | @Entity 13 | @Table(name="TBL_EMPLOYEES") 14 | @AllArgsConstructor 15 | @NoArgsConstructor 16 | public class EmployeeEntity { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | private Long id; 21 | 22 | @Column(name="first_name") 23 | private String firstName; 24 | 25 | @Column(name="last_name") 26 | private String lastName; 27 | 28 | @Column(name="email", nullable=false, length=200) 29 | private String email; 30 | 31 | public Long getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Long id) { 36 | this.id = id; 37 | } 38 | 39 | public String getFirstName() { 40 | return firstName; 41 | } 42 | 43 | public void setFirstName(String firstName) { 44 | this.firstName = firstName; 45 | } 46 | 47 | public String getLastName() { 48 | return lastName; 49 | } 50 | 51 | public void setLastName(String lastName) { 52 | this.lastName = lastName; 53 | } 54 | 55 | public String getEmail() { 56 | return email; 57 | } 58 | 59 | public void setEmail(String email) { 60 | this.email = email; 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | return "EmployeeEntity [id=" + id + ", firstName=" + firstName + 66 | ", lastName=" + lastName + ", email=" + email + "]"; 67 | } 68 | } -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/java/com/howtodoinjava/demo/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.howtodoinjava.demo.model.EmployeeEntity; 7 | 8 | @Repository 9 | public interface EmployeeRepository 10 | extends JpaRepository { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:file:C:/temp/test 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | 7 | # Enabling H2 Console 8 | spring.h2.console.enabled=true 9 | 10 | # Custom H2 Console URL 11 | spring.h2.console.path=/h2 12 | 13 | spring.jpa.hibernate.ddl-auto=update 14 | 15 | #Turn Statistics on and log SQL statements 16 | spring.jpa.show-sql=true 17 | spring.jpa.properties.hibernate.format_sql=true 18 | spring.jpa.properties.hibernate.generate_statistics=false 19 | 20 | logging.level.org.hibernate.type=trace 21 | logging.level.org.hibernate.stat=debug 22 | logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n 23 | 24 | server.port=3000 25 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO 2 | TBL_EMPLOYEES (first_name, last_name, email) 3 | VALUES 4 | ('Lokesh', 'Gupta', 'howtodoinjava@gmail.com'), 5 | ('John', 'Doe', 'xyz@email.com'); -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/resources/import.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO 2 | TBL_EMPLOYEES (first_name, last_name, email) 3 | VALUES 4 | ('Lokesh', 'Gupta', 'howtodoinjava@gmail.com'), 5 | ('John', 'Doe', 'xyz@email.com'); -------------------------------------------------------------------------------- /rest-crud-hibernate/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS TBL_EMPLOYEES; 2 | 3 | CREATE TABLE TBL_EMPLOYEES ( 4 | id INT AUTO_INCREMENT PRIMARY KEY, 5 | first_name VARCHAR(250) NOT NULL, 6 | last_name VARCHAR(250) NOT NULL, 7 | email VARCHAR(250) DEFAULT NULL 8 | ); -------------------------------------------------------------------------------- /rest-crud-hibernate/src/test/java/com/howtodoinjava/demo/DemoApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | 6 | public class DemoApplicationTests { 7 | 8 | @Test 9 | public void contextLoads() { 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /rest-crud-hibernate/src/test/java/com/howtodoinjava/demo/EmployeeRepositoryTest.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import com.howtodoinjava.demo.model.EmployeeEntity; 4 | import com.howtodoinjava.demo.repository.EmployeeRepository; 5 | import org.junit.jupiter.api.Assertions; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; 9 | 10 | @DataJpaTest 11 | public class EmployeeRepositoryTest 12 | { 13 | @Autowired 14 | EmployeeRepository repository; 15 | 16 | @Test 17 | public void testRepository() 18 | { 19 | EmployeeEntity emp = new EmployeeEntity(); 20 | emp.setFirstName("Lokesh"); 21 | emp.setLastName("Gupta"); 22 | emp.setEmail("howtodoinjava@gmail.com"); 23 | 24 | repository.save(emp); 25 | 26 | System.out.println(emp); 27 | 28 | Assertions.assertNotNull(emp.getId()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /rest-hateoas/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | rest-hateoas 12 | rest-hateoas 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-jersey 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-hateoas 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-test 29 | test 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/JerseydemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 6 | 7 | @SpringBootApplication 8 | public class JerseydemoApplication extends SpringBootServletInitializer 9 | { 10 | public static void main(String[] args) 11 | { 12 | new JerseydemoApplication().configure(new SpringApplicationBuilder(JerseydemoApplication.class)).run(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/config/JerseyConfig.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.glassfish.jersey.server.ResourceConfig; 4 | import org.springframework.stereotype.Component; 5 | 6 | import com.springexamples.demo.UserResource; 7 | 8 | @Component 9 | public class JerseyConfig extends ResourceConfig 10 | { 11 | public JerseyConfig() 12 | { 13 | register(UserResource.class); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/dao/UserDB.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.dao; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | 6 | import com.springexamples.demo.model.User; 7 | 8 | public class UserDB { 9 | 10 | private static HashMap DB = new HashMap<>(); 11 | 12 | static { 13 | User one = new User(1, "Lokesh", "Gupta"); 14 | User two = new User(2, "Amit", "Singhal"); 15 | User three = new User(3, "Kirti", "Mishra"); 16 | 17 | DB.put(1, one); 18 | DB.put(2, two); 19 | DB.put(3, three); 20 | } 21 | 22 | public static ArrayList getUsers() 23 | { 24 | ArrayList list = new ArrayList<>(DB.values()); 25 | return list; 26 | } 27 | public static User getUserById( Integer id) 28 | { 29 | return DB.get(id); 30 | } 31 | public static void addUser(User user) { 32 | DB.put(user.getUserId(), user); 33 | } 34 | 35 | public static void updateUser(User user) { 36 | DB.put(user.getUserId(), user); 37 | } 38 | 39 | public static void removeUser(Integer id) { 40 | DB.remove(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/model/Report.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import java.io.Serializable; 4 | import jakarta.xml.bind.annotation.XmlRootElement; 5 | import org.springframework.hateoas.RepresentationModel; 6 | 7 | @XmlRootElement(name="user-report") 8 | public class Report extends RepresentationModel implements Serializable { 9 | 10 | private static final long serialVersionUID = 1L; 11 | 12 | public Report() { 13 | super(); 14 | } 15 | 16 | public Report(String data) { 17 | super(); 18 | this.data = data; 19 | } 20 | 21 | private String data; 22 | 23 | public String getData() { 24 | return data; 25 | } 26 | 27 | public void setData(String data) { 28 | this.data = data; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.xml.bind.annotation.XmlAccessType; 6 | import jakarta.xml.bind.annotation.XmlAccessorType; 7 | import jakarta.xml.bind.annotation.XmlAttribute; 8 | import jakarta.xml.bind.annotation.XmlElement; 9 | import jakarta.xml.bind.annotation.XmlRootElement; 10 | 11 | import org.springframework.hateoas.RepresentationModel; 12 | 13 | @XmlAccessorType(XmlAccessType.NONE) 14 | @XmlRootElement(name = "user") 15 | public class User extends RepresentationModel implements Serializable { 16 | 17 | private static final long serialVersionUID = 1L; 18 | 19 | @XmlAttribute(name = "userId") 20 | private int userId; 21 | 22 | @XmlElement(name = "firstName") 23 | private String firstName; 24 | 25 | @XmlElement(name = "lastName") 26 | private String lastName; 27 | 28 | public User(int id, String firstName, String lastName) { 29 | super(); 30 | this.userId = id; 31 | this.firstName = firstName; 32 | this.lastName = lastName; 33 | } 34 | 35 | public User() { 36 | super(); 37 | } 38 | 39 | public int getUserId() { 40 | return userId; 41 | } 42 | 43 | public void setUserId(int userId) { 44 | this.userId = userId; 45 | } 46 | 47 | public String getFirstName() { 48 | return firstName; 49 | } 50 | public void setFirstName(String firstName) { 51 | this.firstName = firstName; 52 | } 53 | public String getLastName() { 54 | return lastName; 55 | } 56 | public void setLastName(String lastName) { 57 | this.lastName = lastName; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /rest-hateoas/src/main/java/com/springexamples/demo/model/Users.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | 6 | import jakarta.xml.bind.annotation.XmlAccessType; 7 | import jakarta.xml.bind.annotation.XmlAccessorType; 8 | import jakarta.xml.bind.annotation.XmlElement; 9 | import jakarta.xml.bind.annotation.XmlRootElement; 10 | 11 | import org.springframework.hateoas.RepresentationModel; 12 | 13 | 14 | @XmlAccessorType(XmlAccessType.NONE) 15 | @XmlRootElement(name = "users") 16 | public class Users extends RepresentationModel implements Serializable{ 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | @XmlElement(name="user") 21 | private ArrayList users; 22 | 23 | public ArrayList getUsers() { 24 | return users; 25 | } 26 | 27 | public void setUsers(ArrayList users) { 28 | this.users = users; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /rest-hateoas/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | 4 | public class AppTest { 5 | 6 | 7 | } 8 | -------------------------------------------------------------------------------- /rest-jersey-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | rest-jersey-example 12 | rest-jersey-example 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-jersey 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /rest-jersey-example/src/main/java/com/springexamples/demo/JerseydemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 6 | 7 | @SpringBootApplication 8 | public class JerseydemoApplication extends SpringBootServletInitializer 9 | { 10 | public static void main(String[] args) 11 | { 12 | new JerseydemoApplication().configure(new SpringApplicationBuilder(JerseydemoApplication.class)).run(args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rest-jersey-example/src/main/java/com/springexamples/demo/config/JerseyConfig.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.glassfish.jersey.server.ResourceConfig; 4 | import org.springframework.stereotype.Component; 5 | 6 | import com.springexamples.demo.UserResource; 7 | 8 | @Component 9 | public class JerseyConfig extends ResourceConfig 10 | { 11 | public JerseyConfig() 12 | { 13 | register(UserResource.class); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /rest-jersey-example/src/main/java/com/springexamples/demo/dao/UserDB.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.dao; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | 6 | import com.springexamples.demo.model.User; 7 | 8 | public class UserDB { 9 | 10 | private static HashMap DB = new HashMap<>(); 11 | 12 | static { 13 | User one = new User(1, "Lokesh", "Gupta"); 14 | User two = new User(2, "Amit", "Singhal"); 15 | User three = new User(3, "Kirti", "Mishra"); 16 | 17 | DB.put(1, one); 18 | DB.put(2, two); 19 | DB.put(3, three); 20 | } 21 | 22 | public static ArrayList getUsers() 23 | { 24 | ArrayList list = new ArrayList<>(DB.values()); 25 | return list; 26 | } 27 | public static User getUserById( Integer id) 28 | { 29 | return DB.get(id); 30 | } 31 | public static void addUser(User user) { 32 | DB.put(user.getId(), user); 33 | } 34 | 35 | public static void updateUser(User user) { 36 | DB.put(user.getId(), user); 37 | } 38 | 39 | public static void removeUser(Integer id) { 40 | DB.remove(id); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /rest-jersey-example/src/main/java/com/springexamples/demo/model/User.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.xml.bind.annotation.XmlAccessType; 6 | import jakarta.xml.bind.annotation.XmlAccessorType; 7 | import jakarta.xml.bind.annotation.XmlAttribute; 8 | import jakarta.xml.bind.annotation.XmlElement; 9 | import jakarta.xml.bind.annotation.XmlRootElement; 10 | 11 | @XmlAccessorType(XmlAccessType.NONE) 12 | @XmlRootElement(name = "user") 13 | public class User implements Serializable { 14 | 15 | private static final long serialVersionUID = 1L; 16 | 17 | @XmlAttribute(name = "id") 18 | private int id; 19 | 20 | @XmlElement(name = "firstName") 21 | private String firstName; 22 | 23 | @XmlElement(name = "lastName") 24 | private String lastName; 25 | 26 | public User(int id, String firstName, String lastName) { 27 | super(); 28 | this.id = id; 29 | this.firstName = firstName; 30 | this.lastName = lastName; 31 | } 32 | 33 | public User() { 34 | super(); 35 | } 36 | 37 | public int getId() { 38 | return id; 39 | } 40 | public void setId(int id) { 41 | this.id = id; 42 | } 43 | public String getFirstName() { 44 | return firstName; 45 | } 46 | public void setFirstName(String firstName) { 47 | this.firstName = firstName; 48 | } 49 | public String getLastName() { 50 | return lastName; 51 | } 52 | public void setLastName(String lastName) { 53 | this.lastName = lastName; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /rest-jersey-example/src/main/java/com/springexamples/demo/model/Users.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import java.util.ArrayList; 4 | 5 | import jakarta.xml.bind.annotation.XmlAccessType; 6 | import jakarta.xml.bind.annotation.XmlAccessorType; 7 | import jakarta.xml.bind.annotation.XmlElement; 8 | import jakarta.xml.bind.annotation.XmlRootElement; 9 | 10 | @XmlAccessorType(XmlAccessType.NONE) 11 | @XmlRootElement(name = "users") 12 | public class Users { 13 | 14 | @XmlElement(name="user") 15 | private ArrayList users; 16 | 17 | public ArrayList getUsers() { 18 | return users; 19 | } 20 | 21 | public void setUsers(ArrayList users) { 22 | this.users = users; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rest-jersey-example/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /resttemplate/README.md: -------------------------------------------------------------------------------- 1 | # RestTemplate 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring RestTemplate](https://howtodoinjava.com/spring-boot2/resttemplate/spring-restful-client-resttemplate-example/) 6 | 2. [Spring Boot - @RestClientTest](https://howtodoinjava.com/spring-boot2/testing/restclienttest-test-services/) 7 | 3. [Spring RestTemplate with HttpClient configuration](https://howtodoinjava.com/spring-boot2/resttemplate/resttemplate-httpclient-java-config/) -------------------------------------------------------------------------------- /resttemplate/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | 9 | org.springframework.boot 10 | spring-boot-starter-parent 11 | 2.2.2.RELEASE 12 | 13 | 14 | 15 | resttemplate 16 | resttemplate 17 | http://maven.apache.org 18 | 19 | UTF-8 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.apache.httpcomponents 28 | httpclient 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-test 33 | test 34 | 35 | 36 | jakarta.xml.bind 37 | jakarta.xml.bind-api 38 | 3.0.1 39 | compile 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.context.annotation.Import; 8 | 9 | import com.springexamples.demo.config.HttpClientConfig; 10 | import com.springexamples.demo.config.RestTemplateConfig; 11 | import com.springexamples.demo.service.UserService; 12 | 13 | @SpringBootApplication 14 | @Import({ HttpClientConfig.class, RestTemplateConfig.class }) 15 | public class Application implements CommandLineRunner { 16 | 17 | @Autowired 18 | private UserService userService; 19 | 20 | public static void main(String[] args) { 21 | SpringApplication.run(Application.class, args); 22 | } 23 | 24 | @Override 25 | public void run(String... args) throws Exception { 26 | //userService.testUserService(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/config/RestTemplateConfig.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.apache.http.impl.client.CloseableHttpClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 7 | import org.springframework.scheduling.TaskScheduler; 8 | import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | public class RestTemplateConfig { 12 | 13 | @Autowired 14 | CloseableHttpClient httpClient; 15 | 16 | @Bean 17 | public RestTemplate restTemplate() { 18 | RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory()); 19 | return restTemplate; 20 | } 21 | 22 | @Bean 23 | public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { 24 | HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); 25 | clientHttpRequestFactory.setHttpClient(httpClient); 26 | return clientHttpRequestFactory; 27 | } 28 | 29 | @Bean 30 | public TaskScheduler taskScheduler() { 31 | ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); 32 | scheduler.setThreadNamePrefix("poolScheduler"); 33 | scheduler.setPoolSize(50); 34 | return scheduler; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/model/EmployeeListVO.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import jakarta.xml.bind.annotation.XmlRootElement; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | @XmlRootElement(name="employees") 9 | public class EmployeeListVO 10 | { 11 | private List employees = new ArrayList(); 12 | 13 | public List getEmployees() { 14 | return employees; 15 | } 16 | 17 | public void setEmployees(List employees) { 18 | this.employees = employees; 19 | } 20 | } -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/model/EmployeeVO.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.model; 2 | 3 | import jakarta.xml.bind.annotation.*; 4 | 5 | import java.io.Serializable; 6 | 7 | @XmlRootElement (name = "employee") 8 | @XmlAccessorType(XmlAccessType.NONE) 9 | public class EmployeeVO implements Serializable 10 | { 11 | private static final long serialVersionUID = 1L; 12 | 13 | @XmlAttribute 14 | private Integer id; 15 | 16 | @XmlElement 17 | private String firstName; 18 | 19 | @XmlElement 20 | private String lastName; 21 | 22 | @XmlElement 23 | private String email; 24 | 25 | public EmployeeVO(Integer id, String firstName, String lastName, String email) { 26 | super(); 27 | this.id = id; 28 | this.firstName = firstName; 29 | this.lastName = lastName; 30 | this.email = email; 31 | } 32 | 33 | public EmployeeVO(){ 34 | 35 | } 36 | 37 | public Integer getId() { 38 | return id; 39 | } 40 | 41 | public void setId(Integer id) { 42 | this.id = id; 43 | } 44 | 45 | public String getFirstName() { 46 | return firstName; 47 | } 48 | 49 | public void setFirstName(String firstName) { 50 | this.firstName = firstName; 51 | } 52 | 53 | public String getLastName() { 54 | return lastName; 55 | } 56 | 57 | public void setLastName(String lastName) { 58 | this.lastName = lastName; 59 | } 60 | 61 | public String getEmail() { 62 | return email; 63 | } 64 | 65 | public void setEmail(String email) { 66 | this.email = email; 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "EmployeeVO [id=" + id + ", firstName=" + firstName 72 | + ", lastName=" + lastName + ", email=" + email + "]"; 73 | } 74 | } -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.service; 2 | 3 | public interface UserService { 4 | 5 | public String testUserService(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /resttemplate/src/main/java/com/springexamples/demo/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.service.impl; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpEntity; 5 | import org.springframework.http.HttpHeaders; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | import com.springexamples.demo.service.UserService; 10 | 11 | @Service 12 | public class UserServiceImpl implements UserService { 13 | 14 | @Autowired 15 | RestTemplate restTemplate; 16 | 17 | @Override 18 | public String testUserService() 19 | { 20 | final String uri = "http://localhost:8080/users"; 21 | 22 | HttpHeaders headers = new HttpHeaders(); 23 | headers.set("Header", "value"); 24 | headers.set("Other-Header", "othervalue"); 25 | 26 | HttpEntity entity = new HttpEntity(headers); 27 | 28 | String result = restTemplate.getForObject(uri, String.class); 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /resttemplate/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9000 2 | server.contextPath=/springexamples 3 | 4 | spring.main.allow-bean-definition-overriding=true -------------------------------------------------------------------------------- /resttemplate/src/test/java/com/springexamples/demo/EmployeeControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Import; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import com.springexamples.demo.config.HttpClientConfig; 11 | import com.springexamples.demo.config.RestTemplateConfig; 12 | import com.springexamples.demo.web.EmployeeController; 13 | 14 | /*@RunWith(SpringRunner.class) 15 | @Import({EmployeeController.class, HttpClientConfig.class, RestTemplateConfig.class})*/ 16 | public class EmployeeControllerTest 17 | { 18 | /*@Autowired 19 | RestTemplate restTemplate; 20 | 21 | *//*@Test 22 | public void testGetEmployees() 23 | { 24 | final String uri = "http://localhost:8080/employees"; 25 | 26 | RestTemplate restTemplate = new RestTemplate(); 27 | String result = restTemplate.getForObject(uri, String.class); 28 | 29 | System.out.println(result); 30 | }*/ 31 | } 32 | -------------------------------------------------------------------------------- /resttemplate/src/test/java/com/springexamples/demo/UserServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import static org.assertj.core.api.Assertions.assertThat; 4 | import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; 5 | import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; 11 | import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; 12 | import org.springframework.http.MediaType; 13 | import org.springframework.test.context.junit4.SpringRunner; 14 | import org.springframework.test.web.client.MockRestServiceServer; 15 | 16 | import com.springexamples.demo.service.UserService; 17 | import com.springexamples.demo.service.impl.UserServiceImpl; 18 | 19 | @RunWith(SpringRunner.class) 20 | @RestClientTest(UserServiceImpl.class) 21 | @AutoConfigureWebClient(registerRestTemplate = true) 22 | public class UserServiceTest 23 | { 24 | @Autowired 25 | private MockRestServiceServer server; 26 | 27 | @Autowired 28 | private UserService client; 29 | 30 | @Test 31 | public void testServiceCall() 32 | { 33 | this.server.expect(requestTo("http://localhost:8080/users")) 34 | .andRespond(withSuccess("", MediaType.TEXT_PLAIN)); 35 | 36 | String userServiceResponse = client.testUserService(); 37 | 38 | assertThat(userServiceResponse).isEqualTo(""); 39 | 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /server-customization/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | server-customization 12 | server-customization 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-test 25 | test 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /server-customization/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | /*import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; 6 | import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; 7 | import org.springframework.boot.web.server.ErrorPage; 8 | import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.http.HttpStatus;*/ 11 | 12 | @SpringBootApplication 13 | public class App 14 | { 15 | public static void main( String[] args ) 16 | { 17 | SpringApplication app = new SpringApplication(App.class); 18 | app.run(args); 19 | } 20 | 21 | /*@Bean 22 | public ConfigurableServletWebServerFactory tomcatServerFactory() 23 | { 24 | TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); 25 | factory.setPort(9000); 26 | factory.setContextPath("/myapp"); 27 | factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); 28 | return factory; 29 | }*/ 30 | 31 | /* @Bean 32 | public ConfigurableServletWebServerFactory jettyServerFactory() 33 | { 34 | JettyServletWebServerFactory factory = new JettyServletWebServerFactory(); 35 | factory.setPort(9000); 36 | factory.setContextPath("/myapp"); 37 | factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); 38 | return factory; 39 | }*/ 40 | } 41 | -------------------------------------------------------------------------------- /server-customization/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #server.port=8080 2 | #server.servlet.context-path=/home 3 | 4 | ####Jetty specific properties######## 5 | 6 | #server.jetty.acceptors= # Number of acceptor threads to use. 7 | #server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content. 8 | #server.jetty.selectors= # Number of selector threads to use. -------------------------------------------------------------------------------- /server-customization/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/HELP.md: -------------------------------------------------------------------------------- 1 | # Read Me First 2 | The following was discovered as part of building this project: 3 | 4 | * The original package name 'com.howtodoinjava.spring-boot-actuator-example' is invalid and this project uses 'com.howtodoinjava.springbootactuatorexample' instead. 5 | 6 | # Getting Started 7 | 8 | ### Reference Documentation 9 | For further reference, please consider the following sections: 10 | 11 | * [Official Apache Maven documentation](https://maven.apache.org/guides/index.html) 12 | * [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/) 13 | * [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/maven-plugin/reference/html/#build-image) 14 | * [Spring Web](https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications) 15 | * [Spring Boot Actuator](https://docs.spring.io/spring-boot/docs/2.3.2.RELEASE/reference/htmlsingle/#production-ready) 16 | 17 | ### Guides 18 | The following guides illustrate how to use some features concretely: 19 | 20 | * [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) 21 | * [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) 22 | * [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/) 23 | * [Building a RESTful Web Service with Spring Boot Actuator](https://spring.io/guides/gs/actuator-service/) 24 | 25 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 3.1.2 9 | 10 | 11 | com.howtodoinjava 12 | spring-boot-actuator-example 13 | 0.0.1-SNAPSHOT 14 | spring-boot-actuator-example 15 | Spring Boot Actuator Endpoint Example 16 | 17 | 18 | 17 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-actuator 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/src/main/java/com/howtodoinjava/demo/SimpleRestController.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import java.time.LocalDateTime; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | @RestController 8 | public class SimpleRestController { 9 | @GetMapping("/example") 10 | public String example() { 11 | return "Hello User !! " + LocalDateTime.now(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/src/main/java/com/howtodoinjava/demo/SpringBootActuatorExampleApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootActuatorExampleApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootActuatorExampleApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-actuator-example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | management.security.enabled=false 2 | management.endpoints.web.exposure.include=* 3 | management.endpoint.env.show-values=ALWAYS 4 | 5 | management.info.env.enabled=true 6 | management.info.build.enabled=true 7 | management.info.git.enabled=true 8 | management.info.java.enabled=true 9 | management.info.os.enabled=true 10 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/java/com/howtodoinjava/hateoas/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo; 2 | 3 | import com.howtodoinjava.hateoas.demo.dao.EmployeeRepository; 4 | import com.howtodoinjava.hateoas.demo.model.Employee; 5 | import java.util.List; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.CommandLineRunner; 8 | import org.springframework.boot.SpringApplication; 9 | import org.springframework.boot.autoconfigure.SpringBootApplication; 10 | 11 | @SpringBootApplication 12 | public class Application implements CommandLineRunner { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(Application.class, args); 16 | } 17 | 18 | @Autowired 19 | EmployeeRepository repository; 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | repository.saveAll(List.of( 24 | new Employee(null, "Alex", "Dave", "alex@gmail.com"), 25 | new Employee(null, "Brian", "Dave", "brian@gmail.com"), 26 | new Employee(null, "Charles", "Dave", "charles@gmail.com"))); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/java/com/howtodoinjava/hateoas/demo/dao/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo.dao; 2 | 3 | import com.howtodoinjava.hateoas.demo.model.Employee; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | import org.springframework.stereotype.Repository; 6 | 7 | @Repository 8 | public interface EmployeeRepository extends JpaRepository { 9 | } 10 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/java/com/howtodoinjava/hateoas/demo/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo.model; 2 | 3 | import jakarta.persistence.Entity; 4 | import jakarta.persistence.GeneratedValue; 5 | import jakarta.persistence.GenerationType; 6 | import jakarta.persistence.Id; 7 | import jakarta.xml.bind.annotation.XmlRootElement; 8 | import lombok.AllArgsConstructor; 9 | import lombok.Data; 10 | import lombok.NoArgsConstructor; 11 | import org.springframework.hateoas.RepresentationModel; 12 | 13 | @Entity 14 | @Data 15 | @AllArgsConstructor 16 | @NoArgsConstructor 17 | @XmlRootElement(name = "employee") 18 | public class Employee extends RepresentationModel { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | private Integer id; 23 | private String firstName; 24 | private String lastName; 25 | private String email; 26 | } 27 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/java/com/howtodoinjava/hateoas/demo/model/EmployeeList.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo.model; 2 | 3 | import jakarta.xml.bind.annotation.XmlRootElement; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import lombok.AllArgsConstructor; 7 | import lombok.Data; 8 | import lombok.NoArgsConstructor; 9 | import org.springframework.hateoas.RepresentationModel; 10 | 11 | @XmlRootElement(name = "employees") 12 | @Data 13 | @AllArgsConstructor 14 | @NoArgsConstructor 15 | public class EmployeeList extends RepresentationModel { 16 | 17 | private List employees = new ArrayList(); 18 | } 19 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/java/com/howtodoinjava/hateoas/demo/model/EmployeeReportResult.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo.model; 2 | 3 | import jakarta.xml.bind.annotation.XmlRootElement; 4 | import org.springframework.hateoas.RepresentationModel; 5 | 6 | @XmlRootElement(name = "employee-report") 7 | public class EmployeeReportResult extends RepresentationModel { 8 | 9 | // ... 10 | } 11 | -------------------------------------------------------------------------------- /spring-boot-hateoas/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:test 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect -------------------------------------------------------------------------------- /spring-boot-hateoas/src/test/java/com/howtodoinjava/hateoas/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hateoas.demo; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertTrue; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class AppTest { 9 | 10 | @Test 11 | public void contextLoads() { 12 | assertTrue(true); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-reactive/README.md: -------------------------------------------------------------------------------- 1 | # Spring Reactive 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring WebClient Tutorial](https://howtodoinjava.com/spring-webflux/webclient-get-post-example/) 6 | 2. [Spring WebClient – How to set timeouts](https://howtodoinjava.com/spring-webflux/webclient-set-timeouts/) 7 | -------------------------------------------------------------------------------- /spring-reactive/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | spring-reactive 13 | spring-reactive 14 | http://maven.apache.org 15 | 16 | UTF-8 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-webflux 22 | 23 | 24 | org.projectreactor 25 | reactor-spring 26 | 1.0.1.RELEASE 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/WebfluxFunctionalApp.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class WebfluxFunctionalApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(WebfluxFunctionalApp.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo.config; 2 | 3 | import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 7 | import org.springframework.core.io.ClassPathResource; 8 | 9 | @Configuration 10 | public class AppConfig 11 | { 12 | @Bean 13 | public static PropertySourcesPlaceholderConfigurer getPropertyPlaceholderConfigurer() 14 | { 15 | PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); 16 | ppc.setLocation(new ClassPathResource("application.properties")); 17 | ppc.setIgnoreUnresolvablePlaceholders(true); 18 | return ppc; 19 | } 20 | } -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/config/WebFluxConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo.config; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.http.HttpHeaders; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.client.reactive.ClientHttpConnector; 10 | import org.springframework.http.client.reactive.ReactorClientHttpConnector; 11 | import org.springframework.web.reactive.config.EnableWebFlux; 12 | import org.springframework.web.reactive.config.WebFluxConfigurer; 13 | import org.springframework.web.reactive.function.client.WebClient; 14 | 15 | import io.netty.channel.ChannelOption; 16 | import io.netty.handler.timeout.ReadTimeoutHandler; 17 | import io.netty.handler.timeout.WriteTimeoutHandler; 18 | import reactor.netty.http.client.HttpClient; 19 | 20 | @Configuration 21 | @EnableWebFlux 22 | public class WebFluxConfig implements WebFluxConfigurer 23 | { 24 | Logger logger = LoggerFactory.getLogger(WebFluxConfig.class); 25 | 26 | @Bean 27 | public WebClient getWebClient() 28 | { 29 | HttpClient httpClient = HttpClient.create() 30 | .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) 10000) 31 | .doOnConnected(conn -> 32 | conn.addHandlerLast(new ReadTimeoutHandler(10)) 33 | .addHandlerLast(new WriteTimeoutHandler(10))); 34 | 35 | ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient.wiretap(true)); 36 | 37 | return WebClient.builder() 38 | .baseUrl("http://localhost:3000") 39 | .clientConnector(connector) 40 | .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) 41 | .build(); 42 | } 43 | } -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Employee implements Serializable { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | long id; 10 | String name; 11 | String status; 12 | 13 | public long getId() { 14 | return id; 15 | } 16 | 17 | public void setId(long id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | public String getStatus() { 30 | return status; 31 | } 32 | 33 | public void setStatus(String status) { 34 | this.status = status; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Employee [id=" + id + ", name=" + name + ", status=" + status + "]"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo.service; 2 | 3 | import java.time.Duration; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.web.reactive.function.client.WebClient; 9 | 10 | import com.howtodoinjava.reactive.demo.model.Employee; 11 | 12 | import reactor.core.publisher.Flux; 13 | import reactor.core.publisher.Mono; 14 | 15 | @Service 16 | public class EmployeeService implements IEmployeeService 17 | { 18 | @Autowired 19 | WebClient webClient; 20 | 21 | public Flux findAll() 22 | { 23 | return webClient.get() 24 | .uri("/employees") 25 | .retrieve() 26 | .bodyToFlux(Employee.class) 27 | .timeout(Duration.ofMillis(10_000)); 28 | } 29 | 30 | public Mono create(Employee empl) 31 | { 32 | return webClient.post() 33 | .uri("/employees") 34 | .body(Mono.just(empl), Employee.class) 35 | .retrieve() 36 | .bodyToMono(Employee.class) 37 | .timeout(Duration.ofMillis(10_000)); 38 | } 39 | 40 | public Mono findById(Integer id) 41 | { 42 | return webClient.get() 43 | .uri("/employees/" + id) 44 | .retrieve() 45 | .onStatus(httpStatus -> HttpStatus.NOT_FOUND.equals(httpStatus), 46 | clientResponse -> Mono.empty()) 47 | .bodyToMono(Employee.class); 48 | } 49 | 50 | public Mono update(Employee e) 51 | { 52 | return webClient.put() 53 | .uri("/employees/" + e.getId()) 54 | .body(Mono.just(e), Employee.class) 55 | .retrieve() 56 | .bodyToMono(Employee.class); 57 | } 58 | 59 | public Mono delete(Integer id) 60 | { 61 | return webClient.delete() 62 | .uri("/employees/" +id) 63 | .retrieve() 64 | .bodyToMono(Void.class); 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /spring-reactive/src/main/java/com/howtodoinjava/reactive/demo/service/IEmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.reactive.demo.service; 2 | 3 | import com.howtodoinjava.reactive.demo.model.Employee; 4 | 5 | import reactor.core.publisher.Flux; 6 | import reactor.core.publisher.Mono; 7 | 8 | public interface IEmployeeService 9 | { 10 | Flux findAll(); 11 | 12 | Mono findById(Integer id); 13 | 14 | Mono create(Employee e); 15 | 16 | Mono update(Employee e); 17 | 18 | Mono delete(Integer id); 19 | } -------------------------------------------------------------------------------- /spring-reactive/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | logging.level.org.springframework=DEBUG 2 | server.servlet.context-path=/employee-management 3 | spring.codec.max-in-memory-size=1MB 4 | 5 | logging.level.reactor.netty.http.client.HttpClient=DEBUG -------------------------------------------------------------------------------- /spring-reactive/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-rmi/README.md: -------------------------------------------------------------------------------- 1 | # Spring remoting 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring Boot RMI](https://howtodoinjava.com/spring-boot2/spring-remoting-rmi-hessian/) 6 | -------------------------------------------------------------------------------- /spring-rmi/hessian-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.howtodoinjava.example 8 | hessian-client 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | hessian-client 13 | Demo project for Spring Boot 14 | 15 | 16 | com.springexamples.demo 17 | SpringExamples 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | com.caucho 30 | hessian 31 | 4.0.62 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-web 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-rmi/hessian-client/src/main/java/com/howtodoinjava/example/hessianclient/HessianClientApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.example.hessianclient; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.ConfigurableApplicationContext; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.remoting.caucho.HessianProxyFactoryBean; 8 | 9 | import com.howtodoinjava.hessianserver.hessian.HelloWorld; 10 | 11 | @SpringBootApplication 12 | public class HessianClientApplication { 13 | 14 | @Bean 15 | public HessianProxyFactoryBean hessianInvoker() { 16 | HessianProxyFactoryBean invoker = new HessianProxyFactoryBean(); 17 | invoker.setServiceUrl("http://localhost:8080/hellohessian"); 18 | invoker.setServiceInterface(HelloWorld.class); 19 | return invoker; 20 | } 21 | 22 | public static void main(String[] args) { 23 | ConfigurableApplicationContext context = SpringApplication.run(HessianClientApplication.class, args); 24 | System.out.println("========Client Side==============="); 25 | HelloWorld helloWorld = context.getBean(HelloWorld.class); 26 | System.out.println(helloWorld.sayHelloWithHessian("Sajal")); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-rmi/hessian-client/src/main/java/com/howtodoinjava/hessianserver/hessian/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver.hessian; 2 | 3 | public interface HelloWorld { 4 | public String sayHelloWithHessian(String msg); 5 | } 6 | -------------------------------------------------------------------------------- /spring-rmi/hessian-client/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 9090 -------------------------------------------------------------------------------- /spring-rmi/hessian-client/src/test/java/com/howtodoinjava/example/hessianclient/HessianClientApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.example.hessianclient; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class HessianClientApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.howtodoinjava 7 | hessian-server 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | hessian-server 12 | Demo project for Spring Boot 13 | 14 | 15 | com.springexamples.demo 16 | SpringExamples 17 | 0.0.1-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | 29 | com.caucho 30 | hessian 31 | 4.0.51 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/main/java/com/howtodoinjava/hessianserver/HessianConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.remoting.caucho.HessianServiceExporter; 6 | import org.springframework.remoting.support.RemoteExporter; 7 | 8 | import com.howtodoinjava.hessianserver.hessian.HelloWorld; 9 | import com.howtodoinjava.hessianserver.hessian.HelloWorldImpl; 10 | 11 | @Configuration 12 | public class HessianConfiguration { 13 | 14 | @Bean(name = "/hellohessian") 15 | RemoteExporter sayHelloServiceHessian() { 16 | HessianServiceExporter exporter = new HessianServiceExporter(); 17 | exporter.setService(new HelloWorldImpl()); 18 | exporter.setServiceInterface(HelloWorld.class); 19 | return exporter; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/main/java/com/howtodoinjava/hessianserver/HessianServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class HessianServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(HessianServerApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/main/java/com/howtodoinjava/hessianserver/hessian/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver.hessian; 2 | 3 | public interface HelloWorld { 4 | public String sayHelloWithHessian(String msg); 5 | } 6 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/main/java/com/howtodoinjava/hessianserver/hessian/HelloWorldImpl.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver.hessian; 2 | 3 | import java.util.Date; 4 | 5 | public class HelloWorldImpl implements HelloWorld { 6 | 7 | @Override 8 | public String sayHelloWithHessian(String msg) { 9 | System.out.println("=============server side=============="); 10 | System.out.println("msg : " + msg); 11 | return "Hello " + msg + " Response time :: " + new Date(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/spring-rmi/hessian-server/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-rmi/hessian-server/src/test/java/com/howtodoinjava/hessianserver/HessianServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.hessianserver; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | 9 | @SpringBootTest 10 | public class HessianServerApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /spring-rmi/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.howtodoinjava 8 | spring-rmi 9 | pom 10 | 11 | spring-rmi 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 1.8 17 | 18 | 19 | 20 | com.springexamples.demo 21 | SpringExamples 22 | 0.0.1-SNAPSHOT 23 | 24 | 25 | 26 | hessian-server 27 | hessian-client 28 | rmi-server 29 | rmi-client 30 | 31 | -------------------------------------------------------------------------------- /spring-rmi/rmi-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.howtodoinjava 7 | rmi-client 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | rmi-client 12 | Demo project for Spring Boot 13 | 14 | 15 | com.springexamples.demo 16 | SpringExamples 17 | 0.0.1-SNAPSHOT 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-maven-plugin 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-rmi/rmi-client/src/main/java/com/example/springrmiclient/SpringRmiClientApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiclient; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.remoting.rmi.RmiProxyFactoryBean; 7 | 8 | import com.example.springrmiserver.service.HelloWorldRMI; 9 | 10 | @SpringBootApplication 11 | public class SpringRmiClientApplication { 12 | 13 | @Bean 14 | RmiProxyFactoryBean rmiProxy() { 15 | RmiProxyFactoryBean bean = new RmiProxyFactoryBean(); 16 | bean.setServiceInterface(HelloWorldRMI.class); 17 | bean.setServiceUrl("rmi://localhost:1099/helloworldrmi"); 18 | 19 | return bean; 20 | } 21 | 22 | public static void main(String[] args) { 23 | HelloWorldRMI helloWorldRMI = SpringApplication.run(SpringRmiClientApplication.class, args).getBean(HelloWorldRMI.class); 24 | System.out.println("================Client Side ========================"); 25 | System.out.println(helloWorldRMI.sayHelloRmi("Sajal")); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-rmi/rmi-client/src/main/java/com/example/springrmiserver/service/HelloWorldRMI.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver.service; 2 | 3 | public interface HelloWorldRMI { 4 | public String sayHelloRmi(String msg); 5 | } 6 | -------------------------------------------------------------------------------- /spring-rmi/rmi-client/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/spring-rmi/rmi-client/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-rmi/rmi-client/src/test/java/com/example/springrmiclient/SpringRmiClientApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiclient; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | 6 | 7 | public class SpringRmiClientApplicationTests { 8 | 9 | @Test 10 | public void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.howtodoinjava 8 | rmi-server 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | rmi-server 13 | Demo project for Spring Boot 14 | 15 | 16 | com.howtodoinjava 17 | spring-rmi 18 | 0.0.1-SNAPSHOT 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/main/java/com/example/springrmiserver/Config.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.remoting.rmi.RmiServiceExporter; 6 | import org.springframework.remoting.support.RemoteExporter; 7 | 8 | import com.example.springrmiserver.service.HelloWorldRMI; 9 | import com.example.springrmiserver.service.HelloWorldRMIimpl; 10 | 11 | @Configuration 12 | public class Config { 13 | 14 | @Bean 15 | RemoteExporter registerRMIExporter() { 16 | 17 | RmiServiceExporter exporter = new RmiServiceExporter(); 18 | exporter.setServiceName("helloworldrmi"); 19 | exporter.setServiceInterface(HelloWorldRMI.class); 20 | exporter.setService(new HelloWorldRMIimpl()); 21 | 22 | return exporter; 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/main/java/com/example/springrmiserver/SpringRmiServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRmiServerApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRmiServerApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/main/java/com/example/springrmiserver/service/HelloWorldRMI.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver.service; 2 | 3 | public interface HelloWorldRMI { 4 | public String sayHelloRmi(String msg); 5 | } 6 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/main/java/com/example/springrmiserver/service/HelloWorldRMIimpl.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver.service; 2 | 3 | import java.util.Date; 4 | 5 | public class HelloWorldRMIimpl implements HelloWorldRMI { 6 | 7 | @Override 8 | public String sayHelloRmi(String msg) { 9 | System.out.println("================Server Side ========================"); 10 | System.out.println("Inside Rmi IMPL - Incoming msg : " + msg); 11 | return "Hello " + msg + " :: Response time - > " + new Date(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/spring-rmi/rmi-server/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-rmi/rmi-server/src/test/java/com/example/springrmiserver/SpringRmiServerApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.example.springrmiserver; 2 | 3 | 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class SpringRmiServerApplicationTests { 9 | 10 | @Test 11 | public void contextLoads() { 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /spring-webflux-demo/README.md: -------------------------------------------------------------------------------- 1 | # Spring Webflux 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring WebFlux Tutorial](https://howtodoinjava.com/spring-webflux/spring-webflux-tutorial/) 6 | 2. [Spring boot webflux test with @WebFluxTest with WebTestClient](https://howtodoinjava.com/spring-webflux/webfluxtest-with-webtestclient/). 7 | 3. [Spring boot @MockBean](https://howtodoinjava.com/spring-boot2/spring-mockbean-annotation/) -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/WebfluxFunctionalApp.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class WebfluxFunctionalApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(WebfluxFunctionalApp.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.config; 2 | 3 | import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 7 | import org.springframework.core.io.ClassPathResource; 8 | 9 | @Configuration 10 | public class AppConfig 11 | { 12 | @Bean 13 | public static PropertySourcesPlaceholderConfigurer getPropertyPlaceholderConfigurer() 14 | { 15 | PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); 16 | ppc.setLocation(new ClassPathResource("application.properties")); 17 | ppc.setIgnoreUnresolvablePlaceholders(true); 18 | return ppc; 19 | } 20 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/config/MongoConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.config; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration; 7 | import org.springframework.data.mongodb.core.ReactiveMongoTemplate; 8 | import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; 9 | 10 | import com.mongodb.reactivestreams.client.MongoClient; 11 | import com.mongodb.reactivestreams.client.MongoClients; 12 | 13 | @Configuration 14 | @EnableReactiveMongoRepositories(basePackages = "com.howtodoinjava.demo.dao") 15 | public class MongoConfig extends AbstractReactiveMongoConfiguration 16 | { 17 | @Value("${mongodb.port}") 18 | private String port; 19 | 20 | @Value("${mongodb.dbname}") 21 | private String dbName; 22 | 23 | @Override 24 | public MongoClient reactiveMongoClient() { 25 | return MongoClients.create(); 26 | } 27 | 28 | @Override 29 | protected String getDatabaseName() { 30 | return dbName; 31 | } 32 | 33 | @Bean 34 | public ReactiveMongoTemplate reactiveMongoTemplate() { 35 | return new ReactiveMongoTemplate(reactiveMongoClient(), getDatabaseName()); 36 | } 37 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/config/WebFluxConfig.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.reactive.config.EnableWebFlux; 5 | import org.springframework.web.reactive.config.WebFluxConfigurer; 6 | 7 | @Configuration 8 | @EnableWebFlux 9 | public class WebFluxConfig implements WebFluxConfigurer 10 | { 11 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/dao/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.dao; 2 | 3 | import org.springframework.data.mongodb.repository.Query; 4 | import org.springframework.data.mongodb.repository.ReactiveMongoRepository; 5 | 6 | import com.howtodoinjava.demo.model.Employee; 7 | 8 | import reactor.core.publisher.Flux; 9 | 10 | public interface EmployeeRepository extends ReactiveMongoRepository { 11 | @Query("{ 'name': ?0 }") 12 | Flux findByName(final String name); 13 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import org.springframework.context.annotation.Scope; 6 | import org.springframework.context.annotation.ScopedProxyMode; 7 | import org.springframework.data.annotation.Id; 8 | import org.springframework.data.mongodb.core.mapping.Document; 9 | 10 | @Scope(scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 11 | @Document 12 | public class Employee implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | @Id 17 | int id; 18 | String name; 19 | long salary; 20 | 21 | public int getId() { 22 | return id; 23 | } 24 | 25 | public void setId(int id) { 26 | this.id = id; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public long getSalary() { 38 | return salary; 39 | } 40 | 41 | public void setSalary(long salary) { 42 | this.salary = salary; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]"; 48 | } 49 | 50 | @Override 51 | public int hashCode() { 52 | final int prime = 31; 53 | int result = 1; 54 | result = prime * result + id; 55 | result = prime * result + ((name == null) ? 0 : name.hashCode()); 56 | result = prime * result + (int) (salary ^ (salary >>> 32)); 57 | return result; 58 | } 59 | 60 | @Override 61 | public boolean equals(Object obj) { 62 | if (this == obj) 63 | return true; 64 | if (obj == null) 65 | return false; 66 | if (getClass() != obj.getClass()) 67 | return false; 68 | Employee other = (Employee) obj; 69 | if (id != other.id) 70 | return false; 71 | if (name == null) { 72 | if (other.name != null) 73 | return false; 74 | } else if (!name.equals(other.name)) 75 | return false; 76 | if (salary != other.salary) 77 | return false; 78 | return true; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.howtodoinjava.demo.dao.EmployeeRepository; 7 | import com.howtodoinjava.demo.model.Employee; 8 | 9 | import reactor.core.publisher.Flux; 10 | import reactor.core.publisher.Mono; 11 | 12 | @Service 13 | public class EmployeeService implements IEmployeeService { 14 | 15 | @Autowired 16 | EmployeeRepository employeeRepo; 17 | 18 | public void create(Employee e) { 19 | employeeRepo.save(e).subscribe(); 20 | } 21 | 22 | public Mono findById(Integer id) { 23 | return employeeRepo.findById(id); 24 | } 25 | 26 | public Flux findByName(String name) { 27 | return employeeRepo.findByName(name); 28 | } 29 | 30 | public Flux findAll() { 31 | return employeeRepo.findAll(); 32 | } 33 | 34 | public Mono update(Employee e) { 35 | return employeeRepo.save(e); 36 | } 37 | 38 | public Mono delete(Integer id) { 39 | return employeeRepo.deleteById(id); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/java/com/howtodoinjava/demo/service/IEmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.service; 2 | 3 | import com.howtodoinjava.demo.model.Employee; 4 | 5 | import reactor.core.publisher.Flux; 6 | import reactor.core.publisher.Mono; 7 | 8 | public interface IEmployeeService 9 | { 10 | void create(Employee e); 11 | 12 | Mono findById(Integer id); 13 | 14 | Flux findByName(String name); 15 | 16 | Flux findAll(); 17 | 18 | Mono update(Employee e); 19 | 20 | Mono delete(Integer id); 21 | } -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | mongodb.port=27017 2 | mongodb.dbname=testdb 3 | 4 | logging.level.org.springframework=DEBUG -------------------------------------------------------------------------------- /spring-webflux-demo/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /springrestexample/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.howtodoinjava.demo 5 | springrestexample 6 | war 7 | 0.0.1-SNAPSHOT 8 | springrestexample Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 4.12 15 | test 16 | 17 | 18 | 19 | 20 | 21 | org.springframework 22 | spring-webmvc 23 | 5.2.21.RELEASE 24 | 25 | 26 | 27 | org.springframework 28 | spring-web 29 | 6.0.19 30 | 31 | 32 | 33 | org.springframework 34 | spring-core 35 | 5.2.24.RELEASE 36 | 37 | 38 | 39 | 40 | com.fasterxml.jackson.core 41 | jackson-databind 42 | 2.12.7.1 43 | 44 | 45 | 46 | 47 | springrestexample 48 | 49 | 50 | -------------------------------------------------------------------------------- /springrestexample/src/main/java/com/howtodoinjava/demo/controller/EmployeeRESTController.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.ResponseBody; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import com.howtodoinjava.demo.model.EmployeeListVO; 11 | import com.howtodoinjava.demo.model.EmployeeVO; 12 | 13 | @RestController 14 | public class EmployeeRESTController 15 | { 16 | @RequestMapping(value = "/employees") 17 | public @ResponseBody EmployeeListVO getAllEmployees() 18 | { 19 | EmployeeListVO employees = new EmployeeListVO(); 20 | 21 | EmployeeVO empOne = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com"); 22 | EmployeeVO empTwo = new EmployeeVO(2,"Amit","Singhal","asinghal@yahoo.com"); 23 | EmployeeVO empThree = new EmployeeVO(3,"Kirti","Mishra","kmishra@gmail.com"); 24 | 25 | 26 | employees.getEmployees().add(empOne); 27 | employees.getEmployees().add(empTwo); 28 | employees.getEmployees().add(empThree); 29 | 30 | return employees; 31 | } 32 | 33 | @RequestMapping(value = "/employees/{id}") 34 | @ResponseBody 35 | public ResponseEntity getEmployeeById (@PathVariable("id") int id) 36 | { 37 | if (id <= 3) { 38 | EmployeeVO employee = new EmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com"); 39 | return new ResponseEntity(employee, HttpStatus.OK); 40 | } 41 | return new ResponseEntity(HttpStatus.NOT_FOUND); 42 | } 43 | } -------------------------------------------------------------------------------- /springrestexample/src/main/java/com/howtodoinjava/demo/model/EmployeeListVO.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import javax.xml.bind.annotation.XmlRootElement; 7 | 8 | @XmlRootElement (name="employees") 9 | public class EmployeeListVO 10 | { 11 | private List employees = new ArrayList(); 12 | 13 | public List getEmployees() { 14 | return employees; 15 | } 16 | 17 | public void setEmployees(List employees) { 18 | this.employees = employees; 19 | } 20 | } -------------------------------------------------------------------------------- /springrestexample/src/main/java/com/howtodoinjava/demo/model/EmployeeVO.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.demo.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.xml.bind.annotation.XmlAccessType; 6 | import javax.xml.bind.annotation.XmlAccessorType; 7 | import javax.xml.bind.annotation.XmlAttribute; 8 | import javax.xml.bind.annotation.XmlElement; 9 | import javax.xml.bind.annotation.XmlRootElement; 10 | 11 | @XmlRootElement (name = "employee") 12 | @XmlAccessorType(XmlAccessType.NONE) 13 | public class EmployeeVO implements Serializable 14 | { 15 | private static final long serialVersionUID = 1L; 16 | 17 | @XmlAttribute 18 | private Integer id; 19 | 20 | @XmlElement 21 | private String firstName; 22 | 23 | @XmlElement 24 | private String lastName; 25 | 26 | @XmlElement 27 | private String email; 28 | 29 | public EmployeeVO(Integer id, String firstName, String lastName, String email) { 30 | super(); 31 | this.id = id; 32 | this.firstName = firstName; 33 | this.lastName = lastName; 34 | this.email = email; 35 | } 36 | 37 | public EmployeeVO(){ 38 | 39 | } 40 | 41 | public Integer getId() { 42 | return id; 43 | } 44 | 45 | public void setId(Integer id) { 46 | this.id = id; 47 | } 48 | 49 | public String getFirstName() { 50 | return firstName; 51 | } 52 | 53 | public void setFirstName(String firstName) { 54 | this.firstName = firstName; 55 | } 56 | 57 | public String getLastName() { 58 | return lastName; 59 | } 60 | 61 | public void setLastName(String lastName) { 62 | this.lastName = lastName; 63 | } 64 | 65 | public String getEmail() { 66 | return email; 67 | } 68 | 69 | public void setEmail(String email) { 70 | this.email = email; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "EmployeeVO [id=" + id + ", firstName=" + firstName 76 | + ", lastName=" + lastName + ", email=" + email + "]"; 77 | } 78 | } -------------------------------------------------------------------------------- /springrestexample/src/main/webapp/WEB-INF/spring-servlet.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /springrestexample/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | 9 | spring 10 | 11 | org.springframework.web.servlet.DispatcherServlet 12 | 13 | 1 14 | 15 | 16 | 17 | spring 18 | / 19 | 20 | 21 | -------------------------------------------------------------------------------- /springrestexample/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /ssl-https/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | ssl-https 12 | ssl-https 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-test 26 | test 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /ssl-https/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ssl-https/src/main/java/com/springexamples/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.web; 2 | 3 | import org.springframework.web.bind.annotation.RequestMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class HelloController { 8 | 9 | @RequestMapping("/") 10 | public String index() { 11 | return "Greetings from Spring Examples !!"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /ssl-https/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8443 2 | security.require-ssl=true 3 | server.ssl.key-store = classpath:keystore.jks 4 | server.ssl.key-store-password = password 5 | server.ssl.key-password = password 6 | server.ssl.key-store-type = JKS -------------------------------------------------------------------------------- /ssl-https/src/main/resources/keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/ssl-https/src/main/resources/keystore.jks -------------------------------------------------------------------------------- /ssl-https/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /swagger-ui/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | swagger-ui 12 | swagger-ui 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | io.springfox 24 | springfox-swagger2 25 | 2.8.0 26 | 27 | 28 | io.springfox 29 | springfox-swagger-ui 30 | 2.8.0 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /swagger-ui/src/main/java/com/springexamples/demo/Application.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application 8 | { 9 | public static void main( String[] args ) 10 | { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /swagger-ui/src/main/java/com/springexamples/demo/config/Swagger2UiConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 7 | 8 | import com.google.common.base.Predicates; 9 | 10 | import springfox.documentation.builders.RequestHandlerSelectors; 11 | import springfox.documentation.spi.DocumentationType; 12 | import springfox.documentation.spring.web.plugins.Docket; 13 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 | 15 | @Configuration 16 | @EnableSwagger2 17 | public class Swagger2UiConfiguration implements WebMvcConfigurer 18 | { 19 | @Bean 20 | public Docket api() { 21 | // @formatter:off 22 | //Register the controllers to swagger 23 | //Also it is configuring the Swagger Docket 24 | return new Docket(DocumentationType.SWAGGER_2).select() 25 | // .apis(RequestHandlerSelectors.any()) 26 | .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot"))) 27 | // .paths(PathSelectors.any()) 28 | // .paths(PathSelectors.ant("/swagger2-demo")) 29 | .build(); 30 | // @formatter:on 31 | } 32 | 33 | @Override 34 | public void addResourceHandlers(ResourceHandlerRegistry registry) 35 | { 36 | //enabling swagger-ui part for visual documentation 37 | registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); 38 | registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /swagger-ui/src/main/java/com/springexamples/demo/web/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.web; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import io.swagger.annotations.ApiOperation; 7 | 8 | @RestController 9 | public class HelloController { 10 | 11 | @ApiOperation(value = "Get hello world message ", response = String.class) 12 | @GetMapping("/") 13 | public String helloWorld() { 14 | return "Greetings from Spring Examples !!"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /swagger-ui/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /template-freemarker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | template-freemarker 12 | template-freemarker 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-starter-web 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-freemarker 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-test 32 | test 33 | 34 | 35 | 36 | 37 | 38 | org.webjars 39 | bootstrap 40 | 5.0.0 41 | 42 | 43 | org.webjars 44 | bootstrap-datepicker 45 | 1.0.1 46 | 47 | 48 | org.webjars 49 | jquery 50 | 1.9.1 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /template-freemarker/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App 8 | { 9 | public static void main(String[] args) 10 | { 11 | SpringApplication.run(App.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /template-freemarker/src/main/java/com/springexamples/demo/rest/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.rest; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Controller 8 | public class HomeController { 9 | 10 | @RequestMapping("/") 11 | String home(ModelMap modal) { 12 | modal.addAttribute("title", "Hi buddy"); 13 | modal.addAttribute("message", "Welcome to SprinExamples.com"); 14 | return "home"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /template-freemarker/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.freemarker.templateLoaderPath=classpath:/ftl/ -------------------------------------------------------------------------------- /template-freemarker/src/main/resources/ftl/error.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |
10 |

Oops !! Application Crashed !!

11 |

HTTP Status - ${status}

12 |

HTTP Message - ${error}

13 |
14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /template-freemarker/src/main/resources/ftl/home.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

${title}

16 |

${message}

17 |
18 |
19 | 20 | -------------------------------------------------------------------------------- /template-freemarker/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /template-jsp/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 7 | 8 | @SpringBootApplication 9 | public class App extends SpringBootServletInitializer 10 | { 11 | 12 | @Override 13 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 14 | return application.sources(App.class); 15 | } 16 | 17 | public static void main(String[] args) 18 | { 19 | SpringApplication.run(App.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /template-jsp/src/main/java/com/springexamples/demo/rest/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.rest; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Controller 8 | public class HomeController { 9 | 10 | @RequestMapping("/") 11 | String home(ModelMap modal) { 12 | modal.addAttribute("title", "Hi buddy"); 13 | modal.addAttribute("message", "Welcome to SprinExamples.com"); 14 | return "home"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /template-jsp/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.mvc.view.prefix=/WEB-INF/jsp/ 2 | spring.mvc.view.suffix=.jsp 3 | server.error.whitelabel.enabled=false 4 | server.error.include-stacktrace=always -------------------------------------------------------------------------------- /template-jsp/src/main/webapp/WEB-INF/jsp/error.jsp: -------------------------------------------------------------------------------- 1 | <%@ page language="java" 2 | contentType="text/html; charset=ISO-8859-1" 3 | pageEncoding="ISO-8859-1"%> 4 | 5 | 6 | 13 | 14 | 15 |

My Error Page

16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 42 | 43 |
Date${timestamp}
Error${error}
Status${status}
Message${message}
Exception${exception}
Trace 40 |
${trace}
41 |
44 | 45 | -------------------------------------------------------------------------------- /template-jsp/src/main/webapp/WEB-INF/jsp/home.jsp: -------------------------------------------------------------------------------- 1 | 2 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

${title}

16 |

${message}

17 |
18 |
19 | 20 | -------------------------------------------------------------------------------- /template-jsp/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /template-mustache/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.springexamples.demo 7 | SpringExamples 8 | 0.0.1-SNAPSHOT 9 | 10 | template-mustache 11 | template-mustache 12 | http://maven.apache.org 13 | 14 | UTF-8 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-web 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-mustache 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | 35 | 36 | org.webjars 37 | bootstrap 38 | 3.3.6 39 | 40 | 41 | org.webjars 42 | bootstrap-datepicker 43 | 1.0.1 44 | 45 | 46 | org.webjars 47 | jquery 48 | 1.9.1 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /template-mustache/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.core.env.Environment; 7 | 8 | import com.samskivert.mustache.Mustache; 9 | 10 | @SpringBootApplication 11 | public class App { 12 | public static void main(String[] args) { 13 | SpringApplication.run(App.class, args); 14 | } 15 | 16 | /*@Bean 17 | public Mustache.Compiler mustacheCompiler(Mustache.TemplateLoader templateLoader, 18 | Environment environment) 19 | { 20 | 21 | MustacheEnvironmentCollector collector = new MustacheEnvironmentCollector(); 22 | collector.setEnvironment(environment); 23 | 24 | return Mustache.compiler() 25 | .defaultValue("Some Default Value") 26 | .withLoader(templateLoader) 27 | .withCollector(collector); 28 | }*/ 29 | } 30 | -------------------------------------------------------------------------------- /template-mustache/src/main/java/com/springexamples/demo/rest/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.rest; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Controller 8 | public class HomeController { 9 | 10 | @RequestMapping("/") 11 | String home(ModelMap modal) { 12 | modal.addAttribute("title", "Hi buddy"); 13 | modal.addAttribute("message", "Welcome to SprinExamples.com"); 14 | return "home"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /template-mustache/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/template-mustache/src/main/resources/application.properties -------------------------------------------------------------------------------- /template-mustache/src/main/resources/templates/error.mustache: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |
10 |

Oops !! Application Crashed !!

11 |

{{status}}

12 |

{{error}}

13 |
14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /template-mustache/src/main/resources/templates/home.mustache: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |
13 |

{{title}}

14 |

{{message}}

15 |
16 |
17 | 18 | -------------------------------------------------------------------------------- /template-mustache/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | 4 | public class AppTest { 5 | 6 | } 7 | -------------------------------------------------------------------------------- /template-thymeleaf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.springexamples.demo 8 | SpringExamples 9 | 0.0.1-SNAPSHOT 10 | 11 | template-thymeleaf 12 | template-thymeleaf 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-web 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-thymeleaf 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-test 31 | test 32 | 33 | 34 | 35 | 36 | 37 | org.webjars 38 | bootstrap 39 | 3.3.6 40 | 41 | 42 | org.webjars 43 | bootstrap-datepicker 44 | 1.0.1 45 | 46 | 47 | org.webjars 48 | jquery 49 | 1.9.1 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /template-thymeleaf/src/main/java/com/springexamples/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class App 8 | { 9 | public static void main(String[] args) 10 | { 11 | SpringApplication.run(App.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /template-thymeleaf/src/main/java/com/springexamples/demo/rest/HomeController.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo.rest; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.ui.ModelMap; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @Controller 8 | public class HomeController { 9 | 10 | @RequestMapping("/") 11 | String home(ModelMap modal) { 12 | modal.addAttribute("title", "Hi buddy"); 13 | modal.addAttribute("message", "Welcome to SprinExamples.com"); 14 | return "home"; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /template-thymeleaf/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lokeshgupta1981/SpringExamples/240dbab25053208ed298b9fbeeaf99ae7fcafccb/template-thymeleaf/src/main/resources/application.properties -------------------------------------------------------------------------------- /template-thymeleaf/src/main/resources/templates/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 |
10 |

Oops !! Application Crashed !!

11 |

12 |

13 |
14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /template-thymeleaf/src/main/resources/templates/home.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 |
15 |

16 |

17 |
18 |
19 | 20 | -------------------------------------------------------------------------------- /template-thymeleaf/src/test/java/com/springexamples/demo/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.springexamples.demo; 2 | 3 | import org.junit.jupiter.api.Test; 4 | 5 | public class AppTest 6 | { 7 | @Test 8 | void contextLoads() { 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /unit-testing/README.md: -------------------------------------------------------------------------------- 1 | # Unit testing examples 2 | 3 | Tutorials related to this project: 4 | 5 | 1. [Spring rest controller unit test example](https://howtodoinjava.com/spring-boot2/rest-controller-unit-test-example/). 6 | 2. [Junit 5 with Spring boot 2](https://howtodoinjava.com/spring-boot2/junit5-with-spring-boot2/) 7 | 3. [Spring boot integration test example](https://howtodoinjava.com/spring-boot2/spring-integration-testing/) 8 | 9 | ## Import the project into Eclipse IDE 10 | 11 | If you want to import these project into the local eclipse setup - 12 | 13 | 1. Download the project as zip file into your computer 14 | 2. Unzip the project at your desired location 15 | 3. Import the project into Eclipse as existing maven project 16 | 17 | ``` 18 | File > Import... > Maven > Existing Maven Projects 19 | ``` -------------------------------------------------------------------------------- /unit-testing/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.springexamples.demo 9 | SpringExamples 10 | 0.0.1-SNAPSHOT 11 | 12 | unit-testing 13 | unit-testing 14 | http://maven.apache.org 15 | jar 16 | 17 | UTF-8 18 | UTF-8 19 | 17 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-data-jpa 31 | 32 | 33 | 34 | com.h2database 35 | h2 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | 45 | org.mockito 46 | mockito-junit-jupiter 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-maven-plugin 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/SpringBootDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootDemoApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/controller/EmployeeController.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.controller; 2 | 3 | import java.net.URI; 4 | import java.util.ArrayList; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestBody; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.servlet.support.ServletUriComponentsBuilder; 13 | 14 | import com.howtodoinjava.rest.dao.EmployeeRepository; 15 | import com.howtodoinjava.rest.model.Employee; 16 | import com.howtodoinjava.rest.model.Employees; 17 | 18 | @RestController 19 | public class EmployeeController { 20 | 21 | @Autowired 22 | private EmployeeRepository employeeRepository; 23 | 24 | @GetMapping(path = "/employees", produces = "application/json") 25 | public Employees getEmployees() { 26 | Employees response = new Employees(); 27 | ArrayList list = new ArrayList<>(); 28 | employeeRepository.findAll().forEach(e -> list.add(e)); 29 | response.setEmployeeList(list); 30 | return response; 31 | } 32 | 33 | @PostMapping(path = "/employees", consumes = "application/json", produces = "application/json") 34 | public ResponseEntity addEmployee(@RequestBody Employee employee) { 35 | 36 | //add resource 37 | employee = employeeRepository.save(employee); 38 | 39 | //Create resource location 40 | URI location = ServletUriComponentsBuilder.fromCurrentRequest() 41 | .path("/{id}") 42 | .buildAndExpand(employee.getId()) 43 | .toUri(); 44 | 45 | //Send location in response 46 | return ResponseEntity.created(location).build(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/dao/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.dao; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.howtodoinjava.rest.model.Employee; 7 | 8 | @Repository 9 | public interface EmployeeRepository extends CrudRepository 10 | { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/exception/CustomExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.exception; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.ServletRequestBindingException; 9 | import org.springframework.web.bind.annotation.ControllerAdvice; 10 | import org.springframework.web.bind.annotation.ExceptionHandler; 11 | import org.springframework.web.context.request.WebRequest; 12 | 13 | @SuppressWarnings({"unchecked","rawtypes"}) 14 | @ControllerAdvice 15 | public class CustomExceptionHandler 16 | { 17 | @ExceptionHandler(ServletRequestBindingException.class) 18 | public final ResponseEntity handleHeaderException(Exception ex, WebRequest request) 19 | { 20 | List details = new ArrayList<>(); 21 | details.add(ex.getLocalizedMessage()); 22 | ErrorResponse error = new ErrorResponse("Bad Request", details); 23 | return new ResponseEntity(error, HttpStatus.BAD_REQUEST); 24 | } 25 | 26 | @ExceptionHandler(Exception.class) 27 | public final ResponseEntity handleAllExceptions(Exception ex, WebRequest request) 28 | { 29 | List details = new ArrayList<>(); 30 | details.add(ex.getLocalizedMessage()); 31 | ErrorResponse error = new ErrorResponse("Server Error", details); 32 | return new ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/exception/ErrorResponse.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.exception; 2 | 3 | import java.util.List; 4 | 5 | public class ErrorResponse 6 | { 7 | public ErrorResponse(String message, List details) { 8 | super(); 9 | this.message = message; 10 | this.details = details; 11 | } 12 | 13 | //General error message about nature of error 14 | private String message; 15 | 16 | //Specific errors in API request processing 17 | private List details; 18 | 19 | public String getMessage() { 20 | return message; 21 | } 22 | 23 | public void setMessage(String message) { 24 | this.message = message; 25 | } 26 | 27 | public List getDetails() { 28 | return details; 29 | } 30 | 31 | public void setDetails(List details) { 32 | this.details = details; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.persistence.Column; 6 | import jakarta.persistence.Entity; 7 | import jakarta.persistence.GeneratedValue; 8 | import jakarta.persistence.GenerationType; 9 | import jakarta.persistence.Id; 10 | import jakarta.persistence.Table; 11 | 12 | @Entity 13 | @Table(name = "tbl_employees") 14 | public class Employee implements Serializable { 15 | private static final long serialVersionUID = 1L; 16 | 17 | public Employee() { 18 | } 19 | 20 | public Employee(String firstName, String lastName, String email) { 21 | super(); 22 | this.firstName = firstName; 23 | this.lastName = lastName; 24 | this.email = email; 25 | } 26 | 27 | @Id 28 | @GeneratedValue(strategy = GenerationType.AUTO) 29 | @Column(name = "id") 30 | private Integer id; 31 | 32 | @Column(name = "firstName") 33 | private String firstName; 34 | 35 | @Column(name = "lastName") 36 | private String lastName; 37 | 38 | @Column(name = "email") 39 | private String email; 40 | 41 | public Integer getId() { 42 | return id; 43 | } 44 | 45 | public void setId(Integer id) { 46 | this.id = id; 47 | } 48 | 49 | public String getFirstName() { 50 | return firstName; 51 | } 52 | 53 | public void setFirstName(String firstName) { 54 | this.firstName = firstName; 55 | } 56 | 57 | public String getLastName() { 58 | return lastName; 59 | } 60 | 61 | public void setLastName(String lastName) { 62 | this.lastName = lastName; 63 | } 64 | 65 | public String getEmail() { 66 | return email; 67 | } 68 | 69 | public void setEmail(String email) { 70 | this.email = email; 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]"; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /unit-testing/src/main/java/com/howtodoinjava/rest/model/Employees.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Employees 7 | { 8 | private List employeeList; 9 | 10 | public List getEmployeeList() { 11 | if(employeeList == null) { 12 | employeeList = new ArrayList<>(); 13 | } 14 | return employeeList; 15 | } 16 | 17 | public void setEmployeeList(List employeeList) { 18 | this.employeeList = employeeList; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /unit-testing/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url = jdbc:h2:mem:testdb 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect 6 | spring.h2.console.enabled=true -------------------------------------------------------------------------------- /unit-testing/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO TBL_EMPLOYEES (first_name, last_name, email) VALUES 2 | ('Lokesh', 'Gupta', 'abc@gmail.com'), 3 | ('Deja', 'Vu', 'xyz@email.com'), 4 | ('Caption', 'America', 'cap@marvel.com'); -------------------------------------------------------------------------------- /unit-testing/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE IF EXISTS TBL_EMPLOYEES; 2 | 3 | CREATE TABLE TBL_EMPLOYEES ( 4 | id INT AUTO_INCREMENT PRIMARY KEY, 5 | first_name VARCHAR(250) NOT NULL, 6 | last_name VARCHAR(250) NOT NULL, 7 | email VARCHAR(250) DEFAULT NULL 8 | ); -------------------------------------------------------------------------------- /unit-testing/src/test/java/com/howtodoinjava/rest/EmployeeControllerIntegrationTests.java: -------------------------------------------------------------------------------- 1 | package com.howtodoinjava.rest; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertTrue; 5 | 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 10 | import org.springframework.boot.test.web.client.TestRestTemplate; 11 | import org.springframework.boot.test.web.server.LocalServerPort; 12 | import org.springframework.http.ResponseEntity; 13 | import org.springframework.test.context.jdbc.Sql; 14 | 15 | import com.howtodoinjava.rest.model.Employee; 16 | import com.howtodoinjava.rest.model.Employees; 17 | 18 | @SpringBootTest(classes = SpringBootDemoApplication.class, 19 | webEnvironment = WebEnvironment.RANDOM_PORT) 20 | public class EmployeeControllerIntegrationTests 21 | { 22 | @LocalServerPort 23 | private int port; 24 | 25 | @Autowired 26 | private TestRestTemplate restTemplate; 27 | 28 | @Sql({ "classpath:schema.sql", "classpath:data.sql" }) 29 | @Test 30 | public void testAllEmployees() 31 | { 32 | assertTrue( 33 | this.restTemplate 34 | .getForObject("http://localhost:" + port + "/employees", Employees.class) 35 | .getEmployeeList().size() == 3); 36 | } 37 | 38 | @Test 39 | public void testAddEmployee() { 40 | Employee employee = new Employee("Lokesh", "Gupta", "howtodoinjava@gmail.com"); 41 | ResponseEntity responseEntity = this.restTemplate 42 | .postForEntity("http://localhost:" + port + "/employees", employee, String.class); 43 | assertEquals(201, responseEntity.getStatusCode().value()); 44 | } 45 | } 46 | --------------------------------------------------------------------------------