├── README.md ├── spring-rest-retry-recover ├── spring-rest-server │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── application.properties │ │ │ └── java │ │ │ └── com │ │ │ └── roytuts │ │ │ └── springrestserver │ │ │ ├── ServerApp.java │ │ │ ├── service │ │ │ └── EmployeeService.java │ │ │ ├── repository │ │ │ └── EmployeeRepository.java │ │ │ ├── model │ │ │ └── Employee.java │ │ │ └── controller │ │ │ └── EmployeeRestController.java │ └── pom.xml ├── readme.rst └── spring-rest-retry │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── springrestretry │ │ ├── ClientApp.java │ │ ├── controller │ │ └── EmployeeRestController.java │ │ ├── custom │ │ └── CustomRetryPolicy.java │ │ ├── model │ │ └── Employee.java │ │ ├── config │ │ └── AppConfig.java │ │ └── service │ │ └── EmployeeService.java │ └── pom.xml ├── spring-hateoas ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ └── data.sql │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── hateoas │ │ ├── SpringHateoas.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ ├── config │ │ └── SpringConfig.java │ │ ├── vo │ │ └── EmployeeVo.java │ │ ├── entity │ │ └── Employee.java │ │ ├── service │ │ └── EmployeeService.java │ │ └── rest │ │ └── controller │ │ └── EmployeeRestController.java ├── build.gradle └── pom.xml ├── spring-rest-junit-mvc ├── readme.rst ├── src │ └── main │ │ ├── webapp │ │ └── index.jsp │ │ └── java │ │ └── roytuts │ │ ├── config │ │ ├── WebMvcConfig.java │ │ └── MvcWebApplicationInitializer.java │ │ ├── model │ │ └── Product.java │ │ ├── sevice │ │ └── ProductService.java │ │ └── rest │ │ └── SpringRestController.java └── pom.xml ├── spring-boot-angular-crud-rest-api ├── readme.rst ├── spring-boot-rest.sql ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── boot │ │ │ └── angular │ │ │ └── crud │ │ │ └── rest │ │ │ └── api │ │ │ ├── repository │ │ │ └── WebsiteRepository.java │ │ │ ├── SpringAngularCrudRestApiApp.java │ │ │ ├── vo │ │ │ └── WebsiteVo.java │ │ │ ├── entity │ │ │ └── Website.java │ │ │ ├── converter │ │ │ └── EntityVoConverter.java │ │ │ ├── rest │ │ │ └── controller │ │ │ │ └── WebsiteRestController.java │ │ │ └── service │ │ │ └── WebsiteService.java │ │ └── resources │ │ └── application.properties └── pom.xml ├── spring-boot-swagger ├── readme.rst ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── boot │ │ └── swagger │ │ ├── SpringSwaggerApp.java │ │ ├── config │ │ └── SwaggerConfig.java │ │ └── rest │ │ └── controller │ │ └── GreetingController.java └── pom.xml ├── spring-concurrency-completablefuture ├── spring-concurrency-concurrent-calls │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ └── application.properties │ │ │ └── java │ │ │ └── com │ │ │ └── roytuts │ │ │ └── java │ │ │ └── spring │ │ │ └── concurrency │ │ │ └── concurrent │ │ │ └── calls │ │ │ ├── BeanConfig.java │ │ │ ├── CustomAsyncExceptionHandler.java │ │ │ ├── AsyncService.java │ │ │ ├── AsyncConfig.java │ │ │ └── ConcurrentRunnerApp.java │ ├── pom_3.5.4.xml │ └── pom.xml ├── readme.rst └── spring-concurrency-service-api │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── java │ │ └── spring │ │ └── concurrency │ │ └── service │ │ └── api │ │ └── ServiceApiApp.java │ ├── pom_3.5.4.xml │ └── pom.xml ├── spring-openapi-documentation ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── openapi │ │ └── documentation │ │ ├── SpringOpenApiDocumentationApp.java │ │ └── rest │ │ └── controller │ │ └── RestApi.java ├── build.gradle ├── api-docs.yaml ├── pom.xml_3.5.4 ├── pom.xml_3_2_5 └── pom.xml ├── spring-rest-junit-complete ├── src │ ├── main │ │ ├── webapp │ │ │ └── index.jsp │ │ └── java │ │ │ └── roytuts │ │ │ ├── config │ │ │ ├── WebMvcConfig.java │ │ │ └── MvcWebApplicationInitializer.java │ │ │ ├── model │ │ │ └── Product.java │ │ │ ├── sevice │ │ │ └── ProductService.java │ │ │ └── rest │ │ │ └── SpringRestController.java │ └── test │ │ └── java │ │ └── jeejava │ │ └── rest │ │ └── SpringRestControllerTest.java └── readme.rst ├── spring-rest-optional-path-variable ├── readme.rst ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── optional │ │ └── path │ │ └── variable │ │ ├── App.java │ │ └── controller │ │ └── SpringRestController.java ├── build.gradle └── pom.xml ├── spring-online-visitor-tracker ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── online │ │ └── visitor │ │ └── tracker │ │ ├── repository │ │ └── VisitorRepository.java │ │ ├── service │ │ └── VisitorService.java │ │ ├── OnlineVisitorTracker.java │ │ ├── config │ │ ├── AppConfig.java │ │ └── SecurityConfig.java │ │ ├── rest │ │ └── controller │ │ │ └── GreetingController.java │ │ ├── logger │ │ └── VisitorLogger.java │ │ └── entity │ │ └── Visitor.java └── pom.xml ├── spring-rest-file-upload ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── file │ │ └── upload │ │ ├── App.java │ │ └── controller │ │ └── FileUploadRestController.java ├── build.gradle ├── pom.xml └── pom.xml_3 ├── spring-rest-h2-download-file ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ ├── application.yml │ │ ├── schema.sql │ │ ├── data.sql │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── download │ │ └── file │ │ ├── repository │ │ └── EmployeeRepository.java │ │ ├── vo │ │ └── EmployeeVo.java │ │ ├── SpringRestH2FileDownloadApp.java │ │ ├── service │ │ └── EmployeeService.java │ │ ├── entity │ │ └── Employee.java │ │ └── rest │ │ └── controller │ │ └── EmployeeRestController.java └── pom.xml ├── spring-rest-large-file-download ├── readme.rst ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── large │ │ └── file │ │ └── download │ │ ├── config │ │ └── AppConfig.java │ │ ├── SpringRestLargeFileDownloadApp.java │ │ ├── dto │ │ └── Input.java │ │ └── rest │ │ └── controller │ │ └── FileDownloadRestController.java └── pom.xml ├── spring-web-exception-handling ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ ├── data.sql │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── web │ │ └── exception │ │ └── handling │ │ ├── constants │ │ └── AppConstants.java │ │ ├── exception │ │ ├── DbException.java │ │ └── AppException.java │ │ ├── repository │ │ └── EmployeeRepository.java │ │ ├── wrapper │ │ └── ExceptionWrapper.java │ │ ├── SpringWebExceptionHandlingApp.java │ │ ├── entity │ │ └── Employee.java │ │ ├── rest │ │ └── controller │ │ │ └── EmployeeRestController.java │ │ └── handler │ │ └── AppExceptionHandler.java ├── build.gradle └── pom.xml ├── springboot-rest-api-multivaluemap ├── readme.rst ├── springboot-rest-api-multivaluemap │ ├── src │ │ └── main │ │ │ └── java │ │ │ └── com │ │ │ └── roytuts │ │ │ └── springboot │ │ │ └── rest │ │ │ └── api │ │ │ └── multivaluedmap │ │ │ ├── App.java │ │ │ └── rest │ │ │ └── controller │ │ │ └── AppRestController.java │ └── pom.xml └── springboot-rest-api-multivaluemap-client │ ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── springboot │ │ └── rest │ │ └── api │ │ └── multivaluedmap │ │ └── client │ │ ├── config │ │ └── RestClientConfig.java │ │ └── App.java │ └── pom.xml ├── spring-rest-https-certificate ├── readme.rst ├── spring-rest-https-client │ ├── src │ │ └── main │ │ │ ├── resources │ │ │ ├── certificate.jks │ │ │ └── javaclient.jks │ │ │ └── java │ │ │ └── com │ │ │ └── roytuts │ │ │ └── spring │ │ │ └── rest │ │ │ └── https │ │ │ ├── enums │ │ │ └── TemperatureUnit.java │ │ │ ├── dto │ │ │ └── Temperature.java │ │ │ ├── SpringRestHttpsClientApp.java │ │ │ ├── client │ │ │ └── TempConverterRestClient.java │ │ │ └── config │ │ │ └── RestConfig.java │ └── build.gradle └── spring-rest-https-server │ ├── src │ └── main │ │ ├── resources │ │ ├── certificate.jks │ │ ├── truststore.jks │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── https │ │ ├── enums │ │ └── TemperatureUnit.java │ │ ├── SpringRestHttpsServerApp.java │ │ ├── dto │ │ └── Temperature.java │ │ └── rest │ │ └── controller │ │ └── TempConverterRestController.java │ └── build.gradle ├── spring-rest-multiple-files-upload ├── readme.rst ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── multiple │ │ └── files │ │ └── upload │ │ ├── SpringRestMultipleFilesUploadApp.java │ │ └── rest │ │ └── controller │ │ └── MultipleFileUploadRestController.java ├── build.gradle └── pom.xml ├── spring-resttemplate-proxy-timeout ├── readme.rst ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── resttemplate │ │ └── proxy │ │ └── timeout │ │ ├── SpringRestTemplateProxyTimeoutApp.java │ │ ├── RestApiController.java │ │ └── Config.java └── pom.xml ├── spring-rest-multiple-files-upload-client ├── readme.rst ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── roytuts │ └── spring │ └── rest │ └── multiple │ └── files │ └── upload │ └── client │ └── FileUploadClient.java ├── spring-rest-return-different-data-format ├── readme.rst ├── settings.gradle ├── src │ └── main │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── different │ │ └── data │ │ └── format │ │ ├── SpringRestDataFormatsApp.java │ │ ├── model │ │ └── Employee.java │ │ └── rest │ │ └── controller │ │ └── SpringRestController.java ├── build.gradle └── pom.xml └── spring-rest-file-download ├── readme.rst ├── src └── main │ ├── resources │ └── application.properties │ └── java │ └── com │ └── roytuts │ ├── repository │ └── EmployeeRepository.java │ ├── service │ └── EmployeeService.java │ ├── main │ └── Application.java │ ├── entity │ └── Employee.java │ └── controller │ └── FileDownloadRestController.java ├── spring-boot-3 ├── src │ └── main │ │ ├── resources │ │ └── application.properties │ │ └── java │ │ └── com │ │ └── roytuts │ │ └── spring │ │ └── rest │ │ └── file │ │ └── download │ │ ├── repository │ │ └── EmployeeRepository.java │ │ ├── App.java │ │ ├── service │ │ └── EmployeeService.java │ │ ├── entity │ │ └── Employee.java │ │ └── controller │ │ └── FileDownloadRestController.java └── pom.xml ├── build.gradle ├── example.json ├── pom.xml └── employee.sql /README.md: -------------------------------------------------------------------------------- 1 | Spring REST API related programs 2 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9000 2 | -------------------------------------------------------------------------------- /spring-hateoas/readme.rst: -------------------------------------------------------------------------------- 1 | You can read tutorial https://roytuts.com/hateoas-driven-rest-api-using-spring-boot/ -------------------------------------------------------------------------------- /spring-rest-junit-mvc/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-restful-webservice-crud-example/ 2 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-rest-api-retryable-and-recover/ 2 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/angular-spring-boot-rest-api-crud-example/ -------------------------------------------------------------------------------- /spring-boot-swagger/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/spring-boot-rest-api-documentation-with-swagger-2/ -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8000 -------------------------------------------------------------------------------- /spring-openapi-documentation/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/how-to-document-rest-apis-with-openapi/ 2 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-rest-optional-path-variable/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-rest-optional-pathvariable/ 2 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/readme.rst: -------------------------------------------------------------------------------- 1 | You can go through the tutorial https://roytuts.com/how-to-log-user-activity-using-spring-boot/ -------------------------------------------------------------------------------- /spring-rest-file-upload/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/file-upload-example-using-spring-rest-controller/ 2 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/download-file-using-angular-and-spring-boot/ 2 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/download-large-file-using-spring-rest-api/ 2 | -------------------------------------------------------------------------------- /spring-web-exception-handling/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/exception-handling-in-spring-web-application/ 2 | -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-boot-rest-api-multivaluemap-example/ 2 | -------------------------------------------------------------------------------- /spring-openapi-documentation/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | springdoc.api-docs.path=/api-docs 2 | springdoc.swagger-ui.path=/swagger.html -------------------------------------------------------------------------------- /spring-rest-https-certificate/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/rest-over-https-with-client-certificate-authentication/ 2 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/how-to-upload-multiple-files-using-spring-rest-api/ 2 | -------------------------------------------------------------------------------- /spring-resttemplate-proxy-timeout/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/spring-resttemplate-with-auth-token-proxy-and-timeout/ -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload-client/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/multipart-file-upload-client-using-spring-rest-template/ 2 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/readme.rst: -------------------------------------------------------------------------------- 1 | You can read the tutorial https://roytuts.com/how-to-return-different-data-formats-json-xml-from-spring-rest-api/ -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/how-to-call-spring-rest-apis-concurrently-using-java-completablefuture/ 2 | -------------------------------------------------------------------------------- /spring-rest-file-upload/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #multipart max size 2 | spring.http.multipart.max-file-size=10MB 3 | spring.http.multipart.max-request-size=10MB 4 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #multipart max size 2 | spring.http.multipart.max-file-size=10MB 3 | spring.http.multipart.max-request-size=10MB -------------------------------------------------------------------------------- /spring-rest-file-download/readme.rst: -------------------------------------------------------------------------------- 1 | Please follow the tutorial https://roytuts.com/file-download-example-using-spring-rest-controller/ 2 | 3 | For spring boot 3 please check the folder spring-boot-3 4 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | show-sql: true 4 | hibernate: 5 | ddl-auto: none 6 | h2: 7 | console: 8 | enabled: true -------------------------------------------------------------------------------- /spring-rest-junit-complete/readme.rst: -------------------------------------------------------------------------------- 1 | You can read the following tutorials 2 | 3 | https://roytuts.com/junit-testing-spring-rest-services/ 4 | 5 | https://roytuts.com/spring-restful-webservice-crud-example/ 6 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | show-sql: true 4 | h2: 5 | console: 6 | enabled: true 7 | jackson: 8 | default-property-inclusion: NON_NULL -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/resources/certificate.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-rest/HEAD/spring-rest-https-certificate/spring-rest-https-client/src/main/resources/certificate.jks -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/resources/javaclient.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-rest/HEAD/spring-rest-https-certificate/spring-rest-https-client/src/main/resources/javaclient.jks -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/resources/certificate.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-rest/HEAD/spring-rest-https-certificate/spring-rest-https-server/src/main/resources/certificate.jks -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/resources/truststore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roytuts/spring-rest/HEAD/spring-rest-https-certificate/spring-rest-https-server/src/main/resources/truststore.jks -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/java/com/roytuts/spring/rest/https/enums/TemperatureUnit.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.enums; 2 | 3 | public enum TemperatureUnit { 4 | 5 | CELSIUS, FAHRENHEIT; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/java/com/roytuts/spring/rest/https/enums/TemperatureUnit.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.enums; 2 | 3 | public enum TemperatureUnit { 4 | 5 | CELSIUS, FAHRENHEIT; 6 | 7 | } 8 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 2 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 3 | spring.datasource.username=root 4 | spring.datasource.password=root 5 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | drop table if exists employee; 2 | 3 | create table employee ( 4 | id INTEGER generated by default as identity (start with 1) primary key, 5 | name varchar(25) not null, 6 | email_address varchar(50) not null 7 | ); -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/java/com/roytuts/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=8443 2 | server.ssl.key-store=classpath:certificate.jks 3 | server.ssl.key-store-password=changeit 4 | server.ssl.key-alias=selfsigned 5 | server.ssl.trust-store=classpath:truststore.jks 6 | server.ssl.trust-store-password=changeit 7 | server.ssl.client-auth=need -------------------------------------------------------------------------------- /spring-hateoas/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into employee(name,email_address) 2 | values('Soumitra','soumitra@email.com'); 3 | insert into employee(name,email_address) 4 | values('Liton','liton@email.com'); 5 | insert into employee(name,email_address) 6 | values('Suman','suman@email.com'); 7 | insert into employee(name,email_address) 8 | values('Debabrata','debabrata@email.com'); -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into employee(name,email_address) 2 | values('Soumitra','soumitra@email.com'); 3 | insert into employee(name,email_address) 4 | values('Liton','liton@email.com'); 5 | insert into employee(name,email_address) 6 | values('Suman','suman@email.com'); 7 | insert into employee(name,email_address) 8 | values('Debabrata','debabrata@email.com'); -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:roytuts 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.show-sql = true 7 | 8 | spring.h2.console.enabled = true 9 | spring.h2.console.path=/h2console/ 10 | 11 | spring.jpa.hibernate.ddl-auto = create -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/resources/data.sql: -------------------------------------------------------------------------------- 1 | insert into employee(name,email_address) 2 | values('Soumitra','soumitra@email.com'); 3 | insert into employee(name,email_address) 4 | values('Liton','liton@email.com'); 5 | insert into employee(name,email_address) 6 | values('Suman','suman@email.com'); 7 | insert into employee(name,email_address) 8 | values('Debabrata','debabrata@email.com'); -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.rest.download.file.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/constants/AppConstants.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.constants; 2 | 3 | public final class AppConstants { 4 | 5 | private AppConstants() { 6 | } 7 | 8 | public final static String SUCCESS = "SUCCESS"; 9 | public final static String INCORRECT_RESULT_SIZE = "Incorrect result size"; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/spring-boot-rest.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS `roytuts`; 2 | USE `roytuts`; 3 | 4 | CREATE TABLE IF NOT EXISTS `website` ( 5 | `id` int NOT NULL AUTO_INCREMENT, 6 | `title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, 7 | `url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, 8 | PRIMARY KEY (`id`) 9 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/repository/VisitorRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.online.visitor.tracker.entity.Visitor; 6 | 7 | public interface VisitorRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/java/com/roytuts/spring/rest/file/download/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.download.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.rest.file.download.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-rest-file-upload/src/main/java/com/roytuts/spring/rest/file/upload/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.upload; 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 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/repository/WebsiteRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.boot.angular.crud.rest.api.entity.Website; 6 | 7 | public interface WebsiteRepository extends JpaRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/java/roytuts/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package roytuts.config; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 6 | 7 | @Configuration 8 | @EnableWebMvc 9 | @ComponentScan(basePackages = "roytuts") 10 | public class WebMvcConfig { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/java/roytuts/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | package roytuts.config; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 6 | 7 | @Configuration 8 | @EnableWebMvc 9 | @ComponentScan(basePackages = "roytuts") 10 | public class WebMvcConfig { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/java/com/roytuts/spring/rest/file/download/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.download; 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 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1 2 | #spring.datasource.driverClassName=org.h2.Driver 3 | #spring.datasource.username=sa 4 | #spring.datasource.password= 5 | 6 | spring.jpa.show-sql=true 7 | spring.h2.console.enabled=true 8 | spring.jpa.hibernate.ddl-auto=none 9 | 10 | #spring.datasource.schema=classpath:schema.sql 11 | #spring.datasource.data=classpath:data.sql -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/java/com/roytuts/springrestserver/ServerApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class ServerApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(ServerApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/roytuts/spring/boot/swagger/SpringSwaggerApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.swagger; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringSwaggerApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringSwaggerApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-optional-path-variable/src/main/java/com/roytuts/spring/rest/optional/path/variable/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.optional.path.variable; 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 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/exception/DbException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.exception; 2 | 3 | public class DbException extends Exception { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public DbException(String message) { 8 | super(message); 9 | } 10 | 11 | public DbException(String message, Throwable cause) { 12 | super(message, cause); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user manual at https://docs.gradle.org/6.5.1/userguide/multi_project_builds.html 8 | */ 9 | 10 | rootProject.name = 'spring-rest-return-different-data-format' 11 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/SpringHateoas.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = "com.roytuts.spring.hateoas") 7 | public class SpringHateoas { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringHateoas.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.hateoas.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | Employee findById(int id); 10 | 11 | Employee findByName(String name); 12 | 13 | Employee findByEmail(String email); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap/src/main/java/com/roytuts/springboot/rest/api/multivaluedmap/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.rest.api.multivaluedmap; 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 | SpringApplication.run(App.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:h2:mem:roytuts 2 | spring.datasource.driverClassName=org.h2.Driver 3 | spring.datasource.username=sa 4 | spring.datasource.password= 5 | 6 | spring.jpa.show-sql = true 7 | 8 | spring.h2.console.enabled = true 9 | spring.h2.console.path=/h2console/ 10 | 11 | spring.jpa.hibernate.ddl-auto = create 12 | 13 | #insert data from SQL script after table creation 14 | spring.jpa.defer-datasource-initialization=true 15 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/src/main/java/com/roytuts/spring/rest/large/file/download/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.large.file.download.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | @Configuration 8 | public class AppConfig { 9 | 10 | @Bean 11 | public RestTemplate restTemplate() { 12 | return new RestTemplate(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.roytuts.spring.web.exception.handling.entity.Employee; 6 | 7 | public interface EmployeeRepository extends JpaRepository { 8 | 9 | Employee findByName(String name); 10 | 11 | Employee findByEmail(String email); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/java/com/roytuts/spring/rest/https/SpringRestHttpsServerApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRestHttpsServerApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRestHttpsServerApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/config/SpringConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.config; 2 | 3 | import org.springframework.boot.autoconfigure.domain.EntityScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 6 | 7 | @Configuration 8 | @EntityScan("com.roytuts.spring.hateoas.entity") 9 | @EnableJpaRepositories("com.roytuts.spring.hateoas.repository") 10 | public class SpringConfig { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/SpringAngularCrudRestApiApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringAngularCrudRestApiApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringAngularCrudRestApiApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/src/main/java/com/roytuts/spring/rest/large/file/download/SpringRestLargeFileDownloadApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.large.file.download; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRestLargeFileDownloadApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRestLargeFileDownloadApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/ClientApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.retry.annotation.EnableRetry; 6 | 7 | @EnableRetry 8 | @SpringBootApplication 9 | public class ClientApp { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(ClientApp.class, args); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-openapi-documentation/src/main/java/com/roytuts/spring/openapi/documentation/SpringOpenApiDocumentationApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.openapi.documentation; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringOpenApiDocumentationApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringOpenApiDocumentationApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/src/main/java/com/roytuts/spring/rest/different/data/format/SpringRestDataFormatsApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.different.data.format; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRestDataFormatsApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRestDataFormatsApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/src/main/java/com/roytuts/spring/rest/multiple/files/upload/SpringRestMultipleFilesUploadApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.multiple.files.upload; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRestMultipleFilesUploadApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRestMultipleFilesUploadApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-resttemplate-proxy-timeout/src/main/java/com/roytuts/spring/resttemplate/proxy/timeout/SpringRestTemplateProxyTimeoutApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.resttemplate.proxy.timeout; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringRestTemplateProxyTimeoutApp { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringRestTemplateProxyTimeoutApp.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/java/com/roytuts/java/spring/concurrency/concurrent/calls/BeanConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.concurrent.calls; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | @Configuration 8 | public class BeanConfig { 9 | 10 | @Bean 11 | public RestTemplate restTemplate() { 12 | return new RestTemplate(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #Spring Datasource 2 | spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 3 | spring.datasource.url=jdbc:mysql://localhost:3306/roytuts 4 | spring.datasource.username=root 5 | spring.datasource.password=root 6 | 7 | #ignore null fields in json 8 | spring.jackson.default-property-inclusion=NON_NULL 9 | 10 | #SQL related 11 | spring.jpa.show-sql = true 12 | spring.jpa.properties.hibernate.format_sql=true 13 | logging.level.org.hibernate.type.descriptor.sql=trace 14 | 15 | spring.jpa.hibernate.ddl-auto = create -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap-client/src/main/java/com/roytuts/springboot/rest/api/multivaluedmap/client/config/RestClientConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.rest.api.multivaluemap.client.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | @Configuration 8 | public class RestClientConfig { 9 | 10 | @Bean 11 | public RestTemplate restTemplate() { 12 | return new RestTemplate(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/java/com/roytuts/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.service; 2 | 3 | import java.util.List; 4 | 5 | import javax.annotation.Resource; 6 | 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.entity.Employee; 10 | import com.roytuts.repository.EmployeeRepository; 11 | 12 | @Service 13 | public class EmployeeService { 14 | 15 | @Resource 16 | private EmployeeRepository employeeRepository; 17 | 18 | public List getEmployees() { 19 | return employeeRepository.findAll(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/src/main/java/com/roytuts/spring/rest/large/file/download/dto/Input.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.large.file.download.dto; 2 | 3 | public class Input { 4 | 5 | private String downloadUrl; 6 | private String downloadPath; 7 | 8 | public String getDownloadUrl() { 9 | return downloadUrl; 10 | } 11 | 12 | public void setDownloadUrl(String downloadUrl) { 13 | this.downloadUrl = downloadUrl; 14 | } 15 | 16 | public String getDownloadPath() { 17 | return downloadPath; 18 | } 19 | 20 | public void setDownloadPath(String downloadPath) { 21 | this.downloadPath = downloadPath; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/java/com/roytuts/spring/rest/https/dto/Temperature.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.dto; 2 | 3 | import com.roytuts.spring.rest.https.enums.TemperatureUnit; 4 | 5 | public class Temperature { 6 | 7 | private double value; 8 | private TemperatureUnit unit; 9 | 10 | public double getValue() { 11 | return value; 12 | } 13 | 14 | public void setValue(double value) { 15 | this.value = value; 16 | } 17 | 18 | public TemperatureUnit getUnit() { 19 | return unit; 20 | } 21 | 22 | public void setUnit(TemperatureUnit unit) { 23 | this.unit = unit; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/java/com/roytuts/spring/rest/https/dto/Temperature.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.dto; 2 | 3 | import com.roytuts.spring.rest.https.enums.TemperatureUnit; 4 | 5 | public class Temperature { 6 | 7 | private double value; 8 | private TemperatureUnit unit; 9 | 10 | public double getValue() { 11 | return value; 12 | } 13 | 14 | public void setValue(double value) { 15 | this.value = value; 16 | } 17 | 18 | public TemperatureUnit getUnit() { 19 | return unit; 20 | } 21 | 22 | public void setUnit(TemperatureUnit unit) { 23 | this.unit = unit; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/java/roytuts/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package roytuts.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/service/VisitorService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.roytuts.spring.online.visitor.tracker.entity.Visitor; 7 | import com.roytuts.spring.online.visitor.tracker.repository.VisitorRepository; 8 | 9 | @Service 10 | public class VisitorService { 11 | 12 | @Autowired 13 | private VisitorRepository repository; 14 | 15 | public Visitor saveVisitorInfo(Visitor visitor) { 16 | return repository.save(visitor); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/java/roytuts/config/MvcWebApplicationInitializer.java: -------------------------------------------------------------------------------- 1 | package roytuts.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 6 | 7 | @Override 8 | protected Class[] getRootConfigClasses() { 9 | return new Class[] { WebMvcConfig.class }; 10 | } 11 | 12 | @Override 13 | protected Class[] getServletConfigClasses() { 14 | return null; 15 | } 16 | 17 | @Override 18 | protected String[] getServletMappings() { 19 | return new String[] { "/" }; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-rest-optional-path-variable/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.4.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | } -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/java/com/roytuts/spring/rest/file/download/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.download.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.stereotype.Service; 6 | 7 | import com.roytuts.spring.rest.file.download.entity.Employee; 8 | import com.roytuts.spring.rest.file.download.repository.EmployeeRepository; 9 | 10 | import jakarta.annotation.Resource; 11 | 12 | @Service 13 | public class EmployeeService { 14 | 15 | @Resource 16 | private EmployeeRepository employeeRepository; 17 | 18 | public List getEmployees() { 19 | return employeeRepository.findAll(); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.2.RELEASE' to '2.4.5' 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | } 13 | } 14 | 15 | plugins { 16 | id 'java-library' 17 | id 'org.springframework.boot' version "${springBootVersion}" 18 | } 19 | 20 | sourceCompatibility = 12 21 | targetCompatibility = 12 22 | 23 | repositories { 24 | mavenCentral() 25 | } 26 | 27 | dependencies { 28 | implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}" 29 | } -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/vo/EmployeeVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file.vo; 2 | 3 | public class EmployeeVo { 4 | 5 | private Integer id; 6 | private String name; 7 | private String email; 8 | 9 | public Integer getId() { 10 | return id; 11 | } 12 | 13 | public void setId(Integer id) { 14 | this.id = id; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public String getEmail() { 26 | return email; 27 | } 28 | 29 | public void setEmail(String email) { 30 | this.email = email; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-rest-file-upload/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.5.RELEASE' to 2.4.3 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version "${springBootVersion}" 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | } -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/vo/WebsiteVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.vo; 2 | 3 | public class WebsiteVo { 4 | 5 | private Integer id; 6 | private String url; 7 | private String title; 8 | 9 | public Integer getId() { 10 | return id; 11 | } 12 | 13 | public void setId(Integer id) { 14 | this.id = id; 15 | } 16 | 17 | public String getUrl() { 18 | return url; 19 | } 20 | 21 | public void setUrl(String url) { 22 | this.url = url; 23 | } 24 | 25 | public String getTitle() { 26 | return title; 27 | } 28 | 29 | public void setTitle(String title) { 30 | this.title = title; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/wrapper/ExceptionWrapper.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.wrapper; 2 | 3 | public class ExceptionWrapper { 4 | 5 | private String code; 6 | private String message; 7 | 8 | public ExceptionWrapper(String code, String message) { 9 | this.code = code; 10 | this.message = message; 11 | } 12 | 13 | public String getCode() { 14 | return code; 15 | } 16 | 17 | public void setCode(String code) { 18 | this.code = code; 19 | } 20 | 21 | public String getMessage() { 22 | return message; 23 | } 24 | 25 | public void setMessage(String message) { 26 | this.message = message; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.4.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version '2.2.4.RELEASE' 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | } -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/java/com/roytuts/main/Application.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.main; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @EntityScan(basePackages = "com.roytuts.entity") 9 | @SpringBootApplication(scanBasePackages = "com.roytuts") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.repository") 11 | public class Application { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(Application.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/vo/EmployeeVo.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.vo; 2 | 3 | import org.springframework.hateoas.EntityModel; 4 | 5 | public class EmployeeVo extends EntityModel { 6 | 7 | private Integer empId; 8 | private String name; 9 | private String email; 10 | 11 | public Integer getEmpId() { 12 | return empId; 13 | } 14 | 15 | public void setEmpId(Integer empId) { 16 | this.empId = empId; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public void setName(String name) { 24 | this.name = name; 25 | } 26 | 27 | public String getEmail() { 28 | return email; 29 | } 30 | 31 | public void setEmail(String email) { 32 | this.email = email; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/SpringRestH2FileDownloadApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan("com.roytuts.spring.rest.download.file.entity") 10 | @EnableJpaRepositories("com.roytuts.spring.rest.download.file.repository") 11 | public class SpringRestH2FileDownloadApp { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringRestH2FileDownloadApp.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/java/com/roytuts/springrestserver/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestserver.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.springrestserver.model.Employee; 9 | import com.roytuts.springrestserver.repository.EmployeeRepository; 10 | 11 | @Service 12 | public class EmployeeService { 13 | 14 | @Autowired 15 | private EmployeeRepository employeeRepository; 16 | 17 | public List getEmployeeList() { 18 | return employeeRepository.getEmployeeList(); 19 | } 20 | 21 | public Employee getEmployee() { 22 | return employeeRepository.getEmployeeById(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/java/com/roytuts/java/spring/concurrency/concurrent/calls/CustomAsyncExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.concurrent.calls; 2 | 3 | import java.lang.reflect.Method; 4 | 5 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; 6 | 7 | public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { 8 | 9 | @Override 10 | public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { 11 | System.out.println("Exception message - " + throwable.getMessage()); 12 | System.out.println("Method name - " + method.getName()); 13 | for (Object param : obj) { 14 | System.out.println("Parameter value - " + param); 15 | } 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /spring-hateoas/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.4.5' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-hateoas:${springBootVersion}") 27 | compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | runtime("com.h2database:h2:1.4.200") 29 | } -------------------------------------------------------------------------------- /spring-web-exception-handling/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.1.5.RELEASE' 4 | } 5 | repositories { 6 | mavenLocal() 7 | mavenCentral() 8 | } 9 | dependencies { 10 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 11 | } 12 | } 13 | 14 | apply plugin: 'java' 15 | apply plugin: 'org.springframework.boot' 16 | 17 | sourceCompatibility = 1.8 18 | targetCompatibility = 1.8 19 | 20 | repositories { 21 | mavenLocal() 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | compile("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | runtime("com.h2database:h2:1.4.196") 29 | } -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/OnlineVisitorTracker.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @SpringBootApplication 9 | @EntityScan(basePackages = "com.roytuts.spring.online.visitor.tracker.entity") 10 | @EnableJpaRepositories(basePackages = "com.roytuts.spring.online.visitor.tracker.repository") 11 | public class OnlineVisitorTracker { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(OnlineVisitorTracker.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/SpringWebExceptionHandlingApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.autoconfigure.domain.EntityScan; 6 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 7 | 8 | @EntityScan("com.roytuts.spring.web.exception.handling.entity") 9 | @EnableJpaRepositories("com.roytuts.spring.web.exception.handling.repository") 10 | @SpringBootApplication 11 | public class SpringWebExceptionHandlingApp { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SpringWebExceptionHandlingApp.class, args); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 6 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 7 | 8 | import com.roytuts.spring.online.visitor.tracker.logger.VisitorLogger; 9 | 10 | @Configuration 11 | public class AppConfig implements WebMvcConfigurer { 12 | 13 | @Autowired 14 | private VisitorLogger visitorLogger; 15 | 16 | @Override 17 | public void addInterceptors(InterceptorRegistry registry) { 18 | registry.addInterceptor(visitorLogger); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-openapi-documentation/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.4.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version '2.2.4.RELEASE' 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | //implementation('org.springdoc:springdoc-openapi-core:1.1.49') 28 | implementation('org.springdoc:springdoc-openapi-ui:1.2.32') 29 | } -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/exception/AppException.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.exception; 2 | 3 | public class AppException extends Exception { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | private String code; 8 | 9 | public AppException(String message) { 10 | super(message); 11 | this.code = null; 12 | } 13 | 14 | public AppException(Throwable cause) { 15 | super(cause); 16 | this.code = null; 17 | } 18 | 19 | public AppException(String message, String code) { 20 | super(message); 21 | this.code = code; 22 | } 23 | 24 | public AppException(String message, Throwable cause) { 25 | super(message, cause); 26 | this.code = null; 27 | } 28 | 29 | public String getCode() { 30 | return code; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/java/com/roytuts/springrestserver/repository/EmployeeRepository.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestserver.repository; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.roytuts.springrestserver.model.Employee; 9 | 10 | @Repository 11 | public class EmployeeRepository { 12 | 13 | public List getEmployeeList() { 14 | List employees = Arrays.asList(new Employee(1, "Smith John", 1000000.00), 15 | new Employee(2, "John Carle", 1200000.00), new Employee(3, "Micheal Vaun", 1020000.00), 16 | new Employee(4, "Jerome K Jerome", 1250000.00)); 17 | 18 | return employees; 19 | } 20 | 21 | public Employee getEmployeeById() { 22 | return new Employee(1, "Smith John", 1000000.00); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.4.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version '2.2.4.RELEASE' 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") { 27 | exclude group:'org.springframework.boot', module:'spring-boot-starter-tomcat' 28 | } 29 | 30 | implementation("org.apache.httpcomponents:httpclient:4.5.11") 31 | } -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/java/com/roytuts/java/spring/concurrency/concurrent/calls/AsyncService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.concurrent.calls; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.scheduling.annotation.Async; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | @Service 11 | public class AsyncService { 12 | 13 | @Autowired 14 | private RestTemplate restTemplate; 15 | 16 | @Async 17 | public CompletableFuture callMsgService() { 18 | final String msgServiceUrl = "http://localhost:8080/msg"; 19 | 20 | final String response = restTemplate.getForObject(msgServiceUrl, String.class); 21 | 22 | return CompletableFuture.completedFuture(response); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/java/com/roytuts/spring/rest/https/SpringRestHttpsClientApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https; 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 | import com.roytuts.spring.rest.https.client.TempConverterRestClient; 9 | 10 | @SpringBootApplication 11 | public class SpringRestHttpsClientApp implements CommandLineRunner { 12 | 13 | @Autowired 14 | private TempConverterRestClient tempConverterRestClient; 15 | 16 | public static void main(String[] args) { 17 | SpringApplication.run(SpringRestHttpsClientApp.class, args); 18 | } 19 | 20 | @Override 21 | public void run(String... args) throws Exception { 22 | tempConverterRestClient.temperature(); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-rest-file-download/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.2.5.RELEASE' to 2.4.3 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | plugins { 14 | id 'java-library' 15 | id 'org.springframework.boot' version "${springBootVersion}" 16 | } 17 | 18 | sourceCompatibility = 12 19 | targetCompatibility = 12 20 | 21 | repositories { 22 | mavenCentral() 23 | } 24 | 25 | dependencies { 26 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 27 | implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") 28 | implementation('mysql:mysql-connector-java:8.0.17')//8.0.22 29 | //required only if jdk 9 or higher version is used 30 | runtimeOnly('javax.xml.bind:jaxb-api:2.4.0-b180830.0359') 31 | } -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/src/main/java/com/roytuts/spring/rest/different/data/format/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.different.data.format.model; 2 | 3 | public class Employee { 4 | 5 | private String name; 6 | private String designation; 7 | 8 | public Employee() { 9 | } 10 | 11 | public Employee(String name, String designation) { 12 | this.name = name; 13 | this.designation = designation; 14 | } 15 | 16 | public String getName() { 17 | return name; 18 | } 19 | 20 | public void setName(String name) { 21 | this.name = name; 22 | } 23 | 24 | public String getDesignation() { 25 | return designation; 26 | } 27 | 28 | public void setDesignation(String designation) { 29 | this.designation = designation; 30 | } 31 | 32 | @Override 33 | public String toString() { 34 | return "Employee [name=" + name + ", designation=" + designation + "]"; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.3.5.RELEASE' to 2.4.5 4 | } 5 | 6 | repositories { 7 | mavenCentral() 8 | jcenter() 9 | } 10 | 11 | dependencies { 12 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 13 | } 14 | } 15 | 16 | plugins { 17 | id 'java-library' 18 | //id 'org.springframework.boot' version "${springBootVersion}" 19 | } 20 | 21 | sourceCompatibility = 12 22 | targetCompatibility = 12 23 | 24 | repositories { 25 | mavenCentral() 26 | jcenter() 27 | } 28 | 29 | dependencies { 30 | implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}") 31 | 32 | //required for JDK 9 or above for XML response 33 | implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.3' 34 | implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359' 35 | implementation 'org.glassfish.jaxb:jaxb-runtime:3.0.0-M5' 36 | } -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-service-api/src/main/java/com/roytuts/java/spring/concurrency/service/api/ServiceApiApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.service.api; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @SpringBootApplication 12 | public class ServiceApiApp { 13 | 14 | public static void main(String[] args) { 15 | SpringApplication.run(ServiceApiApp.class, args); 16 | } 17 | 18 | @GetMapping("/msg") 19 | public ResponseEntity sayHello() { 20 | try { 21 | Thread.sleep(1000); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | return new ResponseEntity("Hello", HttpStatus.OK); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/java/com/roytuts/springrestserver/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestserver.model; 2 | 3 | public class Employee { 4 | 5 | private Integer id; 6 | 7 | private String employeeName; 8 | 9 | private Double employeeSalary; 10 | 11 | public Employee() { 12 | } 13 | 14 | public Employee(Integer id, String employeeName, Double employeeSalary) { 15 | this.id = id; 16 | this.employeeName = employeeName; 17 | this.employeeSalary = employeeSalary; 18 | } 19 | 20 | public Integer getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getEmployeeName() { 29 | return employeeName; 30 | } 31 | 32 | public void setEmployeeName(String employeeName) { 33 | this.employeeName = employeeName; 34 | } 35 | 36 | public Double getEmployeeSalary() { 37 | return employeeSalary; 38 | } 39 | 40 | public void setEmployeeSalary(Double employeeSalary) { 41 | this.employeeSalary = employeeSalary; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file.service; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.spring.rest.download.file.entity.Employee; 10 | import com.roytuts.spring.rest.download.file.repository.EmployeeRepository; 11 | import com.roytuts.spring.rest.download.file.vo.EmployeeVo; 12 | 13 | @Service 14 | public class EmployeeService { 15 | 16 | @Autowired 17 | private EmployeeRepository employeeRepository; 18 | 19 | public List getEmployeeList() { 20 | 21 | List list = employeeRepository.findAll(); 22 | return list.stream().map(emp -> { 23 | EmployeeVo vo = new EmployeeVo(); 24 | vo.setId(emp.getId()); 25 | vo.setName(emp.getName()); 26 | vo.setEmail(emp.getEmail()); 27 | return vo; 28 | }).collect(Collectors.toList()); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/entity/Website.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Table 11 | @Entity 12 | public class Website { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.IDENTITY) 16 | private Integer id; 17 | 18 | @Column 19 | private String url; 20 | 21 | @Column 22 | private String title; 23 | 24 | public Integer getId() { 25 | return id; 26 | } 27 | 28 | public void setId(Integer id) { 29 | this.id = id; 30 | } 31 | 32 | public String getUrl() { 33 | return url; 34 | } 35 | 36 | public void setUrl(String url) { 37 | this.url = url; 38 | } 39 | 40 | public String getTitle() { 41 | return title; 42 | } 43 | 44 | public void setTitle(String title) { 45 | this.title = title; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/rest/controller/GreetingController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class GreetingController { 11 | 12 | @GetMapping("/") 13 | public ResponseEntity home() { 14 | return new ResponseEntity("Welcome to User Activity Logging", HttpStatus.OK); 15 | } 16 | 17 | @GetMapping("/hello/{name}") 18 | public ResponseEntity greet(@PathVariable String name) { 19 | return new ResponseEntity("Hello, " + name, HttpStatus.OK); 20 | } 21 | 22 | @GetMapping("/login") 23 | public ResponseEntity login() { 24 | return new ResponseEntity("Hello, this is a secured page", HttpStatus.OK); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.roytuts.springrestretry.model.Employee; 12 | import com.roytuts.springrestretry.service.EmployeeService; 13 | 14 | @RestController 15 | public class EmployeeRestController { 16 | 17 | @Autowired 18 | private EmployeeService employeeService; 19 | 20 | @GetMapping("/employees") 21 | public ResponseEntity> getEmployees() { 22 | List employees = employeeService.getEmployeeList(); 23 | //List employees = employeeService.getEmployeeList2(); 24 | 25 | return new ResponseEntity>(employees, HttpStatus.OK); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-rest-file-download/example.json: -------------------------------------------------------------------------------- 1 | { 2 | "quiz": { 3 | "sport": { 4 | "q1": { 5 | "question": "Which one is correct team name in NBA?", 6 | "options": [ 7 | "New York Bulls", 8 | "Los Angeles Kings", 9 | "Golden State Warriros", 10 | "Huston Rocket" 11 | ], 12 | "answer": "Huston Rocket" 13 | } 14 | }, 15 | "maths": { 16 | "q1": { 17 | "question": "5 + 7 = ?", 18 | "options": [ 19 | "10", 20 | "11", 21 | "12", 22 | "13" 23 | ], 24 | "answer": "12" 25 | }, 26 | "q2": { 27 | "question": "12 - 8 = ?", 28 | "options": [ 29 | "1", 30 | "2", 31 | "3", 32 | "4" 33 | ], 34 | "answer": "4" 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap/src/main/java/com/roytuts/springboot/rest/api/multivaluedmap/rest/controller/AppRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.rest.api.multivaluedmap.rest.controller; 2 | 3 | import java.util.stream.Collectors; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.util.MultiValueMap; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | @RestController 13 | public class AppRestController { 14 | 15 | @PostMapping("/multiValueMap") 16 | public ResponseEntity multiValueMap(@RequestBody MultiValueMap multiValueMap) { 17 | String encoding = multiValueMap.get("Accept-Encoding").stream().map(String::toString) 18 | .collect(Collectors.joining(",")); 19 | 20 | String contentType = multiValueMap.get("Content-Type").get(0); 21 | 22 | return new ResponseEntity(encoding + "," + contentType, HttpStatus.OK); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /spring-rest-optional-path-variable/src/main/java/com/roytuts/spring/rest/optional/path/variable/controller/SpringRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.optional.path.variable.controller; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @RestController 12 | public class SpringRestController { 13 | 14 | @GetMapping(value = { "/hi/{name}", "/hi" }) 15 | public ResponseEntity getHi(@PathVariable(required = false) String name) { 16 | return new ResponseEntity( 17 | "Hi " + (name == null || name.length() == 0 ? "Anonymous" : name) + ", Good Morning!", HttpStatus.OK); 18 | } 19 | 20 | @GetMapping(value = { "/hello/{name}", "/hello" }) 21 | public ResponseEntity getHello(@PathVariable Optional name) { 22 | return new ResponseEntity("Hello " + (name.isPresent() ? name.get() : "Anonymous") + ", Good Morning!", 23 | HttpStatus.OK); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/custom/CustomRetryPolicy.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry.custom; 2 | 3 | import org.springframework.retry.RetryContext; 4 | import org.springframework.retry.RetryPolicy; 5 | import org.springframework.retry.context.RetryContextSupport; 6 | 7 | public class CustomRetryPolicy implements RetryPolicy { 8 | 9 | private static final long serialVersionUID = 1L; 10 | 11 | private int maxAttempts; 12 | 13 | public CustomRetryPolicy(int maxAttempts) { 14 | this.maxAttempts = maxAttempts; 15 | } 16 | 17 | @Override 18 | public boolean canRetry(RetryContext context) { 19 | int attempts = context.getRetryCount(); 20 | return attempts < maxAttempts; 21 | } 22 | 23 | @Override 24 | public RetryContext open(RetryContext parent) { 25 | return new RetryContextSupport(parent); 26 | } 27 | 28 | @Override 29 | public void close(RetryContext context) { 30 | 31 | } 32 | 33 | @Override 34 | public void registerThrowable(RetryContext context, Throwable throwable) { 35 | RetryContextSupport retryContext = (RetryContextSupport) context; 36 | retryContext.registerThrowable(throwable); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/java/com/roytuts/java/spring/concurrency/concurrent/calls/AsyncConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.concurrent.calls; 2 | 3 | import java.util.concurrent.Executor; 4 | 5 | import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.scheduling.annotation.AsyncConfigurer; 8 | import org.springframework.scheduling.annotation.EnableAsync; 9 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 10 | 11 | @EnableAsync 12 | @Configuration 13 | public class AsyncConfig implements AsyncConfigurer { 14 | 15 | @Override 16 | public Executor getAsyncExecutor() { 17 | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 18 | //executor.setCorePoolSize(10); 19 | executor.setCorePoolSize(5); 20 | executor.setMaxPoolSize(25); 21 | executor.setQueueCapacity(100); 22 | executor.initialize(); 23 | return executor; 24 | } 25 | 26 | @Override 27 | public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { 28 | return new CustomAsyncExceptionHandler(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Employee { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | private Integer id; 15 | 16 | private String name; 17 | 18 | @Column(name = "email_address") 19 | private String email; 20 | 21 | public Employee() { 22 | } 23 | 24 | public Employee(Integer id, String name, String email) { 25 | this.id = id; 26 | this.name = name; 27 | this.email = email; 28 | } 29 | 30 | public Integer getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Integer id) { 35 | this.id = id; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getEmail() { 47 | return email; 48 | } 49 | 50 | public void setEmail(String email) { 51 | this.email = email; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Employee { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | @Column(name = "id") 15 | private Integer empId; 16 | private String name; 17 | @Column(name = "email_address") 18 | private String email; 19 | 20 | public Employee() { 21 | } 22 | 23 | public Employee(Integer empId, String name, String email) { 24 | this.empId = empId; 25 | this.name = name; 26 | this.email = email; 27 | } 28 | 29 | public Integer getEmpId() { 30 | return empId; 31 | } 32 | 33 | public void setEmpId(Integer empId) { 34 | this.empId = empId; 35 | } 36 | 37 | public String getName() { 38 | return name; 39 | } 40 | 41 | public void setName(String name) { 42 | this.name = name; 43 | } 44 | 45 | public String getEmail() { 46 | return email; 47 | } 48 | 49 | public void setEmail(String email) { 50 | this.email = email; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.entity; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.GeneratedValue; 6 | import javax.persistence.GenerationType; 7 | import javax.persistence.Id; 8 | 9 | @Entity 10 | public class Employee { 11 | 12 | @Id 13 | @GeneratedValue(strategy = GenerationType.IDENTITY) 14 | private Integer id; 15 | 16 | private String name; 17 | 18 | @Column(name = "email_address") 19 | private String email; 20 | 21 | public Employee() { 22 | } 23 | 24 | public Employee(Integer id, String name, String email) { 25 | this.id = id; 26 | this.name = name; 27 | this.email = email; 28 | } 29 | 30 | public Integer getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Integer id) { 35 | this.id = id; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public void setName(String name) { 43 | this.name = name; 44 | } 45 | 46 | public String getEmail() { 47 | return email; 48 | } 49 | 50 | public void setEmail(String email) { 51 | this.email = email; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/model/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry.model; 2 | 3 | public class Employee { 4 | 5 | private Integer id; 6 | 7 | private String employeeName; 8 | 9 | private Double employeeSalary; 10 | 11 | public Employee() { 12 | } 13 | 14 | public Employee(Integer id, String employeeName, Double employeeSalary) { 15 | this.id = id; 16 | this.employeeName = employeeName; 17 | this.employeeSalary = employeeSalary; 18 | } 19 | 20 | public Integer getId() { 21 | return id; 22 | } 23 | 24 | public void setId(Integer id) { 25 | this.id = id; 26 | } 27 | 28 | public String getEmployeeName() { 29 | return employeeName; 30 | } 31 | 32 | public void setEmployeeName(String employeeName) { 33 | this.employeeName = employeeName; 34 | } 35 | 36 | public Double getEmployeeSalary() { 37 | return employeeSalary; 38 | } 39 | 40 | public void setEmployeeSalary(Double employeeSalary) { 41 | this.employeeSalary = employeeSalary; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return "Employee [id=" + id + ", employeeName=" + employeeName + ", employeeSalary=" + employeeSalary + "]"; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/java/com/roytuts/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "employee") 12 | public class Employee implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | @Id 17 | @Column(name = "id") 18 | private Integer empId; 19 | 20 | @Column(name = "first_name") 21 | private String empFirstName; 22 | 23 | @Column(name = "last_name") 24 | private String empLastName; 25 | 26 | public Integer getEmpId() { 27 | return empId; 28 | } 29 | 30 | public void setEmpId(Integer empId) { 31 | this.empId = empId; 32 | } 33 | 34 | public String getEmpFirstName() { 35 | return empFirstName; 36 | } 37 | 38 | public void setEmpFirstName(String empFirstName) { 39 | this.empFirstName = empFirstName; 40 | } 41 | 42 | public String getEmpLastName() { 43 | return empLastName; 44 | } 45 | 46 | public void setEmpLastName(String empLastName) { 47 | this.empLastName = empLastName; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/src/main/java/com/roytuts/springrestserver/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestserver.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import com.roytuts.springrestserver.model.Employee; 13 | import com.roytuts.springrestserver.service.EmployeeService; 14 | 15 | @RestController 16 | public class EmployeeRestController { 17 | 18 | @Autowired 19 | private EmployeeService employeeService; 20 | 21 | @GetMapping("/employees") 22 | public ResponseEntity> getEmployees() { 23 | return new ResponseEntity>(employeeService.getEmployeeList(), HttpStatus.OK); 24 | } 25 | 26 | @GetMapping("/employee/{id}") 27 | public ResponseEntity getEmployee(@PathVariable Integer id) { 28 | return new ResponseEntity(employeeService.getEmployee(), HttpStatus.OK); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/java/com/roytuts/spring/rest/file/download/entity/Employee.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.download.entity; 2 | 3 | import java.io.Serializable; 4 | 5 | import jakarta.persistence.Column; 6 | import jakarta.persistence.Entity; 7 | import jakarta.persistence.Id; 8 | import jakarta.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "employee") 12 | public class Employee implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | @Id 17 | @Column(name = "id") 18 | private Integer empId; 19 | 20 | @Column(name = "first_name") 21 | private String empFirstName; 22 | 23 | @Column(name = "last_name") 24 | private String empLastName; 25 | 26 | public Integer getEmpId() { 27 | return empId; 28 | } 29 | 30 | public void setEmpId(Integer empId) { 31 | this.empId = empId; 32 | } 33 | 34 | public String getEmpFirstName() { 35 | return empFirstName; 36 | } 37 | 38 | public void setEmpFirstName(String empFirstName) { 39 | this.empFirstName = empFirstName; 40 | } 41 | 42 | public String getEmpLastName() { 43 | return empLastName; 44 | } 45 | 46 | public void setEmpLastName(String empLastName) { 47 | this.empLastName = empLastName; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-service-api/pom_3.5.4.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | spring-concurrency-service-api 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 22 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 3.5.4 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-rest-file-upload/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 4.0.0 6 | 7 | com.roytuts 8 | spring-rest-file-upload 9 | 0.0.1-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 12 14 | 12 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-parent 20 | 2.4.3 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-maven-plugin 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/pom_3.5.4.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-concurrency-concurrent-calls 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 22 13 | 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 3.5.4 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-maven-plugin 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /spring-rest-file-upload/pom.xml_3: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-file-upload 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/roytuts/spring/boot/swagger/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.swagger.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | import springfox.documentation.builders.ApiInfoBuilder; 7 | import springfox.documentation.builders.PathSelectors; 8 | import springfox.documentation.builders.RequestHandlerSelectors; 9 | import springfox.documentation.service.ApiInfo; 10 | import springfox.documentation.spi.DocumentationType; 11 | import springfox.documentation.spring.web.plugins.Docket; 12 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 13 | 14 | @Configuration 15 | @EnableSwagger2 16 | public class SwaggerConfig { 17 | 18 | @Bean 19 | public Docket docket() { 20 | Docket docket = new Docket(DocumentationType.SWAGGER_2); 21 | docket.apiInfo(apiInfo()).select() 22 | .apis(RequestHandlerSelectors.basePackage("com.roytuts.spring.boot.swagger.rest.controller")) 23 | .paths(PathSelectors.any()).build(); 24 | return docket; 25 | } 26 | 27 | private ApiInfo apiInfo() { 28 | return new ApiInfoBuilder().title("RESTful API").description("Browse and interact with RESTful API dynamically") 29 | .build(); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /spring-rest-optional-path-variable/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-optional-path-variable 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.0 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-maven-plugin 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-multiple-files-upload 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 2.4.5 16 | 17 | 18 | 19 | UTF-8 20 | 12 21 | 12 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.apache.maven.plugins 35 | maven-compiler-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-multiple-files-upload-client 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.0 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-maven-plugin 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-service-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-concurrency-service-api 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | springboot-rest-api-multivaluemap 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.4.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-concurrency-completablefuture 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap-client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | springboot-rest-api-multivaluemap-client 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.4.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/java/roytuts/model/Product.java: -------------------------------------------------------------------------------- 1 | package roytuts.model; 2 | 3 | public class Product { 4 | 5 | private int id; 6 | 7 | private String name; 8 | 9 | private double price; 10 | 11 | public Product() { 12 | } 13 | 14 | public Product(int id, String name, double price) { 15 | this.id = id; 16 | this.name = name; 17 | this.price = price; 18 | } 19 | 20 | public int getId() { 21 | return id; 22 | } 23 | 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public double getPrice() { 37 | return price; 38 | } 39 | 40 | public void setPrice(double price) { 41 | this.price = price; 42 | } 43 | 44 | @Override 45 | 46 | public int hashCode() { 47 | final int prime = 31; 48 | int result = 1; 49 | result = prime * result + (int) (id ^ (id >>> 32)); 50 | return result; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object obj) { 55 | if (this == obj) 56 | return true; 57 | 58 | if (obj == null) 59 | return false; 60 | 61 | if (getClass() != obj.getClass()) 62 | return false; 63 | 64 | Product other = (Product) obj; 65 | 66 | if (id != other.id) 67 | return false; 68 | 69 | return true; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/java/roytuts/model/Product.java: -------------------------------------------------------------------------------- 1 | package roytuts.model; 2 | 3 | public class Product { 4 | 5 | private int id; 6 | 7 | private String name; 8 | 9 | private double price; 10 | 11 | public Product() { 12 | } 13 | 14 | public Product(int id, String name, double price) { 15 | this.id = id; 16 | this.name = name; 17 | this.price = price; 18 | } 19 | 20 | public int getId() { 21 | return id; 22 | } 23 | 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | 36 | public double getPrice() { 37 | return price; 38 | } 39 | 40 | public void setPrice(double price) { 41 | this.price = price; 42 | } 43 | 44 | @Override 45 | 46 | public int hashCode() { 47 | final int prime = 31; 48 | int result = 1; 49 | result = prime * result + (int) (id ^ (id >>> 32)); 50 | return result; 51 | } 52 | 53 | @Override 54 | public boolean equals(Object obj) { 55 | if (this == obj) 56 | return true; 57 | 58 | if (obj == null) 59 | return false; 60 | 61 | if (getClass() != obj.getClass()) 62 | return false; 63 | 64 | Product other = (Product) obj; 65 | 66 | if (id != other.id) 67 | return false; 68 | 69 | return true; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/config/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry.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.retry.backoff.FixedBackOffPolicy; 7 | import org.springframework.retry.support.RetryTemplate; 8 | import org.springframework.web.client.RestTemplate; 9 | 10 | import com.roytuts.springrestretry.custom.CustomRetryPolicy; 11 | 12 | @Configuration 13 | public class AppConfig { 14 | 15 | @Value("${retry.maxAttempts:3}") 16 | private int maxAttempts; 17 | 18 | @Value("${retry.backoffInMillis:1000}") 19 | private long backoffInMillis; 20 | 21 | @Bean 22 | public RetryTemplate retryTemplate() { 23 | RetryTemplate retryTemplate = new RetryTemplate(); 24 | 25 | FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); 26 | backOffPolicy.setBackOffPeriod(backoffInMillis); 27 | retryTemplate.setBackOffPolicy(backOffPolicy); 28 | 29 | CustomRetryPolicy retryPolicy = new CustomRetryPolicy(maxAttempts); 30 | retryTemplate.setRetryPolicy(retryPolicy); 31 | 32 | return retryTemplate; 33 | } 34 | 35 | @Bean 36 | public RestTemplate restTemplate() { 37 | return new RestTemplate(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /springboot-rest-api-multivaluemap/springboot-rest-api-multivaluemap-client/src/main/java/com/roytuts/springboot/rest/api/multivaluedmap/client/App.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springboot.rest.api.multivaluemap.client; 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.http.MediaType; 8 | import org.springframework.util.LinkedMultiValueMap; 9 | import org.springframework.util.MultiValueMap; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | @SpringBootApplication 13 | public class App implements CommandLineRunner { 14 | 15 | @Autowired 16 | private RestTemplate restTemplate; 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(App.class, args).close(); 20 | } 21 | 22 | @Override 23 | public void run(String... args) throws Exception { 24 | MultiValueMap map = new LinkedMultiValueMap<>(); 25 | map.add("Accept-Encoding", "compress;q=0.5"); 26 | map.add("Accept-Encoding", "gzip;q=1.0"); 27 | map.add("Content-Type", MediaType.APPLICATION_JSON_VALUE); 28 | 29 | String response = restTemplate.postForObject("http://localhost:8080/multiValueMap", map, String.class); 30 | 31 | System.out.println("Response: " + response); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/java/com/roytuts/spring/rest/https/client/TempConverterRestClient.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.client; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | import com.roytuts.spring.rest.https.dto.Temperature; 8 | import com.roytuts.spring.rest.https.enums.TemperatureUnit; 9 | 10 | @Component 11 | public class TempConverterRestClient { 12 | 13 | @Autowired 14 | private RestTemplate restTemplate; 15 | 16 | public void temperature() { 17 | Temperature temperature = new Temperature(); 18 | temperature.setValue(32.0); 19 | temperature.setUnit(TemperatureUnit.CELSIUS); 20 | 21 | // final String url = "http://localhost:8080/temperature"; 22 | final String url = "https://localhost:8443/temperature"; 23 | 24 | Temperature temp = restTemplate.postForObject(url, temperature, Temperature.class); 25 | 26 | System.out.println(temp.getValue() + " " + temp.getUnit().name()); 27 | 28 | temperature = new Temperature(); 29 | temperature.setValue(89.6); 30 | temperature.setUnit(TemperatureUnit.FAHRENHEIT); 31 | 32 | temp = restTemplate.postForObject(url, temperature, Temperature.class); 33 | 34 | System.out.println(temp.getValue() + " " + temp.getUnit().name()); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-resttemplate-proxy-timeout/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-resttemplate-proxy-timeout 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 2.6.1 16 | 17 | 18 | 19 | UTF-8 20 | 16 21 | 16 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.apache.httpcomponents 32 | httpclient 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-large-file-download 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-webflux 33 | 34 | 35 | 36 | 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-maven-plugin 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-server 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.2.4 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-devtools 33 | runtime 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-maven-plugin 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/converter/EntityVoConverter.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.converter; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import com.roytuts.spring.boot.angular.crud.rest.api.entity.Website; 7 | import com.roytuts.spring.boot.angular.crud.rest.api.vo.WebsiteVo; 8 | 9 | public final class EntityVoConverter { 10 | 11 | private EntityVoConverter() { 12 | } 13 | 14 | public static WebsiteVo convertEntityToVo(Website website) { 15 | WebsiteVo websiteVo = new WebsiteVo(); 16 | websiteVo.setId(website.getId()); 17 | websiteVo.setTitle(website.getTitle()); 18 | websiteVo.setUrl(website.getUrl()); 19 | 20 | return websiteVo; 21 | } 22 | 23 | public static Website convertVoToEntity(WebsiteVo websiteVo) { 24 | Website website = new Website(); 25 | website.setId(websiteVo.getId()); 26 | website.setTitle(websiteVo.getTitle()); 27 | website.setUrl(websiteVo.getUrl()); 28 | 29 | return website; 30 | } 31 | 32 | public static List convertEntityToVoList(List websites) { 33 | return websites.stream().map(w -> { 34 | return convertEntityToVo(w); 35 | }).collect(Collectors.toList()); 36 | } 37 | 38 | public static List convertVoToEntityList(List websiteVos) { 39 | return websiteVos.stream().map(wvo -> { 40 | return convertVoToEntity(wvo); 41 | }).collect(Collectors.toList()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-server/src/main/java/com/roytuts/spring/rest/https/rest/controller/TempConverterRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.PostMapping; 6 | import org.springframework.web.bind.annotation.RequestBody; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import com.roytuts.spring.rest.https.dto.Temperature; 10 | import com.roytuts.spring.rest.https.enums.TemperatureUnit; 11 | 12 | @RestController 13 | public class TempConverterRestController { 14 | 15 | @PostMapping("/temperature") 16 | public ResponseEntity getTemperature(@RequestBody Temperature temperature) { 17 | Temperature temp = new Temperature(); 18 | 19 | if (TemperatureUnit.CELSIUS == temperature.getUnit()) { 20 | double fahrenheit = (9.0 / 5.0) * temperature.getValue() + 32; 21 | 22 | temp.setValue(fahrenheit); 23 | temp.setUnit(TemperatureUnit.FAHRENHEIT); 24 | } else if (TemperatureUnit.FAHRENHEIT == temperature.getUnit()) { 25 | double celsius = (5.0 / 9.0) * (temperature.getValue() - 32); 26 | 27 | temp.setValue(celsius); 28 | temp.setUnit(TemperatureUnit.CELSIUS); 29 | } else { 30 | throw new IllegalArgumentException("Illegal Argument"); 31 | } 32 | 33 | return new ResponseEntity(temp, HttpStatus.OK); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /spring-openapi-documentation/api-docs.yaml: -------------------------------------------------------------------------------- 1 | openapi: 3.0.1 2 | info: 3 | title: OpenAPI definition 4 | version: v0 5 | servers: 6 | - url: http://localhost:8080 7 | description: Generated server url 8 | paths: 9 | /greet: 10 | post: 11 | tags: 12 | - rest-api 13 | summary: Returns a response as Hello with a name 14 | description: This POST operation returns a response as Hello with a given name 15 | operationId: greet 16 | requestBody: 17 | content: 18 | application/json: 19 | schema: 20 | type: string 21 | required: true 22 | responses: 23 | "200": 24 | description: SUCCESS 25 | content: 26 | '*/*': 27 | schema: 28 | type: string 29 | "500": 30 | description: FAILURE 31 | content: 32 | '*/*': 33 | schema: 34 | type: string 35 | /: 36 | get: 37 | tags: 38 | - rest-api 39 | summary: Returns a response as Hello World! 40 | description: This GET operation returns a response as Hello World! 41 | operationId: greet_1 42 | responses: 43 | "200": 44 | description: SUCCESS 45 | content: 46 | '*/*': 47 | schema: 48 | type: string 49 | "500": 50 | description: FAILURE 51 | content: 52 | '*/*': 53 | schema: 54 | type: string 55 | components: {} 56 | -------------------------------------------------------------------------------- /spring-hateoas/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-hateoas 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 12 15 | 12 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.4.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-hateoas 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.h2database 37 | h2 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-openapi-documentation/pom.xml_3.5.4: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-openapi-documentation 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 22 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter-parent 20 | 3.5.4 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.springdoc 31 | springdoc-openapi-starter-webmvc-ui 32 | 2.8.9 33 | 34 | 35 | 36 | jakarta.servlet 37 | jakarta.servlet-api 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-file-download 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | com.mysql 37 | mysql-connector-j 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-openapi-documentation/pom.xml_3_2_5: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-openapi-documentation 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.2.5 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springdoc 32 | springdoc-openapi-starter-webmvc-ui 33 | 2.5.0 34 | 35 | 36 | 37 | jakarta.servlet 38 | jakarta.servlet-api 39 | 40 | 41 | 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-maven-plugin 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /spring-rest-file-download/src/main/java/com/roytuts/controller/FileDownloadRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpHeaders; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import com.fasterxml.jackson.databind.ObjectMapper; 14 | import com.roytuts.entity.Employee; 15 | import com.roytuts.service.EmployeeService; 16 | 17 | @RestController 18 | public class FileDownloadRestController { 19 | 20 | @Autowired 21 | private EmployeeService employeeService; 22 | 23 | @GetMapping("/download") 24 | public ResponseEntity downloadErrorData() throws Exception { 25 | List employees = employeeService.getEmployees(); 26 | ObjectMapper objectMapper = new ObjectMapper(); 27 | String json = objectMapper.writeValueAsString(employees); 28 | byte[] isr = json.getBytes(); 29 | String fileName = "employees.json"; 30 | HttpHeaders respHeaders = new HttpHeaders(); 31 | respHeaders.setContentLength(isr.length); 32 | respHeaders.setContentType(new MediaType("text", "json")); 33 | respHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0"); 34 | respHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName); 35 | return new ResponseEntity(isr, respHeaders, HttpStatus.OK); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-openapi-documentation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-openapi-documentation 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.1.0 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springdoc 32 | springdoc-openapi-starter-webmvc-ui 33 | 2.1.0 34 | 35 | 36 | 37 | javax.servlet 38 | javax.servlet-api 39 | 4.0.1 40 | provided 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-rest-file-upload/src/main/java/com/roytuts/spring/rest/file/upload/controller/FileUploadRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.upload.controller; 2 | 3 | import java.io.InputStream; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.PostMapping; 10 | import org.springframework.web.bind.annotation.RequestParam; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.multipart.MultipartFile; 13 | 14 | @RestController 15 | public class FileUploadRestController { 16 | 17 | private static final Logger logger = LoggerFactory.getLogger(FileUploadRestController.class.getName()); 18 | 19 | @PostMapping("/upload") 20 | public ResponseEntity uploadData(@RequestParam("file") MultipartFile file) throws Exception { 21 | 22 | if (file == null) { 23 | throw new RuntimeException("You must select the a file for uploading"); 24 | } 25 | 26 | InputStream inputStream = file.getInputStream(); 27 | String originalName = file.getOriginalFilename(); 28 | String name = file.getName(); 29 | String contentType = file.getContentType(); 30 | long size = file.getSize(); 31 | 32 | logger.info("inputStream: " + inputStream); 33 | logger.info("originalName: " + originalName); 34 | logger.info("name: " + name); 35 | logger.info("contentType: " + contentType); 36 | logger.info("size: " + size); 37 | 38 | // Do processing with uploaded file data in Service layer 39 | return new ResponseEntity(originalName, HttpStatus.OK); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-online-visitor-tracker 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.4 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-security 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | 39 | com.h2database 40 | h2 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-web-exception-handling/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | spring-web-exception-handling 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 11 13 | 11 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.6.6 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-devtools 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | 39 | com.h2database 40 | h2 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /spring-resttemplate-proxy-timeout/src/main/java/com/roytuts/spring/resttemplate/proxy/timeout/RestApiController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.resttemplate.proxy.timeout; 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.http.HttpStatus; 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.client.RestTemplate; 13 | 14 | @RestController 15 | public class RestApiController { 16 | 17 | @Autowired 18 | private RestTemplate restTemplate; 19 | 20 | @GetMapping("/fetch") 21 | public ResponseEntity fetchSomething() { 22 | Object response = restTemplate.getForObject("url of the REST API", Object.class); 23 | 24 | return new ResponseEntity(response, HttpStatus.OK); 25 | } 26 | 27 | @PostMapping("/create") 28 | public ResponseEntity createSomething(@RequestBody Object obj) { 29 | HttpHeaders httpHeaders = new HttpHeaders(); 30 | httpHeaders.set("Content-Type", "application/json"); 31 | httpHeaders.set("Authorization", "Bearer token value"); 32 | 33 | Object request = new Object(); 34 | 35 | HttpEntity httpEntity = new HttpEntity(request, httpHeaders); 36 | 37 | Object response = restTemplate.postForObject("url of the REST API", httpEntity, Object.class); 38 | 39 | return new ResponseEntity(response, HttpStatus.OK); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-retry 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 19 15 | 19 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 3.2.4 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.retry 32 | spring-retry 33 | 34 | 35 | 36 | org.springframework 37 | spring-aspects 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-devtools 43 | runtime 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-boot-swagger/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.roytuts 6 | spring-boot-swagger 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | UTF-8 12 | 8 13 | 8 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.5.4 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | 33 | 34 | 35 | io.springfox 36 | springfox-swagger2 37 | 3.0.0 38 | 39 | 40 | 41 | io.springfox 42 | springfox-swagger-ui 43 | 3.0.0 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/config/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.config; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 7 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 10 | import org.springframework.security.crypto.password.PasswordEncoder; 11 | 12 | @Configuration 13 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 14 | 15 | private static final String PASSWORD_ENCODED = "$2a$10$B18wGZhRjNau6ZBcI/NBQO6EEdJ7GyVvjjWfAK20ODz4B6LmXicRa";// roy 16 | 17 | @Override 18 | protected void configure(HttpSecurity http) throws Exception { 19 | http.headers().frameOptions().sameOrigin().and().authorizeRequests() 20 | .antMatchers("/", "/hello/**", "/h2console/**").permitAll().anyRequest().authenticated().and() 21 | .httpBasic(); 22 | http.csrf().disable(); 23 | http.headers().frameOptions().disable(); 24 | } 25 | 26 | @Autowired 27 | public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 28 | auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser("roy").password(PASSWORD_ENCODED) 29 | .roles("USER"); 30 | } 31 | 32 | @Bean 33 | public PasswordEncoder passwordEncoder() { 34 | return new BCryptPasswordEncoder(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-rest-file-download/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-file-download 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 12 15 | 12 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.4.3 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 39 | 40 | 41 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-rest-return-different-data-format 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | org.springframework.boot 14 | spring-boot-starter-parent 15 | 2.4.5 16 | 17 | 18 | 19 | UTF-8 20 | 12 21 | 12 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | com.fasterxml.jackson.dataformat 32 | jackson-dataformat-xml 33 | 34 | 35 | 36 | 37 | org.glassfish.jaxb 38 | jaxb-runtime 39 | 40 | 41 | 42 | javax.xml.bind 43 | jaxb-api 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-compiler-plugin 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 4.0.0 7 | 8 | com.roytuts 9 | spring-boot-angular-crud-rest-api 10 | 0.0.1-SNAPSHOT 11 | 12 | 13 | UTF-8 14 | 16 15 | 16 16 | 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 2.6.1 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-data-jpa 33 | 34 | 35 | 36 | mysql 37 | mysql-connector-java 38 | 39 | 40 | 41 | 42 | javax.xml.bind 43 | jaxb-api 44 | runtime 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-rest-file-download/spring-boot-3/src/main/java/com/roytuts/spring/rest/file/download/controller/FileDownloadRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.file.download.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpHeaders; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import com.fasterxml.jackson.databind.ObjectMapper; 14 | import com.roytuts.spring.rest.file.download.entity.Employee; 15 | import com.roytuts.spring.rest.file.download.service.EmployeeService; 16 | 17 | @RestController 18 | public class FileDownloadRestController { 19 | 20 | @Autowired 21 | private EmployeeService employeeService; 22 | 23 | @GetMapping("/download") 24 | public ResponseEntity downloadErrorData() throws Exception { 25 | 26 | List employees = employeeService.getEmployees(); 27 | 28 | ObjectMapper objectMapper = new ObjectMapper(); 29 | 30 | String json = objectMapper.writeValueAsString(employees); 31 | 32 | byte[] isr = json.getBytes(); 33 | 34 | String fileName = "employees.json"; 35 | 36 | HttpHeaders respHeaders = new HttpHeaders(); 37 | 38 | respHeaders.setContentLength(isr.length); 39 | respHeaders.setContentType(new MediaType("text", "json")); 40 | respHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0"); 41 | respHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName); 42 | 43 | return new ResponseEntity(isr, respHeaders, HttpStatus.OK); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 4.0.0 5 | 6 | com.roytuts 7 | spring-rest-h2-download-file 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | UTF-8 12 | 11 13 | 11 14 | 15 | 16 | 17 | org.springframework.boot 18 | spring-boot-starter-parent 19 | 2.7.6 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 | runtime 37 | 38 | 39 | 40 | 41 | javax.xml.bind 42 | jaxb-api 43 | 2.4.0-b180830.0359 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-maven-plugin 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-concurrency-completablefuture/spring-concurrency-concurrent-calls/src/main/java/com/roytuts/java/spring/concurrency/concurrent/calls/ConcurrentRunnerApp.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.java.spring.concurrency.concurrent.calls; 2 | 3 | import java.time.Duration; 4 | import java.time.Instant; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.concurrent.CompletableFuture; 8 | 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.boot.CommandLineRunner; 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.ConfigurableApplicationContext; 14 | 15 | @SpringBootApplication 16 | public class ConcurrentRunnerApp implements CommandLineRunner { 17 | 18 | @Autowired 19 | private AsyncService asyncService; 20 | 21 | @Autowired 22 | private ConfigurableApplicationContext context; 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(ConcurrentRunnerApp.class, args);// .close(); 26 | } 27 | 28 | @Override 29 | public void run(String... args) throws Exception { 30 | Instant start = Instant.now(); 31 | 32 | List> allFutures = new ArrayList<>(); 33 | 34 | for (int i = 0; i < 10; i++) { 35 | allFutures.add(asyncService.callMsgService()); 36 | } 37 | 38 | CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[0])).join(); 39 | 40 | for (int i = 0; i < 10; i++) { 41 | System.out.println("response: " + allFutures.get(i).get().toString()); 42 | } 43 | 44 | Instant finish = Instant.now(); 45 | 46 | long timeElapsed = Duration.between(start, finish).toMillis(); 47 | 48 | System.out.println("Total time: " + timeElapsed + " ms"); 49 | 50 | System.exit(SpringApplication.exit(context)); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-rest-h2-download-file/src/main/java/com/roytuts/spring/rest/download/file/rest/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.download.file.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpHeaders; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.CrossOrigin; 11 | import org.springframework.web.bind.annotation.GetMapping; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | import com.fasterxml.jackson.databind.ObjectMapper; 15 | import com.roytuts.spring.rest.download.file.service.EmployeeService; 16 | import com.roytuts.spring.rest.download.file.vo.EmployeeVo; 17 | 18 | @RestController 19 | @CrossOrigin(origins = "http://localhost:4200") 20 | public class EmployeeRestController { 21 | 22 | @Autowired 23 | private EmployeeService employeeService; 24 | 25 | @GetMapping("/employees/download") 26 | public ResponseEntity downloadFile() throws Exception { 27 | 28 | List employees = employeeService.getEmployeeList(); 29 | ObjectMapper objectMapper = new ObjectMapper(); 30 | String json = objectMapper.writeValueAsString(employees); 31 | byte[] isr = json.getBytes(); 32 | 33 | String fileName = "employees.json"; 34 | 35 | HttpHeaders respHeaders = new HttpHeaders(); 36 | respHeaders.setContentLength(isr.length); 37 | respHeaders.setContentType(new MediaType("text", "json")); 38 | respHeaders.setCacheControl("must-revalidate, post-check=0, pre-check=0"); 39 | respHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName); 40 | 41 | return new ResponseEntity(isr, respHeaders, HttpStatus.OK); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/java/roytuts/sevice/ProductService.java: -------------------------------------------------------------------------------- 1 | package roytuts.sevice; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | import org.springframework.stereotype.Service; 9 | 10 | import roytuts.model.Product; 11 | 12 | @Service 13 | public class ProductService { 14 | private static AtomicInteger counter = new AtomicInteger(); 15 | 16 | private static List products; 17 | 18 | static { 19 | products = populateProducts(); 20 | } 21 | 22 | public Product findProductById(int id) { 23 | for (Product product : products) { 24 | if (id == product.getId()) { 25 | return product; 26 | } 27 | } 28 | return null; 29 | } 30 | 31 | public void saveProduct(Product product) { 32 | product.setId(counter.incrementAndGet()); 33 | products.add(product); 34 | } 35 | 36 | public void updateProduct(Product product) { 37 | int index = products.indexOf(product); 38 | products.set(index, product); 39 | } 40 | 41 | public void deleteProductById(int id) { 42 | Iterator it = products.iterator(); 43 | while (it.hasNext()) { 44 | Product product = it.next(); 45 | if (id == product.getId()) { 46 | it.remove(); 47 | } 48 | } 49 | } 50 | 51 | public boolean isProductAvailable(Product product) { 52 | return findProductById(product.getId()) != null; 53 | } 54 | 55 | private static List populateProducts() { 56 | List products = new ArrayList(); 57 | products.add(new Product(counter.incrementAndGet(), "Mobile", 25498.00)); 58 | products.add(new Product(counter.incrementAndGet(), "Desktop", 32658.00)); 59 | products.add(new Product(counter.incrementAndGet(), "Laptop", 52147.00)); 60 | products.add(new Product(counter.incrementAndGet(), "Tab", 18254.00)); 61 | return products; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/java/roytuts/sevice/ProductService.java: -------------------------------------------------------------------------------- 1 | package roytuts.sevice; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | import org.springframework.stereotype.Service; 9 | 10 | import roytuts.model.Product; 11 | 12 | @Service 13 | public class ProductService { 14 | private static AtomicInteger counter = new AtomicInteger(); 15 | 16 | private static List products; 17 | 18 | static { 19 | products = populateProducts(); 20 | } 21 | 22 | public Product findProductById(int id) { 23 | for (Product product : products) { 24 | if (id == product.getId()) { 25 | return product; 26 | } 27 | } 28 | return null; 29 | } 30 | 31 | public void saveProduct(Product product) { 32 | product.setId(counter.incrementAndGet()); 33 | products.add(product); 34 | } 35 | 36 | public void updateProduct(Product product) { 37 | int index = products.indexOf(product); 38 | products.set(index, product); 39 | } 40 | 41 | public void deleteProductById(int id) { 42 | Iterator it = products.iterator(); 43 | while (it.hasNext()) { 44 | Product product = it.next(); 45 | if (id == product.getId()) { 46 | it.remove(); 47 | } 48 | } 49 | } 50 | 51 | public boolean isProductAvailable(Product product) { 52 | return findProductById(product.getId()) != null; 53 | } 54 | 55 | private static List populateProducts() { 56 | List products = new ArrayList(); 57 | products.add(new Product(counter.incrementAndGet(), "Mobile", 25498.00)); 58 | products.add(new Product(counter.incrementAndGet(), "Desktop", 32658.00)); 59 | products.add(new Product(counter.incrementAndGet(), "Laptop", 52147.00)); 60 | products.add(new Product(counter.incrementAndGet(), "Tab", 18254.00)); 61 | return products; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /spring-rest-file-download/employee.sql: -------------------------------------------------------------------------------- 1 | -- -------------------------------------------------------- 2 | -- Host: 127.0.0.1 3 | -- Server version: 8.0.17 - MySQL Community Server - GPL 4 | -- Server OS: Win64 5 | -- HeidiSQL Version: 10.2.0.5599 6 | -- -------------------------------------------------------- 7 | 8 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 9 | /*!40101 SET NAMES utf8 */; 10 | /*!50503 SET NAMES utf8mb4 */; 11 | /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; 12 | /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; 13 | 14 | 15 | -- Dumping database structure for roytuts 16 | CREATE DATABASE IF NOT EXISTS `roytuts` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */ /*!80016 DEFAULT ENCRYPTION='N' */; 17 | USE `roytuts`; 18 | 19 | -- Dumping structure for table roytuts.employee 20 | CREATE TABLE IF NOT EXISTS `employee` ( 21 | `id` int(11) NOT NULL DEFAULT '0', 22 | `first_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, 23 | `last_name` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL, 24 | PRIMARY KEY (`id`) 25 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 26 | 27 | -- Dumping data for table roytuts.employee: ~0 rows (approximately) 28 | /*!40000 ALTER TABLE `employee` DISABLE KEYS */; 29 | INSERT INTO `employee` (`id`, `first_name`, `last_name`) VALUES 30 | (1003349, 'Liton', 'Sarkar'), 31 | (1003350, 'Debabrata', 'Poddar'), 32 | (1003351, 'Abhisekh', 'Paul'), 33 | (1003352, 'Sumit', 'Ghosh'), 34 | (1003353, 'Manoj', 'Kumar'), 35 | (1003354, 'Sreeja', 'Chowdhury'); 36 | /*!40000 ALTER TABLE `employee` ENABLE KEYS */; 37 | 38 | /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; 39 | /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; 40 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 41 | -------------------------------------------------------------------------------- /spring-openapi-documentation/src/main/java/com/roytuts/spring/openapi/documentation/rest/controller/RestApi.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.openapi.documentation.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PostMapping; 7 | import org.springframework.web.bind.annotation.RequestBody; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import io.swagger.v3.oas.annotations.Operation; 11 | import io.swagger.v3.oas.annotations.media.Content; 12 | import io.swagger.v3.oas.annotations.media.Schema; 13 | import io.swagger.v3.oas.annotations.responses.ApiResponse; 14 | import io.swagger.v3.oas.annotations.responses.ApiResponses; 15 | 16 | @RestController 17 | public class RestApi { 18 | 19 | @Operation(summary = "Returns a response as Hello World!", description = "This GET operation returns a response as Hello World!") 20 | @ApiResponses({ 21 | @ApiResponse(responseCode = "200", description = "SUCCESS", content = @Content(schema = @Schema(type = "string"))), 22 | @ApiResponse(responseCode = "500", description = "FAILURE", content = @Content(schema = @Schema(implementation = RuntimeException.class))) }) 23 | @GetMapping("/") 24 | public ResponseEntity greet() { 25 | return new ResponseEntity("Hello World!", HttpStatus.OK); 26 | } 27 | 28 | @Operation(summary = "Returns a response as Hello with a name", description = "This POST operation returns a response as Hello with a given name") 29 | @ApiResponses({ 30 | @ApiResponse(responseCode = "200", description = "SUCCESS", content = @Content(schema = @Schema(type = "string"))), 31 | @ApiResponse(responseCode = "500", description = "FAILURE", content = @Content(schema = @Schema(implementation = RuntimeException.class))) }) 32 | @PostMapping("/greet") 33 | public ResponseEntity greet(@RequestBody String name) { 34 | return new ResponseEntity("Hello, " + name, HttpStatus.OK); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/logger/VisitorLogger.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.logger; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | import javax.servlet.http.HttpServletRequest; 6 | import javax.servlet.http.HttpServletResponse; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.web.servlet.HandlerInterceptor; 11 | 12 | import com.roytuts.spring.online.visitor.tracker.entity.Visitor; 13 | import com.roytuts.spring.online.visitor.tracker.service.VisitorService; 14 | import com.roytuts.spring.online.visitor.tracker.utils.HttpRequestResponseUtils; 15 | 16 | @Component 17 | public class VisitorLogger implements HandlerInterceptor { 18 | 19 | @Autowired 20 | private VisitorService visitorService; 21 | 22 | @Override 23 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 24 | throws Exception { 25 | 26 | final String ip = HttpRequestResponseUtils.getClientIpAddress(); 27 | final String url = HttpRequestResponseUtils.getRequestUrl(); 28 | final String page = HttpRequestResponseUtils.getRequestUri(); 29 | final String refererPage = HttpRequestResponseUtils.getRefererPage(); 30 | final String queryString = HttpRequestResponseUtils.getPageQueryString(); 31 | final String userAgent = HttpRequestResponseUtils.getUserAgent(); 32 | final String requestMethod = HttpRequestResponseUtils.getRequestMethod(); 33 | final LocalDateTime timestamp = LocalDateTime.now(); 34 | 35 | Visitor visitor = new Visitor(); 36 | visitor.setUser(HttpRequestResponseUtils.getLoggedInUser()); 37 | visitor.setIp(ip); 38 | visitor.setMethod(requestMethod); 39 | visitor.setUrl(url); 40 | visitor.setPage(page); 41 | visitor.setQueryString(queryString); 42 | visitor.setRefererPage(refererPage); 43 | visitor.setUserAgent(userAgent); 44 | visitor.setLoggedTime(timestamp); 45 | visitor.setUniqueVisit(true); 46 | 47 | visitorService.saveVisitorInfo(visitor); 48 | 49 | return true; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /spring-boot-swagger/src/main/java/com/roytuts/spring/boot/swagger/rest/controller/GreetingController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.swagger.rest.controller; 2 | 3 | import org.springframework.http.HttpStatus; 4 | import org.springframework.http.ResponseEntity; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import io.swagger.annotations.Api; 12 | import io.swagger.annotations.ApiOperation; 13 | import io.swagger.annotations.ApiResponse; 14 | import io.swagger.annotations.ApiResponses; 15 | 16 | @RestController 17 | //@RequestMapping("/") 18 | @Api(tags = "Message API", value = "Controller handles different messages") 19 | public class GreetingController { 20 | 21 | @GetMapping("/hello") 22 | @ApiOperation(value = "Returns a String", notes = "Method to return a string message.") 23 | @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), 24 | @ApiResponse(code = 404, message = "The resource not found") }) 25 | public ResponseEntity getMsg() { 26 | return new ResponseEntity<>("Hello, Welcome to Swagger", HttpStatus.OK); 27 | } 28 | 29 | @GetMapping("/hello/{name}") 30 | @ApiOperation(value = "Returns a String with name", notes = "Method to return a string message with name.") 31 | @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), 32 | @ApiResponse(code = 404, message = "The resource not found") }) 33 | public ResponseEntity getMsg(@PathVariable String name) { 34 | return new ResponseEntity<>("Hello " + name, HttpStatus.OK); 35 | } 36 | 37 | @PostMapping("/hello") 38 | @ApiOperation(value = "Returns a String with name", notes = "Method to return a string message with name.") 39 | @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), 40 | @ApiResponse(code = 404, message = "The resource not found") }) 41 | public ResponseEntity msg(@RequestBody String name) { 42 | return new ResponseEntity<>("Hello " + name, HttpStatus.OK); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-resttemplate-proxy-timeout/src/main/java/com/roytuts/spring/resttemplate/proxy/timeout/Config.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.resttemplate.proxy.timeout; 2 | 3 | import org.apache.http.HttpHost; 4 | import org.apache.http.client.HttpClient; 5 | import org.apache.http.impl.client.HttpClientBuilder; 6 | import org.springframework.boot.web.client.RestTemplateBuilder; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | @Configuration 13 | public class Config { 14 | 15 | @Bean 16 | public RestTemplate restTemplate(RestTemplateBuilder builder) { 17 | 18 | HttpHost httpHost = new HttpHost("proxy-cloud.example.com", 8080); 19 | 20 | HttpClientBuilder clientBuilder = HttpClientBuilder.create(); 21 | clientBuilder.setProxy(httpHost); 22 | 23 | HttpClient httpClient = clientBuilder.build(); 24 | 25 | HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); 26 | //HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); 27 | // factory.setHttpClient(httpClient); 28 | factory.setConnectionRequestTimeout(1000); 29 | factory.setConnectTimeout(1000); 30 | factory.setReadTimeout(1000); 31 | 32 | return builder.requestFactory(() -> factory).build(); 33 | } 34 | 35 | @Bean 36 | public RestTemplate restTemplate() { 37 | HttpHost httpHost = new HttpHost("proxy-cloud.example.com", 8080); 38 | 39 | HttpClientBuilder clientBuilder = HttpClientBuilder.create(); 40 | clientBuilder.setProxy(httpHost); 41 | 42 | HttpClient httpClient = clientBuilder.build(); 43 | 44 | HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); 45 | ////HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); 46 | // factory.setHttpClient(httpClient); 47 | factory.setConnectionRequestTimeout(1000); 48 | factory.setConnectTimeout(1000); 49 | factory.setReadTimeout(1000); 50 | 51 | return new RestTemplate(factory); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-rest-return-different-data-format/src/main/java/com/roytuts/spring/rest/different/data/format/rest/controller/SpringRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.different.data.format.rest.controller; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.MediaType; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import com.roytuts.spring.rest.different.data.format.model.Employee; 13 | 14 | @RestController 15 | public class SpringRestController { 16 | 17 | @GetMapping(value = "/entityEmployees", produces = { MediaType.APPLICATION_JSON_VALUE, 18 | MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE }) 19 | public ResponseEntity> getEmployeesEntity() { 20 | return new ResponseEntity>(employees(), HttpStatus.OK); 21 | } 22 | 23 | @GetMapping(value = "/employees", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, 24 | MediaType.TEXT_XML_VALUE }) 25 | public List getEmployees() { 26 | return employees(); 27 | } 28 | 29 | @GetMapping(value = "/entityEmployee", produces = { MediaType.APPLICATION_JSON_VALUE, 30 | MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE }) 31 | public ResponseEntity getEmployeeEntity() { 32 | return new ResponseEntity(new Employee("John", "Developer"), HttpStatus.OK); 33 | } 34 | 35 | @GetMapping(value = "/employee", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE, 36 | MediaType.TEXT_XML_VALUE }) 37 | public Employee getEmployee() { 38 | return new Employee("John", "Developer"); 39 | } 40 | 41 | private List employees() { 42 | List employees = Arrays.asList(new Employee("John", "Developer"), 43 | new Employee("Michel", "Sr Developer"), new Employee("Harris", "Developer"), 44 | new Employee("Kamla", "Sr Developer"), new Employee("Jerome", "Manager")); 45 | 46 | return employees; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/rest/controller/WebsiteRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.CrossOrigin; 9 | import org.springframework.web.bind.annotation.DeleteMapping; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.PutMapping; 14 | import org.springframework.web.bind.annotation.RequestBody; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | import com.roytuts.spring.boot.angular.crud.rest.api.service.WebsiteService; 18 | import com.roytuts.spring.boot.angular.crud.rest.api.vo.WebsiteVo; 19 | 20 | @RestController 21 | @CrossOrigin(value = "*") 22 | public class WebsiteRestController { 23 | 24 | @Autowired 25 | private WebsiteService websiteService; 26 | 27 | @GetMapping("/websites") 28 | public ResponseEntity> getWebsites() throws Exception { 29 | return new ResponseEntity>(websiteService.getWebsites(), HttpStatus.OK); 30 | } 31 | 32 | @GetMapping("/website/{id}") 33 | public ResponseEntity getWebsite(@PathVariable Integer id) throws Exception { 34 | return new ResponseEntity(websiteService.getWebsite(id), HttpStatus.OK); 35 | } 36 | 37 | @PostMapping("/website") 38 | public ResponseEntity saveWebsite(@RequestBody WebsiteVo website) throws Exception { 39 | websiteService.saveWebsite(website); 40 | return new ResponseEntity(HttpStatus.CREATED); 41 | } 42 | 43 | @PutMapping("/website") 44 | public ResponseEntity updateWebsite(@RequestBody WebsiteVo website) throws Exception { 45 | websiteService.updateWebsite(website); 46 | return new ResponseEntity(HttpStatus.OK); 47 | } 48 | 49 | @DeleteMapping("/website/{id}") 50 | public ResponseEntity deleteWebsite(@PathVariable Integer id) throws Exception { 51 | websiteService.deleteWebsite(id); 52 | return new ResponseEntity(HttpStatus.OK); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-rest-https-certificate/spring-rest-https-client/src/main/java/com/roytuts/spring/rest/https/config/RestConfig.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.https.config; 2 | 3 | import java.io.InputStream; 4 | import java.security.KeyStore; 5 | import java.util.function.Supplier; 6 | 7 | import javax.net.ssl.SSLContext; 8 | 9 | import org.apache.http.conn.ssl.NoopHostnameVerifier; 10 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 11 | import org.apache.http.impl.client.CloseableHttpClient; 12 | import org.apache.http.impl.client.HttpClientBuilder; 13 | import org.apache.http.ssl.SSLContextBuilder; 14 | import org.springframework.boot.web.client.RestTemplateBuilder; 15 | import org.springframework.context.annotation.Bean; 16 | import org.springframework.context.annotation.Configuration; 17 | import org.springframework.http.client.ClientHttpRequestFactory; 18 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 19 | import org.springframework.web.client.RestTemplate; 20 | 21 | @Configuration 22 | public class RestConfig { 23 | 24 | @Bean 25 | public RestTemplate restTemplate(RestTemplateBuilder builder) { 26 | return builder.requestFactory(myRequestFactorySupplier()).build(); 27 | } 28 | 29 | @Bean 30 | public MyRequestFactorySupplier myRequestFactorySupplier() { 31 | return new MyRequestFactorySupplier(); 32 | } 33 | 34 | class MyRequestFactorySupplier implements Supplier { 35 | 36 | @Override 37 | public ClientHttpRequestFactory get() { 38 | HttpComponentsClientHttpRequestFactory requestFactory = null; 39 | try { 40 | char[] password = "changeit".toCharArray(); 41 | KeyStore ks = KeyStore.getInstance("JKS"); 42 | InputStream ksStream = this.getClass().getClassLoader().getResourceAsStream("javaclient.jks"); 43 | ks.load(ksStream, password); 44 | 45 | SSLContext sslContext = new SSLContextBuilder() 46 | .loadTrustMaterial(this.getClass().getClassLoader().getResource("certificate.jks"), password) 47 | .loadKeyMaterial(ks, password).build(); 48 | 49 | SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, 50 | NoopHostnameVerifier.INSTANCE); 51 | 52 | CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build(); 53 | 54 | requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); 55 | requestFactory.setBufferRequestBody(false); 56 | } catch (Exception e) { 57 | } 58 | 59 | return requestFactory; 60 | } 61 | 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload-client/src/main/java/com/roytuts/spring/rest/multiple/files/upload/client/FileUploadClient.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.multiple.files.upload.client; 2 | 3 | import java.io.File; 4 | import java.io.IOException; 5 | 6 | import org.springframework.core.io.FileSystemResource; 7 | import org.springframework.http.HttpEntity; 8 | import org.springframework.http.HttpHeaders; 9 | import org.springframework.http.HttpMethod; 10 | import org.springframework.http.MediaType; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.util.LinkedMultiValueMap; 13 | import org.springframework.web.client.RestTemplate; 14 | 15 | public class FileUploadClient { 16 | 17 | public static void main(String[] args) throws IOException { 18 | testMultipleFilesUpload(); 19 | testMultipleFilesUpload2(); 20 | } 21 | 22 | public static void testMultipleFilesUpload() throws IOException { 23 | LinkedMultiValueMap map = new LinkedMultiValueMap<>(); 24 | map.add("multipleFiles", 25 | new FileSystemResource(new File("C:\\additional.properties"))); 26 | map.add("multipleFiles", new FileSystemResource( 27 | new File("C:\\recommended pre-requisites.docx"))); 28 | 29 | HttpHeaders headers = new HttpHeaders(); 30 | headers.setContentType(MediaType.MULTIPART_FORM_DATA); 31 | 32 | HttpEntity> reqEntity = new HttpEntity>( 33 | map, headers); 34 | 35 | RestTemplate restTemplate = new RestTemplate(); 36 | 37 | ResponseEntity resE = restTemplate.exchange("http://localhost:8080/upload/multiple/files", 38 | HttpMethod.POST, reqEntity, String.class); 39 | 40 | System.out.println(resE); 41 | } 42 | 43 | public static void testMultipleFilesUpload2() throws IOException { 44 | LinkedMultiValueMap map = new LinkedMultiValueMap<>(); 45 | map.add("multipleFiles", 46 | new FileSystemResource(new File("C:\\additional.properties"))); 47 | map.add("multipleFiles", new FileSystemResource( 48 | new File("C:\\recommended pre-requisites.docx"))); 49 | 50 | HttpHeaders headers = new HttpHeaders(); 51 | headers.setContentType(MediaType.MULTIPART_FORM_DATA); 52 | 53 | HttpEntity> reqEntity = new HttpEntity>( 54 | map, headers); 55 | 56 | RestTemplate restTemplate = new RestTemplate(); 57 | 58 | ResponseEntity resE = restTemplate.exchange("http://localhost:8080/upload/multiple/files2", 59 | HttpMethod.POST, reqEntity, String.class); 60 | 61 | System.out.println(resE); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.service; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.roytuts.spring.hateoas.entity.Employee; 10 | import com.roytuts.spring.hateoas.repository.EmployeeRepository; 11 | import com.roytuts.spring.hateoas.vo.EmployeeVo; 12 | 13 | @Service 14 | public class EmployeeService { 15 | 16 | @Autowired 17 | private EmployeeRepository repository; 18 | 19 | public List getEmployeeList() { 20 | List employees = repository.findAll(); 21 | 22 | return employees.stream().map(e -> { 23 | EmployeeVo vo = new EmployeeVo(); 24 | vo.setEmpId(e.getEmpId()); 25 | vo.setName(e.getName()); 26 | vo.setEmail(e.getEmail()); 27 | return vo; 28 | }).collect(Collectors.toList()); 29 | } 30 | 31 | public EmployeeVo getEmployeeById(int id) { 32 | EmployeeVo employeeVo = new EmployeeVo(); 33 | 34 | Employee employee = repository.findById(id); 35 | 36 | employeeVo.setEmpId(employee.getEmpId()); 37 | employeeVo.setName(employee.getName()); 38 | employeeVo.setEmail(employee.getEmail()); 39 | 40 | return employeeVo; 41 | } 42 | 43 | public EmployeeVo getEmployeeByName(String name) { 44 | EmployeeVo employeeVo = new EmployeeVo(); 45 | 46 | Employee employee = repository.findByName(name); 47 | 48 | employeeVo.setEmpId(employee.getEmpId()); 49 | employeeVo.setName(employee.getName()); 50 | employeeVo.setEmail(employee.getEmail()); 51 | 52 | return employeeVo; 53 | } 54 | 55 | public EmployeeVo getEmployeeByEmail(String email) { 56 | EmployeeVo employeeVo = new EmployeeVo(); 57 | 58 | Employee employee = repository.findByEmail(email); 59 | 60 | employeeVo.setEmpId(employee.getEmpId()); 61 | employeeVo.setName(employee.getName()); 62 | employeeVo.setEmail(employee.getEmail()); 63 | 64 | return employeeVo; 65 | } 66 | 67 | public void saveEmployee(EmployeeVo employeeVo) { 68 | Employee employee = new Employee(); 69 | employee.setName(employeeVo.getName()); 70 | employee.setEmail(employeeVo.getEmail()); 71 | repository.save(employee); 72 | } 73 | 74 | public void updateEmployee(EmployeeVo employeeVo) { 75 | Employee employee = new Employee(); 76 | employee.setEmpId(employeeVo.getEmpId()); 77 | employee.setName(employeeVo.getName()); 78 | employee.setEmail(employeeVo.getEmail()); 79 | repository.save(employee); 80 | } 81 | 82 | public void deleteEmployeeById(int id) { 83 | repository.deleteById(id); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /spring-rest-retry-recover/spring-rest-retry/src/main/java/com/roytuts/springrestretry/service/EmployeeService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.springrestretry.service; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.core.ParameterizedTypeReference; 8 | import org.springframework.http.HttpMethod; 9 | import org.springframework.retry.RetryCallback; 10 | import org.springframework.retry.annotation.Backoff; 11 | import org.springframework.retry.annotation.Recover; 12 | import org.springframework.retry.annotation.Retryable; 13 | import org.springframework.retry.support.RetryTemplate; 14 | import org.springframework.stereotype.Service; 15 | import org.springframework.web.client.RestClientException; 16 | import org.springframework.web.client.RestTemplate; 17 | 18 | import com.roytuts.springrestretry.model.Employee; 19 | 20 | @Service 21 | public class EmployeeService { 22 | 23 | @Autowired 24 | private RestTemplate restTemplate; 25 | 26 | @Autowired 27 | private RetryTemplate retryTemplate; 28 | 29 | //Default retry policy 30 | //@Retryable(retryFor = Exception.class) 31 | 32 | //Simple retry policy 33 | //@Retryable(retryFor = Exception.class, maxAttempts = 4, backoff = @Backoff(delay = 1000)) 34 | 35 | //Exponential Backoff Policy 36 | //@Retryable(retryFor = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 10000)) 37 | 38 | //Random Backoff retry policy 39 | @Retryable(retryFor = Exception.class, maxAttempts = 4, backoff = @Backoff(delay = 1000, maxDelay = 10000, random = true)) 40 | public List getEmployeeList() { 41 | System.out.println("Try "); 42 | 43 | List employees = restTemplate.exchange("http://localhost:9000/employees", HttpMethod.GET, null, 44 | new ParameterizedTypeReference>() { 45 | }).getBody(); 46 | 47 | return employees; 48 | } 49 | 50 | public List getEmployeeList2() { 51 | retryTemplate.execute((RetryCallback, RestClientException>) context -> { 52 | System.out.println("Try "); 53 | 54 | List employees = restTemplate.exchange("http://localhost:9000/employees", HttpMethod.GET, null, 55 | new ParameterizedTypeReference>() { 56 | }).getBody(); 57 | 58 | return employees; 59 | }); 60 | 61 | return null; 62 | } 63 | 64 | @Recover 65 | public List employees(Exception e) { 66 | List employees = Arrays.asList(new Employee(1, "Smith John", 1000000.00), 67 | new Employee(2, "John Carle", 1200000.00)); 68 | 69 | return employees; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /spring-online-visitor-tracker/src/main/java/com/roytuts/spring/online/visitor/tracker/entity/Visitor.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.online.visitor.tracker.entity; 2 | 3 | import java.time.LocalDateTime; 4 | 5 | import javax.persistence.Entity; 6 | import javax.persistence.GeneratedValue; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "visitor") 12 | public class Visitor { 13 | 14 | @Id 15 | @GeneratedValue // (strategy = GenerationType.IDENTITY) 16 | private int id; 17 | private String user; 18 | private String ip; 19 | private String method; 20 | private String url; 21 | private String page; 22 | private String queryString; 23 | private String refererPage; 24 | private String userAgent; 25 | private LocalDateTime loggedTime; 26 | private boolean uniqueVisit; 27 | 28 | public int getId() { 29 | return id; 30 | } 31 | 32 | public void setId(int id) { 33 | this.id = id; 34 | } 35 | 36 | public String getUser() { 37 | return user; 38 | } 39 | 40 | public void setUser(String user) { 41 | this.user = user; 42 | } 43 | 44 | public String getIp() { 45 | return ip; 46 | } 47 | 48 | public void setIp(String ip) { 49 | this.ip = ip; 50 | } 51 | 52 | public String getMethod() { 53 | return method; 54 | } 55 | 56 | public void setMethod(String method) { 57 | this.method = method; 58 | } 59 | 60 | public String getUrl() { 61 | return url; 62 | } 63 | 64 | public void setUrl(String url) { 65 | this.url = url; 66 | } 67 | 68 | public String getPage() { 69 | return page; 70 | } 71 | 72 | public void setPage(String page) { 73 | this.page = page; 74 | } 75 | 76 | public String getQueryString() { 77 | return queryString; 78 | } 79 | 80 | public void setQueryString(String queryString) { 81 | this.queryString = queryString; 82 | } 83 | 84 | public String getRefererPage() { 85 | return refererPage; 86 | } 87 | 88 | public void setRefererPage(String refererPage) { 89 | this.refererPage = refererPage; 90 | } 91 | 92 | public String getUserAgent() { 93 | return userAgent; 94 | } 95 | 96 | public void setUserAgent(String userAgent) { 97 | this.userAgent = userAgent; 98 | } 99 | 100 | public LocalDateTime getLoggedTime() { 101 | return loggedTime; 102 | } 103 | 104 | public void setLoggedTime(LocalDateTime loggedTime) { 105 | this.loggedTime = loggedTime; 106 | } 107 | 108 | public boolean isUniqueVisit() { 109 | return uniqueVisit; 110 | } 111 | 112 | public void setUniqueVisit(boolean uniqueVisit) { 113 | this.uniqueVisit = uniqueVisit; 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /spring-boot-angular-crud-rest-api/src/main/java/com/roytuts/spring/boot/angular/crud/rest/api/service/WebsiteService.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.boot.angular.crud.rest.api.service; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | import com.roytuts.spring.boot.angular.crud.rest.api.converter.EntityVoConverter; 9 | import com.roytuts.spring.boot.angular.crud.rest.api.repository.WebsiteRepository; 10 | import com.roytuts.spring.boot.angular.crud.rest.api.vo.WebsiteVo; 11 | 12 | @Service 13 | public class WebsiteService { 14 | 15 | @Autowired 16 | private WebsiteRepository websiteRepository; 17 | 18 | public List getWebsites() { 19 | System.out.println("Total Websites: " + websiteRepository.findAll().size()); 20 | 21 | return EntityVoConverter.convertEntityToVoList(websiteRepository.findAll()); 22 | } 23 | 24 | public WebsiteVo getWebsite(final Integer id) { 25 | if (id == null || id == 0) { 26 | throw new RuntimeException("You must provide valid website id"); 27 | } 28 | 29 | WebsiteVo website = EntityVoConverter.convertEntityToVo(websiteRepository.findById(id).get()); 30 | 31 | if (website == null) { 32 | throw new RuntimeException("Website detail not found for the given id => " + id); 33 | } else { 34 | return website; 35 | } 36 | } 37 | 38 | public void deleteWebsite(final Integer id) { 39 | if (id == null || id == 0) { 40 | throw new RuntimeException("You must provide valid website id"); 41 | } 42 | 43 | try { 44 | websiteRepository.deleteById(id); 45 | } catch (Exception e) { 46 | throw new RuntimeException("Website detail not found for the given id => " + id); 47 | } 48 | } 49 | 50 | public void saveWebsite(final WebsiteVo website) { 51 | if (website == null || (website.getTitle() == null && website.getUrl() == null)) { 52 | throw new RuntimeException("You must provide Website details"); 53 | } 54 | 55 | try { 56 | websiteRepository.save(EntityVoConverter.convertVoToEntity(website)); 57 | } catch (Exception e) { 58 | throw new RuntimeException("Error occurred during website details saving"); 59 | } 60 | } 61 | 62 | public void updateWebsite(final WebsiteVo website) { 63 | if (website == null || ((website.getId() == null || website.getId() == 0) && website.getTitle() == null 64 | && website.getUrl() == null)) { 65 | throw new RuntimeException("You must provide Website details"); 66 | } 67 | 68 | try { 69 | websiteRepository.save(EntityVoConverter.convertVoToEntity(website)); 70 | } catch (Exception e) { 71 | throw new RuntimeException("Error occurred during website details updating"); 72 | } 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/rest/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.rest.controller; 2 | 3 | import java.util.List; 4 | import java.util.Objects; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.PathVariable; 11 | import org.springframework.web.bind.annotation.PostMapping; 12 | import org.springframework.web.bind.annotation.RequestBody; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | import com.roytuts.spring.web.exception.handling.entity.Employee; 16 | import com.roytuts.spring.web.exception.handling.repository.EmployeeRepository; 17 | 18 | @RestController 19 | public class EmployeeRestController { 20 | 21 | @Autowired 22 | private EmployeeRepository employeeRepository; 23 | 24 | @GetMapping("/employees") 25 | public ResponseEntity> getEmployees() { 26 | return new ResponseEntity>(employeeRepository.findAll(), HttpStatus.OK); 27 | } 28 | 29 | @GetMapping("/employee/id/{id}") 30 | public ResponseEntity getEmployeeById(@PathVariable Integer id) { 31 | Employee employee = employeeRepository.findById(id) 32 | .orElseThrow(() -> new Error("Employee not found for the given id " + id)); 33 | 34 | return new ResponseEntity(employee, HttpStatus.OK); 35 | } 36 | 37 | @GetMapping("/employee/name/{name}") 38 | public ResponseEntity getEmployeeByName(@PathVariable String name) { 39 | Employee employee = employeeRepository.findByName(name); 40 | 41 | if (employee == null) { 42 | throw new Error("Employee not found for the given name " + name); 43 | } 44 | 45 | return new ResponseEntity(employee, HttpStatus.OK); 46 | } 47 | 48 | @GetMapping("/employee/email/{email}") 49 | public ResponseEntity getEmployeeByEmail(@PathVariable String email) { 50 | Employee employee = employeeRepository.findByEmail(email); 51 | 52 | if (employee == null) { 53 | throw new Error("Employee not found for the given email " + email); 54 | } 55 | 56 | return new ResponseEntity(employee, HttpStatus.OK); 57 | } 58 | 59 | @PostMapping("/employee/save") 60 | public ResponseEntity saveNewEmployee(@RequestBody Employee employee) { 61 | if (Objects.isNull(employee.getName()) || Objects.isNull(employee.getEmail())) { 62 | throw new IllegalArgumentException("Invalid request"); 63 | } 64 | 65 | return new ResponseEntity(HttpStatus.CREATED); 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.roytuts 5 | spring-rest 6 | war 7 | 0.0.1-SNAPSHOT 8 | spring-rest-junit Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | UTF-8 13 | 1.8 14 | 4.12 15 | 2.10.0 16 | 3.1.0 17 | 4.3.8.RELEASE 18 | 19 | 20 | 21 | 22 | 23 | javax.servlet 24 | javax.servlet-api 25 | ${servlet.version} 26 | provided 27 | 28 | 29 | 30 | 31 | org.springframework 32 | spring-core 33 | ${spring.version} 34 | 35 | 36 | 37 | org.springframework 38 | spring-webmvc 39 | ${spring.version} 40 | 41 | 42 | 43 | 44 | javax.servlet 45 | jstl 46 | 1.2 47 | provided 48 | 49 | 50 | 51 | 52 | com.fasterxml.jackson.core 53 | jackson-core 54 | ${jackson.version} 55 | 56 | 57 | 58 | com.fasterxml.jackson.core 59 | jackson-databind 60 | ${jackson.version} 61 | 62 | 63 | 64 | com.fasterxml.jackson.core 65 | jackson-annotations 66 | ${jackson.version} 67 | 68 | 69 | 70 | 71 | spring-rest-junit 72 | 73 | 74 | org.apache.maven.plugins 75 | maven-compiler-plugin 76 | 2.3.2 77 | 78 | ${jdk.version} 79 | ${jdk.version} 80 | 81 | 82 | 83 | org.apache.maven.plugins 84 | maven-war-plugin 85 | 2.4 86 | 87 | src/main/webapp 88 | spring-rest-junit 89 | false 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /spring-rest-junit-mvc/src/main/java/roytuts/rest/SpringRestController.java: -------------------------------------------------------------------------------- 1 | package roytuts.rest; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpHeaders; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.util.UriComponentsBuilder; 13 | 14 | import roytuts.model.Product; 15 | import roytuts.sevice.ProductService; 16 | 17 | @RestController 18 | public class SpringRestController { 19 | @Autowired 20 | private ProductService productService; 21 | 22 | @RequestMapping(value = "/product/{id}", method = RequestMethod.GET) 23 | public ResponseEntity getProductByid(@PathVariable("id") int id) { 24 | Product product = productService.findProductById(id); 25 | if (product == null) { 26 | return new ResponseEntity(HttpStatus.NOT_FOUND); 27 | } 28 | return new ResponseEntity(product, HttpStatus.OK); 29 | } 30 | 31 | @RequestMapping(value = "/product", method = RequestMethod.POST) 32 | public ResponseEntity saveProduct(@RequestBody Product product, UriComponentsBuilder ucBuilder) { 33 | if (product == null || product.getName() == null || "".equals(product.getName())) { 34 | throw new RuntimeException("Product Name and Price are required fields"); 35 | } 36 | 37 | if (productService.isProductAvailable(product)) { 38 | System.out.println("A Product with name " + product.getName() + " already exist"); 39 | return new ResponseEntity(HttpStatus.CONFLICT); 40 | } 41 | 42 | productService.saveProduct(product); 43 | HttpHeaders headers = new HttpHeaders(); 44 | headers.setLocation(ucBuilder.path("/product/{id}").buildAndExpand(product.getId()).toUri()); 45 | return new ResponseEntity(headers, HttpStatus.CREATED); 46 | } 47 | 48 | @RequestMapping(value = "/product", method = RequestMethod.PUT) 49 | public ResponseEntity updateProduct(@RequestBody Product product) { 50 | if (product == null || product.getName() == null || "".equals(product.getName()) || product.getId() <= 0) { 51 | throw new RuntimeException("Product Name, Id, Price are required fields"); 52 | } 53 | 54 | Product currentProduct = productService.findProductById(product.getId()); 55 | if (currentProduct == null) { 56 | return new ResponseEntity(HttpStatus.NOT_FOUND); 57 | } 58 | 59 | productService.updateProduct(product); 60 | return new ResponseEntity(HttpStatus.OK); 61 | } 62 | 63 | @RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE) 64 | public ResponseEntity deleteProductByid(@PathVariable("id") int id) { 65 | Product currentProduct = productService.findProductById(id); 66 | if (currentProduct == null) { 67 | return new ResponseEntity(HttpStatus.NOT_FOUND); 68 | } 69 | 70 | productService.deleteProductById(id); 71 | return new ResponseEntity(HttpStatus.OK); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/main/java/roytuts/rest/SpringRestController.java: -------------------------------------------------------------------------------- 1 | package roytuts.rest; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.http.HttpHeaders; 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.PathVariable; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RestController; 12 | import org.springframework.web.util.UriComponentsBuilder; 13 | 14 | import roytuts.model.Product; 15 | import roytuts.sevice.ProductService; 16 | 17 | @RestController 18 | public class SpringRestController { 19 | @Autowired 20 | private ProductService productService; 21 | 22 | @RequestMapping(value = "/product/{id}", method = RequestMethod.GET) 23 | public ResponseEntity getProductByid(@PathVariable("id") int id) { 24 | Product product = productService.findProductById(id); 25 | if (product == null) { 26 | return new ResponseEntity(HttpStatus.NOT_FOUND); 27 | } 28 | return new ResponseEntity(product, HttpStatus.OK); 29 | } 30 | 31 | @RequestMapping(value = "/product", method = RequestMethod.POST) 32 | public ResponseEntity saveProduct(@RequestBody Product product, UriComponentsBuilder ucBuilder) { 33 | if (product == null || product.getName() == null || "".equals(product.getName())) { 34 | throw new RuntimeException("Product Name and Price are required fields"); 35 | } 36 | 37 | if (productService.isProductAvailable(product)) { 38 | System.out.println("A Product with name " + product.getName() + " already exist"); 39 | return new ResponseEntity(HttpStatus.CONFLICT); 40 | } 41 | 42 | productService.saveProduct(product); 43 | HttpHeaders headers = new HttpHeaders(); 44 | headers.setLocation(ucBuilder.path("/product/{id}").buildAndExpand(product.getId()).toUri()); 45 | return new ResponseEntity(headers, HttpStatus.CREATED); 46 | } 47 | 48 | @RequestMapping(value = "/product", method = RequestMethod.PUT) 49 | public ResponseEntity updateProduct(@RequestBody Product product) { 50 | if (product == null || product.getName() == null || "".equals(product.getName()) || product.getId() <= 0) { 51 | throw new RuntimeException("Product Name, Id, Price are required fields"); 52 | } 53 | 54 | Product currentProduct = productService.findProductById(product.getId()); 55 | if (currentProduct == null) { 56 | return new ResponseEntity(HttpStatus.NOT_FOUND); 57 | } 58 | 59 | productService.updateProduct(product); 60 | return new ResponseEntity(HttpStatus.OK); 61 | } 62 | 63 | @RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE) 64 | public ResponseEntity deleteProductByid(@PathVariable("id") int id) { 65 | Product currentProduct = productService.findProductById(id); 66 | if (currentProduct == null) { 67 | return new ResponseEntity(HttpStatus.NOT_FOUND); 68 | } 69 | 70 | productService.deleteProductById(id); 71 | return new ResponseEntity(HttpStatus.OK); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /spring-rest-multiple-files-upload/src/main/java/com/roytuts/spring/rest/multiple/files/upload/rest/controller/MultipleFileUploadRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.multiple.files.upload.rest.controller; 2 | 3 | import java.io.InputStream; 4 | import java.util.List; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.ResponseEntity; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.bind.annotation.RestController; 13 | import org.springframework.web.multipart.MultipartFile; 14 | 15 | import jakarta.servlet.annotation.MultipartConfig; 16 | 17 | @RestController 18 | /*@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2 MB 19 | maxFileSize = 1024 * 1024 * 10, // 10 MB 20 | maxRequestSize = 1024 * 1024 * 10)*/ 21 | public class MultipleFileUploadRestController { 22 | 23 | private static final Logger logger = LoggerFactory.getLogger(MultipleFileUploadRestController.class.getName()); 24 | 25 | @PostMapping("/upload/multiple/files") 26 | public ResponseEntity uploadMultipleFiles(@RequestParam("multipleFiles") MultipartFile[] files) 27 | throws Exception { 28 | 29 | if (files == null || files.length == 0) { 30 | throw new RuntimeException("You must select at least one file for uploading"); 31 | } 32 | 33 | StringBuilder sb = new StringBuilder(files.length); 34 | 35 | for (int i = 0; i < files.length; i++) { 36 | InputStream inputStream = files[i].getInputStream(); 37 | String originalName = files[i].getOriginalFilename(); 38 | String name = files[i].getName(); 39 | String contentType = files[i].getContentType(); 40 | long size = files[i].getSize(); 41 | 42 | sb.append("File Name: " + originalName + "\n"); 43 | 44 | logger.info("InputStream: " + inputStream); 45 | logger.info("OriginalName: " + originalName); 46 | logger.info("Name: " + name); 47 | logger.info("ContentType: " + contentType); 48 | logger.info("Size: " + size); 49 | } 50 | 51 | // Do processing with uploaded file data in Service layer 52 | return new ResponseEntity(sb.toString(), HttpStatus.OK); 53 | } 54 | 55 | @PostMapping("/upload/multiple/files2") 56 | public ResponseEntity uploadMultipleFiles(@RequestParam("multipleFiles") List files) 57 | throws Exception { 58 | 59 | if (files == null || files.isEmpty()) { 60 | throw new RuntimeException("You must select at least one file for uploading"); 61 | } 62 | 63 | StringBuilder sb = new StringBuilder(files.size()); 64 | 65 | for (int i = 0; i < files.size(); i++) { 66 | InputStream inputStream = files.get(i).getInputStream(); 67 | String originalName = files.get(i).getOriginalFilename(); 68 | String name = files.get(i).getName(); 69 | String contentType = files.get(i).getContentType(); 70 | 71 | sb.append("File Name: " + originalName + "\n"); 72 | 73 | logger.info("InputStream: " + inputStream); 74 | logger.info("OriginalName: " + originalName); 75 | logger.info("Name: " + name); 76 | logger.info("ContentType: " + contentType); 77 | logger.info("Size: " + files.size()); 78 | } 79 | 80 | // Do processing with uploaded file data in Service layer 81 | return new ResponseEntity(sb.toString(), HttpStatus.OK); 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /spring-web-exception-handling/src/main/java/com/roytuts/spring/web/exception/handling/handler/AppExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.web.exception.handling.handler; 2 | 3 | import java.util.Objects; 4 | 5 | import org.springframework.http.HttpStatus; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.ControllerAdvice; 8 | import org.springframework.web.bind.annotation.ExceptionHandler; 9 | import org.springframework.web.multipart.MultipartException; 10 | 11 | import com.roytuts.spring.web.exception.handling.constants.AppConstants; 12 | import com.roytuts.spring.web.exception.handling.exception.AppException; 13 | import com.roytuts.spring.web.exception.handling.exception.DbException; 14 | import com.roytuts.spring.web.exception.handling.wrapper.ExceptionWrapper; 15 | 16 | @ControllerAdvice 17 | public class AppExceptionHandler { 18 | 19 | @ExceptionHandler(AppException.class) 20 | public ResponseEntity appException(AppException exception) { 21 | if (!Objects.isNull(exception.getCode()) && AppConstants.SUCCESS.equalsIgnoreCase(exception.getCode())) { 22 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.OK.toString(), exception.getMessage()); 23 | 24 | return new ResponseEntity(exceptionWrapper, HttpStatus.OK); 25 | } 26 | 27 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.INTERNAL_SERVER_ERROR.toString(), 28 | exception.getMessage()); 29 | 30 | return new ResponseEntity(exceptionWrapper, HttpStatus.INTERNAL_SERVER_ERROR); 31 | } 32 | 33 | @ExceptionHandler(DbException.class) 34 | public ResponseEntity dbException(DbException exception) { 35 | if (!Objects.isNull(exception.getMessage()) 36 | && AppConstants.INCORRECT_RESULT_SIZE.equalsIgnoreCase(exception.getMessage())) { 37 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.OK.toString(), exception.getMessage()); 38 | 39 | return new ResponseEntity(exceptionWrapper, HttpStatus.OK); 40 | } 41 | 42 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.INTERNAL_SERVER_ERROR.toString(), 43 | exception.getMessage()); 44 | 45 | return new ResponseEntity(exceptionWrapper, HttpStatus.INTERNAL_SERVER_ERROR); 46 | } 47 | 48 | @ExceptionHandler(IllegalArgumentException.class) 49 | public ResponseEntity validationException(IllegalArgumentException exception) { 50 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.BAD_REQUEST.toString(), 51 | exception.getMessage()); 52 | 53 | return new ResponseEntity(exceptionWrapper, HttpStatus.BAD_REQUEST); 54 | } 55 | 56 | @ExceptionHandler(MultipartException.class) 57 | public ResponseEntity multipartException(MultipartException exception) { 58 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.INTERNAL_SERVER_ERROR.toString(), 59 | exception.getMessage()); 60 | 61 | return new ResponseEntity(exceptionWrapper, HttpStatus.INTERNAL_SERVER_ERROR); 62 | } 63 | 64 | @ExceptionHandler(Error.class) 65 | public ResponseEntity otherError(Error error) { 66 | ExceptionWrapper exceptionWrapper = new ExceptionWrapper(HttpStatus.NOT_FOUND.toString(), error.getMessage()); 67 | 68 | return new ResponseEntity(exceptionWrapper, HttpStatus.NOT_FOUND); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /spring-rest-large-file-download/src/main/java/com/roytuts/spring/rest/large/file/download/rest/controller/FileDownloadRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.rest.large.file.download.rest.controller; 2 | 3 | import java.io.IOException; 4 | import java.net.URI; 5 | import java.nio.file.Files; 6 | import java.nio.file.Path; 7 | import java.nio.file.Paths; 8 | import java.nio.file.StandardOpenOption; 9 | import java.util.Arrays; 10 | 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.core.io.buffer.DataBuffer; 13 | import org.springframework.core.io.buffer.DataBufferUtils; 14 | import org.springframework.http.HttpMethod; 15 | import org.springframework.http.HttpStatus; 16 | import org.springframework.http.MediaType; 17 | import org.springframework.http.ResponseEntity; 18 | import org.springframework.web.bind.annotation.PostMapping; 19 | import org.springframework.web.bind.annotation.RequestBody; 20 | import org.springframework.web.bind.annotation.RestController; 21 | import org.springframework.web.client.RequestCallback; 22 | import org.springframework.web.client.ResponseExtractor; 23 | import org.springframework.web.client.RestTemplate; 24 | import org.springframework.web.reactive.function.client.WebClient; 25 | 26 | import com.roytuts.spring.rest.large.file.download.dto.Input; 27 | 28 | import reactor.core.publisher.Flux; 29 | 30 | @RestController 31 | public class FileDownloadRestController { 32 | 33 | @Autowired 34 | private RestTemplate restTemplate; 35 | 36 | @PostMapping("/download/large/file1") 37 | public ResponseEntity downloadFileUsingRestTemplate(@RequestBody Input input) { 38 | // Optional Accept header 39 | RequestCallback requestCallback = request -> request.getHeaders() 40 | .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); 41 | 42 | // Streams the response instead of loading it all in memory 43 | ResponseExtractor responseExtractor = response -> { 44 | String filename = input.getDownloadUrl().substring(input.getDownloadUrl().lastIndexOf("/") + 1); 45 | 46 | Path path = Paths 47 | .get(input.getDownloadPath() == null ? "/" + filename : input.getDownloadPath() + "/" + filename); 48 | 49 | Files.copy(response.getBody(), path); 50 | 51 | return null; 52 | }; 53 | 54 | // URL - https://speed.hetzner.de/100MB.bin, https://speed.hetzner.de/10GB.bin 55 | // URL - https://link.testfile.org/250MB, https://bit.ly/1GB-testfile 56 | restTemplate.execute(URI.create(input.getDownloadUrl()), HttpMethod.GET, requestCallback, responseExtractor); 57 | 58 | return new ResponseEntity("File Downloaded Successfully", HttpStatus.OK); 59 | } 60 | 61 | @PostMapping("/download/large/file2") 62 | public ResponseEntity downloadFileUsingWebClient(@RequestBody Input input) throws IOException { 63 | String filename = input.getDownloadUrl().substring(input.getDownloadUrl().lastIndexOf("/") + 1); 64 | Path path = Paths 65 | .get(input.getDownloadPath() == null ? "/" + filename : input.getDownloadPath() + "/" + filename); 66 | 67 | WebClient webClient = WebClient.builder().baseUrl(input.getDownloadUrl()).build(); 68 | 69 | // Get file data 70 | Flux dataBufferFlux = webClient.get().accept(MediaType.APPLICATION_OCTET_STREAM) 71 | .retrieve().bodyToFlux(DataBuffer.class); 72 | 73 | // Streams the dataBufferFlux from response instead of loading it all in memory 74 | DataBufferUtils.write(dataBufferFlux, path, StandardOpenOption.CREATE).share().block(); 75 | 76 | return new ResponseEntity("File Downloaded Successfully", HttpStatus.OK); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /spring-rest-junit-complete/src/test/java/jeejava/rest/SpringRestControllerTest.java: -------------------------------------------------------------------------------- 1 | package roytuts.rest; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.mockito.InjectMocks; 8 | import org.mockito.Mock; 9 | import org.mockito.Mockito; 10 | import org.mockito.Spy; 11 | import org.mockito.junit.MockitoJUnitRunner; 12 | import org.springframework.http.MediaType; 13 | import org.springframework.test.web.servlet.MockMvc; 14 | import org.springframework.test.web.servlet.MvcResult; 15 | import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 16 | import org.springframework.test.web.servlet.result.MockMvcResultMatchers; 17 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 18 | 19 | import com.fasterxml.jackson.databind.ObjectMapper; 20 | 21 | import roytuts.model.Product; 22 | import roytuts.sevice.ProductService; 23 | 24 | @RunWith(MockitoJUnitRunner.class) 25 | public class SpringRestControllerTest { 26 | @Mock 27 | private ProductService service; 28 | 29 | private MockMvc mockMvc; 30 | 31 | @Spy 32 | @InjectMocks 33 | private SpringRestController controller = new SpringRestController(); 34 | 35 | @Before 36 | public void init() { 37 | mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 38 | } 39 | 40 | @Test 41 | public void testGetProductById() throws Exception { 42 | Product product = new Product(); 43 | product.setId(1); 44 | product.setName("Product name"); 45 | product.setPrice(21540.00); 46 | Mockito.when(service.findProductById(Mockito.anyInt())).thenReturn(product); 47 | mockMvc.perform(MockMvcRequestBuilders.get("/product/1")).andExpect(MockMvcResultMatchers.status().is(200)) 48 | .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(1)); 49 | } 50 | 51 | @Test 52 | public void testSaveProduct() throws Exception { 53 | Product product = new Product(); 54 | product.setId(1); 55 | product.setName("Product name"); 56 | product.setPrice(21540.00); 57 | ObjectMapper mapper = new ObjectMapper(); 58 | String jsonString = mapper.writeValueAsString(product); 59 | Mockito.when(service.isProductAvailable(Mockito.any(Product.class))).thenReturn(false); 60 | Mockito.doNothing().when(service).saveProduct(Mockito.any(Product.class)); 61 | MvcResult result = mockMvc 62 | .perform(MockMvcRequestBuilders.post("/product").content(jsonString) 63 | .contentType(MediaType.APPLICATION_JSON)) 64 | .andExpect(MockMvcResultMatchers.status().is(201)).andReturn(); 65 | Assert.assertEquals(201, result.getResponse().getStatus()); 66 | } 67 | 68 | @Test 69 | public void testUpdateProduct() throws Exception { 70 | Product product = new Product(); 71 | product.setId(1); 72 | product.setName("Product name"); 73 | product.setPrice(21540.00); 74 | ObjectMapper mapper = new ObjectMapper(); 75 | String jsonString = mapper.writeValueAsString(product); 76 | Mockito.when(service.findProductById(Mockito.anyInt())).thenReturn(product); 77 | Mockito.doNothing().when(service).updateProduct(Mockito.any(Product.class)); 78 | mockMvc.perform( 79 | MockMvcRequestBuilders.put("/product").content(jsonString).contentType(MediaType.APPLICATION_JSON)) 80 | .andExpect(MockMvcResultMatchers.status().is(200)); 81 | } 82 | 83 | @Test 84 | public void testDeleteProductByid() throws Exception { 85 | Product product = new Product(); 86 | product.setId(1); 87 | product.setName("Product name"); 88 | product.setPrice(21540.00); 89 | Mockito.when(service.findProductById(Mockito.anyInt())).thenReturn(product); 90 | Mockito.doNothing().when(service).deleteProductById(Mockito.anyInt()); 91 | mockMvc.perform(MockMvcRequestBuilders.delete("/product/1")).andExpect(MockMvcResultMatchers.status().is(200)); 92 | } 93 | } -------------------------------------------------------------------------------- /spring-hateoas/src/main/java/com/roytuts/spring/hateoas/rest/controller/EmployeeRestController.java: -------------------------------------------------------------------------------- 1 | package com.roytuts.spring.hateoas.rest.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.web.bind.annotation.DeleteMapping; 10 | import org.springframework.web.bind.annotation.GetMapping; 11 | import org.springframework.web.bind.annotation.PathVariable; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.PutMapping; 14 | import org.springframework.web.bind.annotation.RequestBody; 15 | import org.springframework.web.bind.annotation.RestController; 16 | 17 | import com.roytuts.spring.hateoas.service.EmployeeService; 18 | import com.roytuts.spring.hateoas.vo.EmployeeVo; 19 | 20 | @RestController 21 | public class EmployeeRestController { 22 | 23 | @Autowired 24 | private EmployeeService service; 25 | 26 | @GetMapping("/employees") 27 | public ResponseEntity> getEmployeeList() { 28 | List list = service.getEmployeeList(); 29 | 30 | list.forEach(ev -> { 31 | ev.add(WebMvcLinkBuilder 32 | .linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeById(ev.getEmpId())) 33 | .withRel("employee-by-id")); 34 | ev.add(WebMvcLinkBuilder 35 | .linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeByName(ev.getName())) 36 | .withRel("employee-by-name")); 37 | ev.add(WebMvcLinkBuilder 38 | .linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeByEmail(ev.getEmail())) 39 | .withRel("employee-by-email")); 40 | }); 41 | 42 | return new ResponseEntity>(list, HttpStatus.OK); 43 | } 44 | 45 | @GetMapping("/employee/id/{id}") 46 | public ResponseEntity getEmployeeById(@PathVariable int id) { 47 | EmployeeVo emp = service.getEmployeeById(id); 48 | 49 | emp.add(WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeList()) 50 | .withRel("employee-list")); 51 | emp.add(WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeById(id)) 52 | .withSelfRel()); 53 | emp.add(WebMvcLinkBuilder 54 | .linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeByName(emp.getName())) 55 | .withRel("employee-by-name")); 56 | emp.add(WebMvcLinkBuilder 57 | .linkTo(WebMvcLinkBuilder.methodOn(EmployeeRestController.class).getEmployeeByEmail(emp.getEmail())) 58 | .withRel("employee-by-email")); 59 | 60 | return new ResponseEntity(emp, HttpStatus.OK); 61 | } 62 | 63 | @GetMapping("/employee/name/{name}") 64 | public ResponseEntity getEmployeeByName(@PathVariable String name) { 65 | EmployeeVo emp = service.getEmployeeByName(name); 66 | 67 | // ...you can add relevant links 68 | 69 | return new ResponseEntity(emp, HttpStatus.OK); 70 | } 71 | 72 | @GetMapping("/employee/email/{email}") 73 | public ResponseEntity getEmployeeByEmail(@PathVariable String email) { 74 | EmployeeVo emp = service.getEmployeeByEmail(email); 75 | 76 | // ...you can add relevant links 77 | 78 | return new ResponseEntity(emp, HttpStatus.OK); 79 | } 80 | 81 | @PostMapping("/employee") 82 | public ResponseEntity saveEmployee(@RequestBody EmployeeVo employeeVo) { 83 | service.saveEmployee(employeeVo); 84 | 85 | return new ResponseEntity(HttpStatus.OK); 86 | } 87 | 88 | @PutMapping("/employee") 89 | public ResponseEntity updateEmployee(@RequestBody EmployeeVo employeeVo) { 90 | service.updateEmployee(employeeVo); 91 | 92 | return new ResponseEntity(HttpStatus.OK); 93 | } 94 | 95 | @DeleteMapping("/employee/{id}") 96 | public ResponseEntity deleteEmployee(@PathVariable int id) { 97 | service.deleteEmployeeById(id); 98 | 99 | return new ResponseEntity(HttpStatus.OK); 100 | } 101 | 102 | } 103 | --------------------------------------------------------------------------------