├── Angular SpringBoot Pagination - Backend RestAPs ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── pagingansorting │ │ │ ├── SpringBootPagingAndSortingApplication.java │ │ │ ├── model │ │ │ ├── Customer.java │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── restapis │ │ │ └── RestController.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── table.js │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── pagingansorting │ └── SpringBootPagingAndSortingApplicationTests.java ├── Angular SpringBoot RestAPIs CRUD Application - Backend Development ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── crudapp │ │ │ ├── SpringBootCrudApplication.java │ │ │ ├── controller │ │ │ └── RestAPIController.java │ │ │ ├── model │ │ │ ├── Customer.java │ │ │ └── Message.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── service │ │ │ └── CustomerServices.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── customers.html │ │ ├── index.html │ │ └── js │ │ ├── delete_customer.js │ │ ├── get_customer.js │ │ ├── post_customer.js │ │ └── update_customer.js │ └── test │ └── java │ └── com │ └── loizenai │ └── crudapp │ └── SpringBootCrudApplicationTests.java ├── Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── mongodb │ │ │ ├── SpringBootMongoDbRestApIsApplication.java │ │ │ ├── exception │ │ │ └── CustomException.java │ │ │ ├── message │ │ │ └── ResponseMsg.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── rest │ │ │ └── WebController.java │ │ │ └── service │ │ │ └── CustomerService.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── mongodb │ └── SpringBootMongoDbRestApIsApplicationTests.java ├── Angular10 SpringBoot Jwt Authentication - Backend Development ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── jwtauthentication │ │ │ ├── SpringBootJwtAuthenticationExamplesApplication.java │ │ │ ├── controller │ │ │ ├── AuthRestAPIs.java │ │ │ └── TestRestAPIs.java │ │ │ ├── message │ │ │ ├── request │ │ │ │ ├── LoginForm.java │ │ │ │ └── SignUpForm.java │ │ │ └── response │ │ │ │ ├── JwtResponse.java │ │ │ │ └── ResponseMessage.java │ │ │ ├── model │ │ │ ├── Role.java │ │ │ ├── RoleName.java │ │ │ └── User.java │ │ │ ├── repository │ │ │ ├── RoleRepository.java │ │ │ └── UserRepository.java │ │ │ └── security │ │ │ ├── WebSecurityConfig.java │ │ │ ├── jwt │ │ │ ├── JwtAuthEntryPoint.java │ │ │ ├── JwtAuthTokenFilter.java │ │ │ └── JwtProvider.java │ │ │ └── services │ │ │ ├── UserDetailsServiceImpl.java │ │ │ └── UserPrinciple.java │ └── resources │ │ ├── application.properties │ │ └── roles.sql │ └── test │ └── java │ └── com │ └── loizenai │ └── jwtauthentication │ └── SpringBootJwtAuthenticationExamplesApplicationTests.java ├── How-to-Integrate-SpringBoot-with-Reactjs-Example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── reactjs │ │ │ ├── SpringBootRestApplication.java │ │ │ ├── controller │ │ │ └── CustomerController.java │ │ │ └── model │ │ │ └── Customer.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── asset-manifest.json │ │ ├── favicon.ico │ │ ├── index.html │ │ ├── logo192.png │ │ ├── logo512.png │ │ ├── manifest.json │ │ ├── precache-manifest.909894d6e5a5ec6adb29c03fbc62f5a5.js │ │ ├── robots.txt │ │ ├── service-worker.js │ │ └── static │ │ ├── css │ │ ├── main.6ea727e0.chunk.css │ │ └── main.6ea727e0.chunk.css.map │ │ └── js │ │ ├── 2.cb01f4d6.chunk.js │ │ ├── 2.cb01f4d6.chunk.js.LICENSE.txt │ │ ├── 2.cb01f4d6.chunk.js.map │ │ ├── main.613831a1.chunk.js │ │ ├── main.613831a1.chunk.js.map │ │ ├── runtime-main.92ee656d.js │ │ └── runtime-main.92ee656d.js.map │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── reactjs │ └── SpringBootRestApplicationTests.java ├── README.md ├── SpringBoot Download Upload Import Excel Files to MySQL ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── excelfile │ │ │ ├── DownloadUploadExcelFileApplication.java │ │ │ ├── controller │ │ │ ├── DownloadFileRestAPIs.java │ │ │ └── UploadFileRestAPIs.java │ │ │ ├── errorhandler │ │ │ ├── Error.java │ │ │ └── RestExceptionHandler.java │ │ │ ├── message │ │ │ └── Message.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── services │ │ │ └── ExcelFileServices.java │ │ │ └── utils │ │ │ └── ExcelUtils.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── excelfiles.js │ └── test │ └── java │ └── com │ └── loizenai │ └── excelfile │ └── DownloadUploadExcelFileApplicationTests.java ├── SpringBoot Upload Download MultipartFile - Thymeleaf ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── uploaddownloadfiles │ │ │ ├── SpringBootUploadDownloadMultipartFileApplication.java │ │ │ ├── controller │ │ │ ├── DownloadController.java │ │ │ ├── UploadMultipleFileController.java │ │ │ └── UploadSingleFileController.java │ │ │ ├── info │ │ │ └── FileInfo.java │ │ │ └── service │ │ │ ├── FileStorage.java │ │ │ └── FileStorageImpl.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── files.html │ │ ├── uploadmultiplefile.html │ │ └── uploadsinglefile.html │ └── test │ └── java │ └── com │ └── loizenai │ └── uploaddownloadfiles │ └── SpringBootUploadDownloadMultipartFileApplicationTests.java ├── SpringBoot-CRUD-RestAPIs-App ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── crudapp │ │ │ ├── SpringBootCrudApplication.java │ │ │ ├── controller │ │ │ └── RestAPIController.java │ │ │ ├── model │ │ │ ├── Customer.java │ │ │ └── Message.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── service │ │ │ └── CustomerServices.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── customers.html │ │ ├── index.html │ │ └── js │ │ ├── delete_customer.js │ │ ├── get_customer.js │ │ ├── post_customer.js │ │ └── update_customer.js │ └── test │ └── java │ └── com │ └── loizenai │ └── crudapp │ └── SpringBootCrudApplicationTests.java ├── SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── updownloadcsv │ │ │ ├── SpringBootUpDownloadCsvMySqlApplication.java │ │ │ ├── controller │ │ │ ├── DownloadCsvRestApi.java │ │ │ └── UploadCsvRestApi.java │ │ │ ├── errorhandler │ │ │ └── RestExceptionHandler.java │ │ │ ├── message │ │ │ ├── Error.java │ │ │ ├── FileInfo.java │ │ │ ├── Message.java │ │ │ └── Response.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── service │ │ │ └── CsvFileServices.java │ │ │ └── utils │ │ │ ├── ApacheCommonsCsvUtil.java │ │ │ └── OpenCsvUtil.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── ajaxupdownloadcsv.js │ └── test │ └── java │ └── com │ └── loizenai │ └── updownloadcsv │ └── SpringBootUpDownloadCsvMySqlApplicationTests.java ├── SpringBoot-Jwt-RestAPIs-Authentication-Examples ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── jwtauthentication │ │ │ ├── SpringBootJwtAuthenticationExamplesApplication.java │ │ │ ├── controller │ │ │ ├── AuthRestAPIs.java │ │ │ └── TestRestAPIs.java │ │ │ ├── message │ │ │ ├── request │ │ │ │ ├── LoginForm.java │ │ │ │ └── SignUpForm.java │ │ │ └── response │ │ │ │ └── JwtResponse.java │ │ │ ├── model │ │ │ ├── Role.java │ │ │ ├── RoleName.java │ │ │ └── User.java │ │ │ ├── repository │ │ │ ├── RoleRepository.java │ │ │ └── UserRepository.java │ │ │ └── security │ │ │ ├── WebSecurityConfig.java │ │ │ ├── jwt │ │ │ ├── JwtAuthEntryPoint.java │ │ │ ├── JwtAuthTokenFilter.java │ │ │ └── JwtProvider.java │ │ │ └── services │ │ │ ├── UserDetailsServiceImpl.java │ │ │ └── UserPrinciple.java │ └── resources │ │ ├── application.properties │ │ └── roles.sql │ └── test │ └── java │ └── com │ └── loizenai │ └── jwtauthentication │ └── SpringBootJwtAuthenticationExamplesApplicationTests.java ├── SpringBoot-MongoDB-RestAPIs ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── mongodb │ │ │ ├── SpringBootMongoDbRestApIsApplication.java │ │ │ ├── exception │ │ │ └── CustomException.java │ │ │ ├── message │ │ │ └── ResponseMsg.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── rest │ │ │ └── WebController.java │ │ │ └── service │ │ │ └── CustomerService.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── mongodb │ └── SpringBootMongoDbRestApIsApplicationTests.java ├── SpringBoot-Pagination-MongoDB ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── pagination │ │ │ └── mongodb │ │ │ ├── SpringBootPaginationMongoDbApplication.java │ │ │ ├── document │ │ │ └── Customer.java │ │ │ ├── message │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── restapis │ │ │ └── RestController.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── pagination │ └── mongodb │ └── SpringBootPaginationMongoDbApplicationTests.java ├── SpringBoot-Reactjs-MongoDB-CRUD-Example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── reactjs │ │ │ ├── Initializer.java │ │ │ ├── SpringBootCrudReactApplication.java │ │ │ ├── controller │ │ │ └── CustomerController.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ └── repository │ │ │ └── CustomerRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── reactjs │ └── SpringBootCrudReactApplicationTests.java ├── SpringBoot-Reactjs-MySQL-CRUD-Example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── reactjs │ │ │ ├── Initializer.java │ │ │ ├── SpringBootCrudReactApplication.java │ │ │ ├── controller │ │ │ └── CustomerController.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ └── repository │ │ │ └── CustomerRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── reactjs │ └── SpringBootCrudReactApplicationTests.java ├── SpringBoot-Reactjs-PostgreSQL-CRUD-Example ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── reactjs │ │ │ ├── Initializer.java │ │ │ ├── SpringBootCrudReactApplication.java │ │ │ ├── controller │ │ │ └── CustomerController.java │ │ │ ├── model │ │ │ └── Customer.java │ │ │ └── repository │ │ │ └── CustomerRepository.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── reactjs │ └── SpringBootCrudReactApplicationTests.java ├── SpringBoot-Upload-Parse-Excel-MongoDB ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── mongodb │ │ │ └── excel │ │ │ ├── SpringBootUploadExcelMongoDbApplication.java │ │ │ ├── controller │ │ │ ├── DownloadFileRestAPIs.java │ │ │ └── UploadExcelFileRestAPIs.java │ │ │ ├── document │ │ │ └── Customer.java │ │ │ ├── errorhandler │ │ │ ├── Error.java │ │ │ └── RestExceptionHandler.java │ │ │ ├── message │ │ │ ├── FileInfo.java │ │ │ ├── Message.java │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── services │ │ │ └── ExcelFileServices.java │ │ │ └── utils │ │ │ └── ExcelUtils.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── excelfiles.js │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── mongodb │ └── excel │ └── SpringBootUploadExcelMongoDbApplicationTests.java ├── SpringBootPaginationFilteringSortingRestAPIs ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── pagingansorting │ │ │ ├── SpringBootPagingAndSortingApplication.java │ │ │ ├── model │ │ │ ├── Customer.java │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── restapis │ │ │ └── RestController.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── table.js │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── pagingansorting │ └── SpringBootPagingAndSortingApplicationTests.java ├── SpringBootPagingAndSorting ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── pagingansorting │ │ │ ├── SpringBootPagingAndSortingApplication.java │ │ │ ├── model │ │ │ ├── Customer.java │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ └── restapis │ │ │ └── RestController.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── table.js │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── pagingansorting │ └── SpringBootPagingAndSortingApplicationTests.java ├── SpringBootUploadCsvMongoDB ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── springboot │ │ │ └── uploadcsv │ │ │ └── mongodb │ │ │ ├── SpringBootUploadCsvMongoDbApplication.java │ │ │ ├── controller │ │ │ ├── DownloadCsvRestApi.java │ │ │ └── UploadCsvRestApi.java │ │ │ ├── document │ │ │ └── Customer.java │ │ │ ├── errorhandler │ │ │ └── RestExceptionHandler.java │ │ │ ├── message │ │ │ ├── Error.java │ │ │ ├── FileInfo.java │ │ │ ├── Message.java │ │ │ └── Response.java │ │ │ ├── repository │ │ │ └── CustomerRepository.java │ │ │ ├── service │ │ │ └── CsvFileServices.java │ │ │ └── util │ │ │ ├── ApacheCommonsCsvUtil.java │ │ │ └── OpenCsvUtil.java │ └── resources │ │ ├── application.properties │ │ └── static │ │ ├── index.html │ │ └── js │ │ └── ajaxupdownloadcsv.js │ └── test │ └── java │ └── com │ └── loizenai │ └── springboot │ └── uploadcsv │ └── mongodb │ └── SpringBootUploadCsvMongoDbApplicationTests.java ├── SpringBootUploadDownloadMultipartFile ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── loizenai │ │ │ └── uploaddownloadfiles │ │ │ ├── SpringBootUploadDownloadMultipartFileApplication.java │ │ │ ├── controller │ │ │ ├── DownloadController.java │ │ │ ├── UploadMultipleFileController.java │ │ │ └── UploadSingleFileController.java │ │ │ ├── info │ │ │ └── FileInfo.java │ │ │ └── service │ │ │ ├── FileStorage.java │ │ │ └── FileStorageImpl.java │ └── resources │ │ ├── application.properties │ │ └── templates │ │ ├── files.html │ │ ├── uploadmultiplefile.html │ │ └── uploadsinglefile.html │ └── test │ └── java │ └── com │ └── loizenai │ └── uploaddownloadfiles │ └── SpringBootUploadDownloadMultipartFileApplicationTests.java └── SpringBootUploadDownloadMultipleFilesAjax ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── loizenai │ │ └── uploaddownloadfiles │ │ ├── SpringBootUploadFilesAjaxApplication.java │ │ ├── controller │ │ ├── DownloadController.java │ │ └── UploadController.java │ │ ├── errorhandler │ │ └── RestExceptionHandler.java │ │ ├── message │ │ ├── Error.java │ │ ├── FileInfo.java │ │ ├── Message.java │ │ └── Response.java │ │ └── service │ │ ├── FileService.java │ │ └── FileServiceImpl.java └── resources │ ├── application.properties │ └── static │ ├── index.html │ └── js │ └── uploaddownloadfiles.js └── test └── java └── com └── loizenai └── uploaddownloadfiles └── SpringBootUploadFilesAjaxApplicationTests.java /Angular SpringBoot Pagination - Backend RestAPs/README.md: -------------------------------------------------------------------------------- 1 | Build Angular Table Pagination Filtering Sorting with SpringBoot RestAPIs + Bootstrap Example – Angular 10-9-8-6 Tutorial 2 | https://loizenai.com/build-angular-table-pagination-filtering-sorting-with-springboot-restapis-example-tutorial/ 3 | 4 | When we have a large dataset and we want to present it to the user in smaller chunks, pagination and sorting is often helpful solution. So in the tutorial, I introduce how to use Angular to build a table solution for pagination, filtering and sorting the fetching data with SpringBoot RestAPIs examples. 5 | 6 | Related posts: 7 | 8 | 1. Angular Nodejs Fullstack CRUD Application with MySQL/PostgreSQL – Angular 10-9-8 HttpClient + Nodejs Express, Sequelize ORM 9 | https://loizenai.com/angular-nodejs-fullstack-crud-application-with-mysql-postgresql-angular-10-9-8-httpclient-client-nodejs-express-sequelize-orm/ 10 | 11 | 2. Angular Spring Boot JWT Authentication Example – Angular 6, 8, 9 + Spring Security + MySQL/PostgreSQL 12 | https://loizenai.com/angular-spring-boot-jwt-authentication-example-angular-6-8-9-spring-security-mysql-postgresql/ 13 | 14 | 3. Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 15 | https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 16 | -------------------------------------------------------------------------------- /Angular SpringBoot Pagination - Backend RestAPs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootPagingAndSorting 13 | 1.00 14 | SpringBootPagingAndSorting 15 | SpringBootPagingAndSorting 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | runtime 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.junit.vintage 43 | junit-vintage-engine 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Angular SpringBoot Pagination - Backend RestAPs/src/main/java/com/loizenai/springboot/pagingansorting/model/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.model; 2 | 3 | import java.util.List; 4 | 5 | public class Response { 6 | private List customers; 7 | private int totalPages; 8 | private int pageNumber; 9 | private int pageSize; 10 | 11 | public Response(){} 12 | 13 | public Response(List customers, int totalPages, 14 | int pageNumber, int pageSize) { 15 | this.customers = customers; 16 | this.totalPages = totalPages; 17 | this.pageNumber = pageNumber; 18 | this.pageSize = pageSize; 19 | } 20 | 21 | public void setCustomers(List customers) { 22 | this.customers = customers; 23 | } 24 | 25 | public List getCustomers() { 26 | return this.customers; 27 | } 28 | 29 | public void setTotalPages(int totalPages) { 30 | this.totalPages = totalPages; 31 | } 32 | 33 | public int getTotalPages() { 34 | return this.totalPages; 35 | } 36 | 37 | public void setPageNumber(int pageNumber) { 38 | this.pageNumber = pageNumber; 39 | } 40 | 41 | public int getPageNumber() { 42 | return this.pageNumber; 43 | } 44 | 45 | public void setPageSize(int pageSize) { 46 | this.pageSize = pageSize; 47 | } 48 | 49 | public int getPageSize() { 50 | return this.pageSize; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Angular SpringBoot Pagination - Backend RestAPs/src/main/java/com/loizenai/springboot/pagingansorting/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.jpa.repository.Query; 8 | import org.springframework.data.repository.PagingAndSortingRepository; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import com.loizenai.springboot.pagingansorting.model.Customer; 12 | 13 | @Repository 14 | public interface CustomerRepository extends PagingAndSortingRepository{ 15 | Page findAllBySalary (double salary, Pageable pageable); 16 | 17 | @Query("SELECT DISTINCT c.salary FROM Customer c") 18 | List findDistinctSalary(); 19 | } -------------------------------------------------------------------------------- /Angular SpringBoot Pagination - Backend RestAPs/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /Angular SpringBoot Pagination - Backend RestAPs/src/test/java/com/loizenai/springboot/pagingansorting/SpringBootPagingAndSortingApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootPagingAndSortingApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/README.md: -------------------------------------------------------------------------------- 1 | Tutorial: Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 2 | *** 3 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 4 | 5 | Related posts: 6 | *** 7 | 1. Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 8 | https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 9 | 10 | 2. Angular Spring Boot JWT Authentication Example – Angular 6, 8, 9 + Spring Security + MySQL/PostgreSQL 11 | https://loizenai.com/angular-spring-boot-jwt-authentication-example-angular-6-8-9-spring-security-mysql-postgresql/ 12 | 13 | 3. Angular Django CRUD RestAPIs Application Examples – Fullstack Angular 10 + Django + MySQL/PostgreSQL Tutorial 14 | https://loizenai.com/angular-django-crud-restapis-application-examples-fullstack-angular-10-django-mysql-postgresql/ 15 | 16 | 4. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 17 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 18 | -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.loizenai 12 | SpringBootCRUDApplication 13 | 1 14 | SpringBootCRUDApplication 15 | SpringBootCRUDApplication 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | runtime 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.junit.vintage 43 | junit-vintage-engine 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/java/com/loizenai/crudapp/SpringBootCrudApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCrudApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCrudApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/java/com/loizenai/crudapp/model/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Message { 7 | private String message = ""; 8 | private List customers = new ArrayList(); 9 | private String error = ""; 10 | 11 | public Message(String message, List customers, String error) { 12 | this.message = message; 13 | this.customers = customers; 14 | this.error = error; 15 | } 16 | 17 | public String getMessage() { 18 | return this.message; 19 | } 20 | 21 | public void setMessage(String message) { 22 | this.message = message; 23 | } 24 | 25 | public List getCustomers(){ 26 | return this.customers; 27 | } 28 | 29 | public void setCustomers(ArrayList customers) { 30 | this.customers = customers; 31 | } 32 | 33 | public void setError(String error) { 34 | this.error = error; 35 | } 36 | 37 | public String getError() { 38 | return this.error; 39 | } 40 | } -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/java/com/loizenai/crudapp/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.loizenai.crudapp.model.Customer; 7 | 8 | /** 9 | * @Copyright by https://loizenai.com 10 | * @author https://loizenai.com 11 | * youtube loizenai 12 | */ 13 | 14 | @Repository 15 | public interface CustomerRepository extends JpaRepository{ 16 | } -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/java/com/loizenai/crudapp/service/CustomerServices.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.loizenai.crudapp.model.Customer; 10 | import com.loizenai.crudapp.repository.CustomerRepository; 11 | 12 | /** 13 | * @Copyright by https://loizenai.com 14 | * @author https://loizenai.com 15 | * youtube loizenai 16 | */ 17 | 18 | @Service 19 | public class CustomerServices { 20 | 21 | @Autowired CustomerRepository repository; 22 | 23 | public Customer saveCustomer(Customer customer) { 24 | return repository.save(customer); 25 | } 26 | 27 | public List getCustomerInfos(){ 28 | return repository.findAll(); 29 | } 30 | 31 | public Optional getCustomerById(long id) { 32 | return repository.findById(id); 33 | } 34 | 35 | public boolean checkExistedCustomer(long id) { 36 | if(repository.existsById((long) id)) { 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | public Customer updateCustomer(Customer customer) { 43 | return repository.save(customer); 44 | } 45 | 46 | public void deleteCustomerById(long id) { 47 | repository.deleteById(id); 48 | } 49 | } -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/main/resources/static/js/delete_customer.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | let customerId = 0; 3 | 4 | $(document).on("click", "#div_customer_table table button.btn_delete", function() { 5 | let btn_id = (event.srcElement.id); 6 | customerId = btn_id.split("_")[2]; 7 | 8 | $("div.modal-body") 9 | .text("Do you want delete a Customer with id = " + customerId + " ?"); 10 | $("#model-delete-btn").css({"display": "inline"}); 11 | }); 12 | 13 | $(document).on("click", "#model-delete-btn", function() { 14 | $.ajax({ 15 | url: '/api/customer/deletebyid/' + customerId, 16 | type: 'DELETE', 17 | success: function(response) { 18 | $("div.modal-body") 19 | .text("Delete successfully a Customer with id = " + customerId + "!"); 20 | 21 | $("#model-delete-btn").css({"display": "none"}); 22 | $("button.btn.btn-secondary").text("Close"); 23 | 24 | // delete the customer row on html page 25 | let row_id = "tr_" + customerId; 26 | $("#" + row_id).remove(); 27 | $("#div_customer_updating").css({"display": "none"}); 28 | }, 29 | error: function(error){ 30 | console.log(error); 31 | $("#div_customer_updating").css({"display": "none"}); 32 | alert("Error -> " + error); 33 | } 34 | }); 35 | }); 36 | }); -------------------------------------------------------------------------------- /Angular SpringBoot RestAPIs CRUD Application - Backend Development/src/test/java/com/loizenai/crudapp/SpringBootCrudApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootCrudApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.3.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootMongoDBRestAPIs 13 | 1.00 14 | SpringBootMongoDBRestAPIs 15 | SpringBootMongoDBRestAPIs 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-mongodb 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/SpringBootMongoDbRestApIsApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMongoDbRestApIsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMongoDbRestApIsApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/exception/CustomException.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.exception; 2 | 3 | public class CustomException extends Exception { 4 | private static final long serialVersionUID = 1L; 5 | 6 | private String status; 7 | private String message; 8 | 9 | public CustomException(String status, String message) { 10 | super(message); 11 | this.status = status; 12 | this.message = message; 13 | } 14 | 15 | public String getStatus() { 16 | return this.status; 17 | } 18 | 19 | public String getMessage() { 20 | return this.message; 21 | } 22 | } -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/message/ResponseMsg.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.loizenai.springboot.mongodb.model.Customer; 7 | 8 | /** 9 | * Copyright by https://loizenai.com 10 | * Youtube loizenai 11 | * 12 | */ 13 | public class ResponseMsg { 14 | private String message; 15 | private String url; 16 | private String error = ""; 17 | private List customers = new ArrayList(); 18 | 19 | public ResponseMsg(String message, String url, List customers) { 20 | this.message = message; 21 | this.url = url; 22 | this.customers = customers; 23 | } 24 | 25 | public ResponseMsg(String message, String url, String error) { 26 | this.message = message; 27 | this.url = url; 28 | this.error = error; 29 | } 30 | 31 | public ResponseMsg(String message, String url) { 32 | this(message, url, List.of()); 33 | } 34 | 35 | public void setMessage(String message) { 36 | this.message = message; 37 | } 38 | 39 | public String getMessage() { 40 | return this.message; 41 | } 42 | 43 | public void setUrl(String url) { 44 | this.url = url; 45 | } 46 | 47 | public String getUrl() { 48 | return this.url; 49 | } 50 | 51 | public void setCustomers(List customers) { 52 | this.customers = customers; 53 | } 54 | 55 | public List getCustomers() { 56 | return this.customers; 57 | } 58 | 59 | public void setError(String error) { 60 | this.error = error; 61 | } 62 | 63 | public String getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | import org.springframework.data.mongodb.core.mapping.Field; 6 | 7 | @Document(collection = "customers") 8 | public class Customer { 9 | @Id 10 | private String id; 11 | private String firstname; 12 | private String lastname; 13 | private Integer age; 14 | private String address; 15 | 16 | @Field 17 | private String copyrightby = "https://loizenai.com"; 18 | 19 | public Customer(String firstname, String lastname, int age, String address) { 20 | this.firstname = firstname; 21 | this.lastname = lastname; 22 | this.age = age; 23 | this.address = address; 24 | } 25 | 26 | 27 | public void setId(String id) { 28 | this.id = id; 29 | } 30 | 31 | public String getId() { 32 | return this.id; 33 | } 34 | 35 | public void setFirstname(String firstname) { 36 | this.firstname = firstname; 37 | } 38 | 39 | public String getFirstname() { 40 | return this.firstname; 41 | } 42 | 43 | public void setLastname(String lastname) { 44 | this.lastname = lastname; 45 | } 46 | 47 | public String getLastname() { 48 | return this.lastname; 49 | } 50 | 51 | public void setAge(int age) { 52 | this.age = age; 53 | } 54 | 55 | public int getAge() { 56 | return this.age; 57 | } 58 | 59 | public void setAddress(String address) { 60 | this.address = address; 61 | } 62 | 63 | public String getAddress() { 64 | return this.address; 65 | } 66 | 67 | public void setCopyrightby(String copyrightby) { 68 | this.copyrightby = copyrightby; 69 | } 70 | 71 | public String getCopyrightby() { 72 | return this.copyrightby; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.loizenai.springboot.mongodb.model.Customer; 6 | 7 | public interface CustomerRepository extends MongoRepository{ 8 | } -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/java/com/loizenai/springboot/mongodb/service/CustomerService.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.loizenai.springboot.mongodb.exception.CustomException; 10 | import com.loizenai.springboot.mongodb.model.Customer; 11 | import com.loizenai.springboot.mongodb.repository.CustomerRepository; 12 | 13 | @Service 14 | public class CustomerService { 15 | 16 | @Autowired 17 | CustomerRepository repo; 18 | 19 | public Customer saveCustomer(Customer customer){ 20 | return repo.save(customer); 21 | } 22 | 23 | public List retrieveAllCustomers(){ 24 | return repo.findAll(); 25 | } 26 | 27 | public Customer updateCustomer(String id, Customer customer) throws CustomException { 28 | 29 | Optional customerOpt = repo.findById(id); 30 | 31 | if(!customerOpt.isPresent()) { 32 | throw new CustomException("404", "Can not find a customer for updating with id = " + id); 33 | } 34 | 35 | Customer _customer = customerOpt.get(); 36 | 37 | _customer.setFirstname(customer.getFirstname()); 38 | _customer.setLastname(customer.getLastname()); 39 | _customer.setAddress(customer.getAddress()); 40 | _customer.setAge(customer.getAge()); 41 | 42 | repo.save(_customer); 43 | 44 | return _customer; 45 | } 46 | 47 | public void deleteCustomerById(String id) { 48 | repo.deleteById(id); 49 | } 50 | } -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #mongodb 2 | spring.data.mongodb.uri=mongodb+srv://loizenai:loizenai@cluster0.gmd7e.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /Angular-SpringBoot-MongoDB-CRUD-RestAPIs-Backend-Development/src/test/java/com/loizenai/springboot/mongodb/SpringBootMongoDbRestApIsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootMongoDbRestApIsApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/README.md: -------------------------------------------------------------------------------- 1 | Angular 10 Spring Boot JWT Authentication Example – Angular 6, 8, 9, 10 + Spring Security + MySQL/PostgreSQL 2 | Link: https://loizenai.com/angular-spring-boot-jwt-authentication-example-angular-6-8-9-spring-security-mysql-postgresql 3 | 4 | Related posts: 5 | 1. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 6 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 7 | 8 | 2. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 9 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 10 | 11 | 3. Angular Nodejs Fullstack CRUD Application with MySQL/PostgreSQL – Angular 10-9-8 HttpClient + Nodejs Express, Sequelize ORM 12 | https://loizenai.com/angular-nodejs-fullstack-crud-application-with-mysql-postgresql-angular-10-9-8-httpclient-client-nodejs-express-sequelize-orm/ 13 | -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/SpringBootJwtAuthenticationExamplesApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootJwtAuthenticationExamplesApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootJwtAuthenticationExamplesApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/controller/TestRestAPIs.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.CrossOrigin; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @CrossOrigin(origins = "*", maxAge = 3600) 9 | @RestController 10 | public class TestRestAPIs { 11 | 12 | @GetMapping("/api/test/user") 13 | @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") 14 | public String userAccess() { 15 | return ">>> User Contents!"; 16 | } 17 | 18 | @GetMapping("/api/test/pm") 19 | @PreAuthorize("hasRole('PM') or hasRole('ADMIN')") 20 | public String projectManagementAccess() { 21 | return ">>> Project Management Board"; 22 | } 23 | 24 | @GetMapping("/api/test/admin") 25 | @PreAuthorize("hasRole('ADMIN')") 26 | public String adminAccess() { 27 | return ">>> Admin Contents"; 28 | } 29 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/message/request/LoginForm.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.request; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.Size; 5 | 6 | public class LoginForm { 7 | @NotBlank 8 | @Size(min=3, max = 60) 9 | private String username; 10 | 11 | @NotBlank 12 | @Size(min = 6, max = 40) 13 | private String password; 14 | 15 | public String getUsername() { 16 | return username; 17 | } 18 | 19 | public void setUsername(String username) { 20 | this.username = username; 21 | } 22 | 23 | public String getPassword() { 24 | return password; 25 | } 26 | 27 | public void setPassword(String password) { 28 | this.password = password; 29 | } 30 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/message/request/SignUpForm.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.request; 2 | 3 | import java.util.Set; 4 | 5 | import javax.validation.constraints.*; 6 | 7 | public class SignUpForm { 8 | @NotBlank 9 | @Size(min = 3, max = 50) 10 | private String name; 11 | 12 | @NotBlank 13 | @Size(min = 3, max = 50) 14 | private String username; 15 | 16 | @NotBlank 17 | @Size(max = 60) 18 | @Email 19 | private String email; 20 | 21 | private Set role; 22 | 23 | @NotBlank 24 | @Size(min = 6, max = 40) 25 | private String password; 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getUsername() { 36 | return username; 37 | } 38 | 39 | public void setUsername(String username) { 40 | this.username = username; 41 | } 42 | 43 | public String getEmail() { 44 | return email; 45 | } 46 | 47 | public void setEmail(String email) { 48 | this.email = email; 49 | } 50 | 51 | public String getPassword() { 52 | return password; 53 | } 54 | 55 | public void setPassword(String password) { 56 | this.password = password; 57 | } 58 | 59 | public Set getRole() { 60 | return this.role; 61 | } 62 | 63 | public void setRole(Set role) { 64 | this.role = role; 65 | } 66 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/message/response/JwtResponse.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.response; 2 | 3 | import java.util.Collection; 4 | 5 | import org.springframework.security.core.GrantedAuthority; 6 | 7 | public class JwtResponse { 8 | private String token; 9 | private String type = "Bearer"; 10 | private String username; 11 | private Collection authorities; 12 | 13 | public JwtResponse(String accessToken, String username, Collection authorities) { 14 | this.token = accessToken; 15 | this.username = username; 16 | this.authorities = authorities; 17 | } 18 | 19 | public String getAccessToken() { 20 | return token; 21 | } 22 | 23 | public void setAccessToken(String accessToken) { 24 | this.token = accessToken; 25 | } 26 | 27 | public String getTokenType() { 28 | return type; 29 | } 30 | 31 | public void setTokenType(String tokenType) { 32 | this.type = tokenType; 33 | } 34 | 35 | public String getUsername() { 36 | return username; 37 | } 38 | 39 | public void setUsername(String username) { 40 | this.username = username; 41 | } 42 | 43 | public Collection getAuthorities() { 44 | return authorities; 45 | } 46 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/message/response/ResponseMessage.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.response; 2 | 3 | public class ResponseMessage { 4 | private String message; 5 | 6 | public ResponseMessage(String message) { 7 | this.message = message; 8 | } 9 | 10 | public String getMessage() { 11 | return message; 12 | } 13 | 14 | public void setMessage(String message) { 15 | this.message = message; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.model; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.EnumType; 6 | import javax.persistence.Enumerated; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | import org.hibernate.annotations.NaturalId; 13 | 14 | @Entity 15 | @Table(name = "roles") 16 | public class Role { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | @Enumerated(EnumType.STRING) 22 | @NaturalId 23 | @Column(length = 60) 24 | private RoleName name; 25 | 26 | public Role() {} 27 | 28 | public Role(RoleName name) { 29 | this.name = name; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public RoleName getName() { 41 | return name; 42 | } 43 | 44 | public void setName(RoleName name) { 45 | this.name = name; 46 | } 47 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/model/RoleName.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.model; 2 | 3 | public enum RoleName { 4 | ROLE_USER, 5 | ROLE_PM, 6 | ROLE_ADMIN 7 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/repository/RoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.loizenai.jwtauthentication.model.Role; 9 | import com.loizenai.jwtauthentication.model.RoleName; 10 | 11 | @Repository 12 | public interface RoleRepository extends JpaRepository { 13 | Optional findByName(RoleName roleName); 14 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.loizenai.jwtauthentication.model.User; 9 | 10 | @Repository 11 | public interface UserRepository extends JpaRepository { 12 | Optional findByUsername(String username); 13 | Boolean existsByUsername(String username); 14 | Boolean existsByEmail(String email); 15 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/security/jwt/JwtAuthEntryPoint.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.security.jwt; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.security.core.AuthenticationException; 12 | import org.springframework.security.web.AuthenticationEntryPoint; 13 | import org.springframework.stereotype.Component; 14 | 15 | @Component 16 | public class JwtAuthEntryPoint implements AuthenticationEntryPoint { 17 | 18 | private static final Logger logger = LoggerFactory.getLogger(JwtAuthEntryPoint.class); 19 | 20 | @Override 21 | public void commence(HttpServletRequest request, 22 | HttpServletResponse response, 23 | AuthenticationException e) 24 | throws IOException, ServletException { 25 | 26 | logger.error("Unauthorized error. Message - {}", e.getMessage()); 27 | response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error -> Unauthorized"); 28 | } 29 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/java/com/loizenai/jwtauthentication/security/services/UserDetailsServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.security.services; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.UserDetails; 5 | import org.springframework.security.core.userdetails.UserDetailsService; 6 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.loizenai.jwtauthentication.model.User; 11 | import com.loizenai.jwtauthentication.repository.UserRepository; 12 | 13 | @Service 14 | public class UserDetailsServiceImpl implements UserDetailsService { 15 | 16 | @Autowired 17 | UserRepository userRepository; 18 | 19 | @Override 20 | @Transactional 21 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 22 | 23 | User user = userRepository.findByUsername(username).orElseThrow( 24 | () -> new UsernameNotFoundException("User Not Found with -> username or email : " + username)); 25 | 26 | return UserPrinciple.build(user); 27 | } 28 | } -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | # App Properties 7 | loizenai.app.jwtSecret=jwtLoizenai.comSecretKey 8 | loizenai.app.jwtExpiration=86400 -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/main/resources/roles.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO roles(name) VALUES('ROLE_USER'); 2 | INSERT INTO roles(name) VALUES('ROLE_PM'); 3 | INSERT INTO roles(name) VALUES('ROLE_ADMIN'); -------------------------------------------------------------------------------- /Angular10 SpringBoot Jwt Authentication - Backend Development/src/test/java/com/loizenai/jwtauthentication/SpringBootJwtAuthenticationExamplesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootJwtAuthenticationExamplesApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/README.md: -------------------------------------------------------------------------------- 1 | https://loizenai.com/integrate-reactjs-springboot/ 2 | 3 | Tutorial: How to Integrate Reactjs with SpringBoot example. 4 | 5 | React is an open-source, front end, JavaScript library for building user interfaces or UI components. React can be used as a base in the development of single-page or mobile. So in the tutorial, I introduce a tutorial: “How to Integrate Reactjs with SpringBoot example” with the details steps and running sourcecode. 6 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.4.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootRest 13 | 1.00 14 | SpringBootRest 15 | SpringBootRest 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.projectlombok 28 | lombok 29 | 1.18.10 30 | provided 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | org.junit.vintage 39 | junit-vintage-engine 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-maven-plugin 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/java/com/loizenai/springboot/reactjs/SpringBootRestApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootRestApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootRestApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/java/com/loizenai/springboot/reactjs/controller/CustomerController.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.controller; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | import com.loizenai.springboot.reactjs.model.Customer; 8 | 9 | @RestController 10 | @RequestMapping("/api") 11 | public class CustomerController { 12 | 13 | @GetMapping("/customer") 14 | Customer customer() { 15 | return new Customer("Jack", "Smith", "374 William S Canning Blvd", 23); 16 | } 17 | } -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/java/com/loizenai/springboot/reactjs/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.model; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class Customer { 7 | private String firstname; 8 | private String lastname; 9 | private String address; 10 | private int age; 11 | private String copyright = "https://loizenai.com"; 12 | 13 | public Customer(String firstname, String lastname, String address, int age) { 14 | this.firstname = firstname; 15 | this.lastname = lastname; 16 | this.address = address; 17 | this.age = age; 18 | } 19 | } -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/asset-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": { 3 | "main.css": "/static/css/main.6ea727e0.chunk.css", 4 | "main.js": "/static/js/main.613831a1.chunk.js", 5 | "main.js.map": "/static/js/main.613831a1.chunk.js.map", 6 | "runtime-main.js": "/static/js/runtime-main.92ee656d.js", 7 | "runtime-main.js.map": "/static/js/runtime-main.92ee656d.js.map", 8 | "static/js/2.cb01f4d6.chunk.js": "/static/js/2.cb01f4d6.chunk.js", 9 | "static/js/2.cb01f4d6.chunk.js.map": "/static/js/2.cb01f4d6.chunk.js.map", 10 | "index.html": "/index.html", 11 | "precache-manifest.909894d6e5a5ec6adb29c03fbc62f5a5.js": "/precache-manifest.909894d6e5a5ec6adb29c03fbc62f5a5.js", 12 | "service-worker.js": "/service-worker.js", 13 | "static/css/main.6ea727e0.chunk.css.map": "/static/css/main.6ea727e0.chunk.css.map", 14 | "static/js/2.cb01f4d6.chunk.js.LICENSE.txt": "/static/js/2.cb01f4d6.chunk.js.LICENSE.txt" 15 | }, 16 | "entrypoints": [ 17 | "static/js/runtime-main.92ee656d.js", 18 | "static/js/2.cb01f4d6.chunk.js", 19 | "static/css/main.6ea727e0.chunk.css", 20 | "static/js/main.613831a1.chunk.js" 21 | ] 22 | } -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SAAD-BEN/SpringBoot-Tutorials/2a7d15e96681f87b65d33c1dc8ddbec52fb48637/How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/favicon.ico -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SAAD-BEN/SpringBoot-Tutorials/2a7d15e96681f87b65d33c1dc8ddbec52fb48637/How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/logo192.png -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SAAD-BEN/SpringBoot-Tutorials/2a7d15e96681f87b65d33c1dc8ddbec52fb48637/How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/logo512.png -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/precache-manifest.909894d6e5a5ec6adb29c03fbc62f5a5.js: -------------------------------------------------------------------------------- 1 | self.__precacheManifest = (self.__precacheManifest || []).concat([ 2 | { 3 | "revision": "d9a371705745b824c74b764515934cfa", 4 | "url": "/index.html" 5 | }, 6 | { 7 | "revision": "ab53b1993fc5d12459a2", 8 | "url": "/static/css/main.6ea727e0.chunk.css" 9 | }, 10 | { 11 | "revision": "ce5bed5651f0433ea4fd", 12 | "url": "/static/js/2.cb01f4d6.chunk.js" 13 | }, 14 | { 15 | "revision": "570d362d673dab785e62d2b8563e1118", 16 | "url": "/static/js/2.cb01f4d6.chunk.js.LICENSE.txt" 17 | }, 18 | { 19 | "revision": "ab53b1993fc5d12459a2", 20 | "url": "/static/js/main.613831a1.chunk.js" 21 | }, 22 | { 23 | "revision": "ec9875811aa0f00c2e31", 24 | "url": "/static/js/runtime-main.92ee656d.js" 25 | } 26 | ]); -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/service-worker.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Welcome to your Workbox-powered service worker! 3 | * 4 | * You'll need to register this file in your web app and you should 5 | * disable HTTP caching for this file too. 6 | * See https://goo.gl/nhQhGp 7 | * 8 | * The rest of the code is auto-generated. Please don't update this file 9 | * directly; instead, make changes to your Workbox build configuration 10 | * and re-run your build process. 11 | * See https://goo.gl/2aRDsh 12 | */ 13 | 14 | importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js"); 15 | 16 | importScripts( 17 | "/precache-manifest.909894d6e5a5ec6adb29c03fbc62f5a5.js" 18 | ); 19 | 20 | self.addEventListener('message', (event) => { 21 | if (event.data && event.data.type === 'SKIP_WAITING') { 22 | self.skipWaiting(); 23 | } 24 | }); 25 | 26 | workbox.core.clientsClaim(); 27 | 28 | /** 29 | * The workboxSW.precacheAndRoute() method efficiently caches and responds to 30 | * requests for URLs in the manifest. 31 | * See https://goo.gl/S9QRab 32 | */ 33 | self.__precacheManifest = [].concat(self.__precacheManifest || []); 34 | workbox.precaching.precacheAndRoute(self.__precacheManifest, {}); 35 | 36 | workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"), { 37 | 38 | blacklist: [/^\/_/,/\/[^/?]+\.[^/]+$/], 39 | }); 40 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/static/css/main.6ea727e0.chunk.css: -------------------------------------------------------------------------------- 1 | body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}code{font-family:source-code-pro,Menlo,Monaco,Consolas,"Courier New",monospace}.App{text-align:center}.App-logo{height:40vmin;pointer-events:none}@media (prefers-reduced-motion:no-preference){.App-logo{-webkit-animation:App-logo-spin 20s linear infinite;animation:App-logo-spin 20s linear infinite}}.App-header{background-color:#282c34;min-height:100vh;display:flex;flex-direction:column;align-items:center;justify-content:center;font-size:calc(10px + 2vmin);color:#fff}.App-link{color:#61dafb}@-webkit-keyframes App-logo-spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}@keyframes App-logo-spin{0%{transform:rotate(0deg)}to{transform:rotate(1turn)}}.App-lzai{margin:"20px"} 2 | /*# sourceMappingURL=main.6ea727e0.chunk.css.map */ -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/static/css/main.6ea727e0.chunk.css.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["index.css","App.css"],"names":[],"mappings":"AAAA,KACE,QAAS,CACT,mJAEY,CACZ,kCAAmC,CACnC,iCACF,CAEA,KACE,yEAEF,CCZA,KACE,iBACF,CAEA,UACE,aAAc,CACd,mBACF,CAEA,8CACE,UACE,mDAA4C,CAA5C,2CACF,CACF,CAEA,YACE,wBAAyB,CACzB,gBAAiB,CACjB,YAAa,CACb,qBAAsB,CACtB,kBAAmB,CACnB,sBAAuB,CACvB,4BAA6B,CAC7B,UACF,CAEA,UACE,aACF,CAEA,iCACE,GACE,sBACF,CACA,GACE,uBACF,CACF,CAPA,yBACE,GACE,sBACF,CACA,GACE,uBACF,CACF,CAEA,UACE,aACF","file":"main.6ea727e0.chunk.css","sourcesContent":["body {\n margin: 0;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',\n 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',\n sans-serif;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',\n monospace;\n}\n",".App {\n text-align: center;\n}\n\n.App-logo {\n height: 40vmin;\n pointer-events: none;\n}\n\n@media (prefers-reduced-motion: no-preference) {\n .App-logo {\n animation: App-logo-spin infinite 20s linear;\n }\n}\n\n.App-header {\n background-color: #282c34;\n min-height: 100vh;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n font-size: calc(10px + 2vmin);\n color: white;\n}\n\n.App-link {\n color: #61dafb;\n}\n\n@keyframes App-logo-spin {\n from {\n transform: rotate(0deg);\n }\n to {\n transform: rotate(360deg);\n }\n}\n\n.App-lzai {\n margin:\"20px\"\n}"]} -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/static/js/2.cb01f4d6.chunk.js.LICENSE.txt: -------------------------------------------------------------------------------- 1 | /* 2 | object-assign 3 | (c) Sindre Sorhus 4 | @license MIT 5 | */ 6 | 7 | /** @license React v0.19.1 8 | * scheduler.production.min.js 9 | * 10 | * Copyright (c) Facebook, Inc. and its affiliates. 11 | * 12 | * This source code is licensed under the MIT license found in the 13 | * LICENSE file in the root directory of this source tree. 14 | */ 15 | 16 | /** @license React v16.14.0 17 | * react-dom.production.min.js 18 | * 19 | * Copyright (c) Facebook, Inc. and its affiliates. 20 | * 21 | * This source code is licensed under the MIT license found in the 22 | * LICENSE file in the root directory of this source tree. 23 | */ 24 | 25 | /** @license React v16.14.0 26 | * react.production.min.js 27 | * 28 | * Copyright (c) Facebook, Inc. and its affiliates. 29 | * 30 | * This source code is licensed under the MIT license found in the 31 | * LICENSE file in the root directory of this source tree. 32 | */ 33 | -------------------------------------------------------------------------------- /How-to-Integrate-SpringBoot-with-Reactjs-Example/src/main/resources/static/static/js/main.613831a1.chunk.js: -------------------------------------------------------------------------------- 1 | (this.webpackJsonpapp=this.webpackJsonpapp||[]).push([[0],{14:function(e,t,n){},16:function(e,t,n){},17:function(e,t,n){"use strict";n.r(t);var a=n(0),r=n.n(a),o=n(3),c=n.n(o),s=(n(14),n(1)),i=n.n(s),l=n(4),u=n(5),m=n(6),p=n(7),h=n(8),d=(n(16),function(e){Object(h.a)(n,e);var t=Object(p.a)(n);function n(){var e;Object(u.a)(this,n);for(var a=arguments.length,r=new Array(a),o=0;o downloadFile() { 24 | 25 | HttpHeaders headers = new HttpHeaders(); 26 | headers.add("Content-Disposition", "attachment; filename=customers.xlsx"); 27 | 28 | return ResponseEntity.ok().headers(headers) 29 | .contentType(MediaType.parseMediaType("application/vnd.ms-excel")) 30 | .body(new InputStreamResource(fileServices.loadFile())); 31 | } 32 | } -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/errorhandler/Error.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.errorhandler; 2 | 3 | public class Error { 4 | private String errCode; 5 | private String errDesc; 6 | 7 | public Error(String errCode, String errDesc) { 8 | this.errCode = errCode; 9 | this.errDesc = errDesc; 10 | } 11 | 12 | public void setErrCode(String errCode) { 13 | this.errCode = errCode; 14 | } 15 | 16 | public String getErrCode() { 17 | return this.errCode; 18 | } 19 | 20 | public void setErrDesc(String errDesc) { 21 | this.errDesc = errDesc; 22 | } 23 | 24 | public String getErrDesc() { 25 | return this.errDesc; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/errorhandler/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.errorhandler; 2 | 3 | import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 10 | import org.springframework.web.multipart.MultipartException; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | @ControllerAdvice 14 | public class RestExceptionHandler extends ResponseEntityExceptionHandler { 15 | 16 | // Catch file size exceeded exception! 17 | @ExceptionHandler(MultipartException.class) 18 | @ResponseBody 19 | ResponseEntity handleControllerException(HttpServletRequest request, Throwable ex) { 20 | HttpStatus status = getStatus(request); 21 | return new ResponseEntity(new Error("0x123", ex.getMessage()), status); 22 | } 23 | 24 | private HttpStatus getStatus(HttpServletRequest request) { 25 | Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 26 | if (statusCode == null) { 27 | return HttpStatus.INTERNAL_SERVER_ERROR; 28 | } 29 | return HttpStatus.valueOf(statusCode); 30 | } 31 | } -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/message/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.message; 2 | 3 | public class Message { 4 | private String filename; 5 | private String message; 6 | private String status; 7 | 8 | public Message(String filename, String message, String status) { 9 | this.filename = filename; 10 | this.message = message; 11 | this.status = status; 12 | } 13 | 14 | public void setFilename(String filename) { 15 | this.filename = filename; 16 | } 17 | 18 | public String getFilename() { 19 | return this.filename; 20 | } 21 | 22 | public void setMessage(String message) { 23 | this.message = message; 24 | } 25 | 26 | public String getMessage() { 27 | return this.message; 28 | } 29 | 30 | public void setStatus(String status) { 31 | this.status = status; 32 | } 33 | 34 | public String getStatus() { 35 | return this.status; 36 | } 37 | } -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.model; 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 | @Entity 11 | @Table(name = "customer") 12 | public class Customer { 13 | 14 | @Id 15 | @GeneratedValue(strategy = GenerationType.AUTO) 16 | private long id; 17 | 18 | @Column(name = "name") 19 | private String name; 20 | 21 | @Column(name = "address") 22 | private String address; 23 | 24 | @Column(name = "age") 25 | private int age; 26 | 27 | public Customer() { 28 | } 29 | 30 | public Customer(long id, String name, String address, int age) { 31 | this.id = id; 32 | this.name = name; 33 | this.address = address; 34 | this.age = age; 35 | } 36 | 37 | public long getId() { 38 | return id; 39 | } 40 | 41 | public void setId(long id) { 42 | this.id = id; 43 | } 44 | 45 | public String getName() { 46 | return name; 47 | } 48 | 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | public String getAddress() { 54 | return address; 55 | } 56 | 57 | public void setAddress(String address) { 58 | this.address = address; 59 | } 60 | 61 | public int getAge() { 62 | return age; 63 | } 64 | 65 | public void setAge(int age) { 66 | this.age = age; 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"; 72 | } 73 | 74 | } -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.loizenai.excelfile.model.Customer; 6 | 7 | public interface CustomerRepository extends CrudRepository{ 8 | } -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/java/com/loizenai/excelfile/services/ExcelFileServices.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile.services; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.util.List; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.web.multipart.MultipartFile; 10 | 11 | import com.loizenai.excelfile.model.Customer; 12 | import com.loizenai.excelfile.repository.CustomerRepository; 13 | import com.loizenai.excelfile.utils.ExcelUtils; 14 | 15 | @Service 16 | public class ExcelFileServices { 17 | 18 | @Autowired 19 | CustomerRepository customerRepository; 20 | 21 | // Store File Data to Database 22 | public void store(MultipartFile file) { 23 | try { 24 | List lstCustomers = ExcelUtils.parseExcelFile(file.getInputStream()); 25 | // Save Customers to DataBase 26 | customerRepository.saveAll(lstCustomers); 27 | } catch (IOException e) { 28 | throw new RuntimeException("FAIL! -> message = " + e.getMessage()); 29 | } 30 | } 31 | 32 | // Load Data to Excel File 33 | public ByteArrayInputStream loadFile() { 34 | List customers = (List) customerRepository.findAll(); 35 | 36 | try { 37 | ByteArrayInputStream in = ExcelUtils.customersToExcel(customers); 38 | return in; 39 | } catch (IOException e) {} 40 | 41 | return null; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | ## PostgreSQL 2 | spring.datasource.url=jdbc:postgresql://localhost:5432/loizenaidb 3 | spring.datasource.username=postgres 4 | spring.datasource.password=123 5 | #drop & create table again, good for testing, comment this in production 6 | spring.jpa.hibernate.ddl-auto=create 7 | 8 | spring.servlet.multipart.max-file-size=4096KB 9 | spring.servlet.multipart.max-request-size=4096KB -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Upload Download Excel File Examples to PostgreSQL/MySQL 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

Upload Excel Files to PostgreSQL

18 |
19 |
20 | 21 | 22 | 23 |
24 | 25 |
26 | 28 |
29 |
30 |
31 | 32 | -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/main/resources/static/js/excelfiles.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function() { 2 | 3 | function checkExtension(){ 4 | var selection = document.getElementsByName('uploadfiles'); 5 | for (var i=0; icustomers.xlsx"); 35 | $("#uploadedfile").css("display", "block"); 36 | alert("Upload Successfully!"); 37 | }else{ 38 | $("#uploadedfile").css("display", "none"); 39 | alert("Fail!"); 40 | } 41 | }, 42 | error: function(e){ 43 | alert("Fail! " + e); 44 | } 45 | }); 46 | } 47 | 48 | return false; 49 | }); 50 | 51 | }) -------------------------------------------------------------------------------- /SpringBoot Download Upload Import Excel Files to MySQL/src/test/java/com/loizenai/excelfile/DownloadUploadExcelFileApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.excelfile; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class DownloadUploadExcelFileApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/README.md: -------------------------------------------------------------------------------- 1 | SpringBoot Upload Download Multiple Files Examples with Thymeleaf 2 | https://loizenai.com/springboot-upload-multiple-files-examples-with-thymeleaf/ 3 | 4 | Related post: 5 | 1. Ajax Pagination Filtering and Sorting with Bootstrap Table & SpringBoot RestAPI Example – JQuery Ajax Tutorial 6 | https://loizenai.com/jquery-ajax-pagination-filtering-sorting-tutorial-bootstrap-table-springboot-restapis-examples/ 7 | 8 | 2. [Angular SpringBoot] Angular Table Pagination Filtering Sorting with SpringBoot RestAPIs + Bootstrap Example – Angular 10-9-8-6 Tutorial 9 | https://loizenai.com/build-angular-table-pagination-filtering-sorting-with-springboot-restapis-example-tutorial/ 10 | 11 | 3. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 12 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 13 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.loizenai 12 | SpringBootUploadDownloadMultipartFile 13 | 1 14 | SpringBootUploadDownloadMultipartFile 15 | SpringBootUploadDownloadMultipartFile 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/java/com/loizenai/uploaddownloadfiles/SpringBootUploadDownloadMultipartFileApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import javax.annotation.Resource; 4 | 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 10 | 11 | @SpringBootApplication 12 | public class SpringBootUploadDownloadMultipartFileApplication implements CommandLineRunner { 13 | 14 | @Resource 15 | FileStorage fileStorage; 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(SpringBootUploadDownloadMultipartFileApplication.class, args); 19 | } 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | fileStorage.deleteAll(); 24 | fileStorage.init(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/java/com/loizenai/uploaddownloadfiles/controller/UploadMultipleFileController.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.multipart.MultipartFile; 13 | 14 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 15 | 16 | /** 17 | * Copyright by https://loizenai.com 18 | * @author loizenai.com 19 | * 20 | */ 21 | 22 | @Controller 23 | public class UploadMultipleFileController { 24 | 25 | @Autowired 26 | FileStorage fileStorage; 27 | 28 | @GetMapping("/") 29 | public String index() { 30 | return "uploadmultiplefile.html"; 31 | } 32 | 33 | @PostMapping("/") 34 | public String uploadMultipartFile(@RequestParam("uploadfile") MultipartFile[] files, Model model) { 35 | List messages = new ArrayList(); 36 | 37 | for(MultipartFile file: files) { 38 | try { 39 | if(file.getOriginalFilename().isEmpty()) { 40 | messages.add("Fail! -> Empty File Name"); 41 | continue; 42 | } 43 | fileStorage.store(file); 44 | messages.add("Successfully! -> upload filename: " + file.getOriginalFilename()); 45 | } catch (Exception e) { 46 | messages.add("Fail! -> Existed Uploaded Filename: " + file.getOriginalFilename()); 47 | } 48 | } 49 | model.addAttribute("messages", messages); 50 | 51 | return "uploadmultiplefile.html"; 52 | } 53 | } -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/java/com/loizenai/uploaddownloadfiles/controller/UploadSingleFileController.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | import org.springframework.web.multipart.MultipartFile; 10 | 11 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 12 | 13 | @Controller 14 | public class UploadSingleFileController { 15 | 16 | @Autowired 17 | FileStorage fileStorage; 18 | 19 | @GetMapping("/uploadsinglefile") 20 | public String index() { 21 | return "uploadsinglefile.html"; 22 | } 23 | 24 | @PostMapping("/uploadsinglefile") 25 | public String uploadSingleFile(@RequestParam("uploadfile") MultipartFile file, Model model) { 26 | 27 | if(file.getOriginalFilename().isEmpty()) { 28 | model.addAttribute("message", "Fail -> Upload filename is empty! Please check it!"); 29 | return "uploadsinglefile.html"; 30 | } 31 | 32 | try { 33 | fileStorage.store(file); 34 | model.addAttribute("message", "Successfully! -> upload filename: " + file.getOriginalFilename()); 35 | } catch (Exception e) { 36 | model.addAttribute("message", "Fail! -> Existed Uploaded Filename: " + file.getOriginalFilename()); 37 | } 38 | 39 | return "uploadsinglefile.html"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/java/com/loizenai/uploaddownloadfiles/info/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.info; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/java/com/loizenai/uploaddownloadfiles/service/FileStorage.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.service; 2 | 3 | import java.nio.file.Path; 4 | import java.util.stream.Stream; 5 | 6 | import org.springframework.core.io.Resource; 7 | import org.springframework.web.multipart.MultipartFile; 8 | 9 | /** 10 | * Copyright by https://loizenai.com 11 | * @author loizenai.com 12 | * 13 | */ 14 | 15 | public interface FileStorage { 16 | public void store(MultipartFile file); 17 | 18 | public Resource loadFile(String filename); 19 | 20 | public void deleteAll(); 21 | 22 | public void init(); 23 | 24 | public Stream getFiles(); 25 | } -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.http.multipart.max-file-size=10Mb 2 | spring.http.multipart.max-request-size=10Mb -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/resources/templates/files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Uploaded Files 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
19 |

Uploaded Files

20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
NoFilenameDownload
1FilenameLink
36 | 37 | 41 |
42 |
43 |
44 | 45 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/main/resources/templates/uploadsinglefile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Upload Files 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
19 |

Upload File to SpringBoot Backend

20 |
21 |
22 | 23 | 24 |
25 | 26 | Files 27 |
28 |
29 |
30 |
32 |
34 | 35 |
36 |
37 |
38 | 39 | -------------------------------------------------------------------------------- /SpringBoot Upload Download MultipartFile - Thymeleaf/src/test/java/com/loizenai/uploaddownloadfiles/SpringBootUploadDownloadMultipartFileApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUploadDownloadMultipartFileApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/README.md: -------------------------------------------------------------------------------- 1 | Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 2 | ------------------------- 3 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 4 | 5 | In the tutorial, I will introduce how to create a Fullstack SpringBoot RestAPIs CRUD Application to MySQL/PostgreSQL database using Spring WEB MVC framework and Spring JPA for building Backend and using Bootstrap, JQuery Ajax for building frontend client 6 | 7 | Related posts: 8 | 9 | 1. Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 10 | https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 11 | 12 | 2. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 13 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 14 | 15 | 3. Spring Boot Tutorial Pagination, Filtering and Sorting RestAPIs Example | Spring JPA – Pageable & Sort object requests 16 | https://loizenai.com/spring-boot-tutorial-pagination-filtering-and-sorting-example/ 17 | -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.loizenai 12 | SpringBootCRUDApplication 13 | 1 14 | SpringBootCRUDApplication 15 | SpringBootCRUDApplication 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | runtime 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.junit.vintage 43 | junit-vintage-engine 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/java/com/loizenai/crudapp/SpringBootCrudApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCrudApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCrudApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/java/com/loizenai/crudapp/model/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.model; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Message { 7 | private String message = ""; 8 | private List customers = new ArrayList(); 9 | private String error = ""; 10 | 11 | public Message(String message, List customers, String error) { 12 | this.message = message; 13 | this.customers = customers; 14 | this.error = error; 15 | } 16 | 17 | public String getMessage() { 18 | return this.message; 19 | } 20 | 21 | public void setMessage(String message) { 22 | this.message = message; 23 | } 24 | 25 | public List getCustomers(){ 26 | return this.customers; 27 | } 28 | 29 | public void setCustomers(ArrayList customers) { 30 | this.customers = customers; 31 | } 32 | 33 | public void setError(String error) { 34 | this.error = error; 35 | } 36 | 37 | public String getError() { 38 | return this.error; 39 | } 40 | } -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/java/com/loizenai/crudapp/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.loizenai.crudapp.model.Customer; 7 | 8 | /** 9 | * @Copyright by https://loizenai.com 10 | * @author https://loizenai.com 11 | * youtube loizenai 12 | */ 13 | 14 | @Repository 15 | public interface CustomerRepository extends JpaRepository{ 16 | } -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/java/com/loizenai/crudapp/service/CustomerServices.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.loizenai.crudapp.model.Customer; 10 | import com.loizenai.crudapp.repository.CustomerRepository; 11 | 12 | /** 13 | * @Copyright by https://loizenai.com 14 | * @author https://loizenai.com 15 | * youtube loizenai 16 | */ 17 | 18 | @Service 19 | public class CustomerServices { 20 | 21 | @Autowired CustomerRepository repository; 22 | 23 | public Customer saveCustomer(Customer customer) { 24 | return repository.save(customer); 25 | } 26 | 27 | public List getCustomerInfos(){ 28 | return repository.findAll(); 29 | } 30 | 31 | public Optional getCustomerById(long id) { 32 | return repository.findById(id); 33 | } 34 | 35 | public boolean checkExistedCustomer(long id) { 36 | if(repository.existsById((long) id)) { 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | public Customer updateCustomer(Customer customer) { 43 | return repository.save(customer); 44 | } 45 | 46 | public void deleteCustomerById(long id) { 47 | repository.deleteById(id); 48 | } 49 | } -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/main/resources/static/js/delete_customer.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function(){ 2 | let customerId = 0; 3 | 4 | $(document).on("click", "#div_customer_table table button.btn_delete", function() { 5 | let btn_id = (event.srcElement.id); 6 | customerId = btn_id.split("_")[2]; 7 | 8 | $("div.modal-body") 9 | .text("Do you want delete a Customer with id = " + customerId + " ?"); 10 | $("#model-delete-btn").css({"display": "inline"}); 11 | }); 12 | 13 | $(document).on("click", "#model-delete-btn", function() { 14 | $.ajax({ 15 | url: '/api/customer/deletebyid/' + customerId, 16 | type: 'DELETE', 17 | success: function(response) { 18 | $("div.modal-body") 19 | .text("Delete successfully a Customer with id = " + customerId + "!"); 20 | 21 | $("#model-delete-btn").css({"display": "none"}); 22 | $("button.btn.btn-secondary").text("Close"); 23 | 24 | // delete the customer row on html page 25 | let row_id = "tr_" + customerId; 26 | $("#" + row_id).remove(); 27 | $("#div_customer_updating").css({"display": "none"}); 28 | }, 29 | error: function(error){ 30 | console.log(error); 31 | $("#div_customer_updating").css({"display": "none"}); 32 | alert("Error -> " + error); 33 | } 34 | }); 35 | }); 36 | }); -------------------------------------------------------------------------------- /SpringBoot-CRUD-RestAPIs-App/src/test/java/com/loizenai/crudapp/SpringBootCrudApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.crudapp; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootCrudApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/README.md: -------------------------------------------------------------------------------- 1 | SpringBoot RestAPIs Upload Download Multiple CSV files to MySQL/PostgreSQL with Ajax + RestClient 2 | *** 3 | Tutorial Link: https://loizenai.com/springboot-restapi-upload-download-multiple-csv-files-to-mysql-postgresql-database-with-ajax-restclient 4 | 5 | Related posts: 6 | *** 7 | 1. SpringBoot Upload Download Multiple Files with Rest API + Ajax Tutorial 8 | https://loizenai.com/springboot-upload-download-multiple-files-with-rest-api-ajax/ 9 | 10 | 2. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 11 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 12 | 13 | 3. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 14 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 15 | -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/SpringBootUpDownloadCsvMySqlApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootUpDownloadCsvMySqlApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootUpDownloadCsvMySqlApplication.class, args); 11 | } 12 | 13 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/controller/DownloadCsvRestApi.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.controller; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.http.HttpServletResponse; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.loizenai.updownloadcsv.service.CsvFileServices; 12 | 13 | /** 14 | * Copyright https://loizenai.com 15 | * @author loizenai.com 16 | * 17 | */ 18 | @RestController 19 | public class DownloadCsvRestApi { 20 | 21 | @Autowired 22 | CsvFileServices csvFileService; 23 | 24 | /* 25 | * Download CSV Files 26 | */ 27 | @GetMapping("/api/download/csv/") 28 | public void downloadFile(HttpServletResponse response) throws IOException { 29 | response.setContentType("text/csv"); 30 | response.setHeader("Content-Disposition", "attachment; filename=customers.csv"); 31 | csvFileService.loadFile(response.getWriter()); 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/errorhandler/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.errorhandler; 2 | 3 | import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 10 | import org.springframework.web.multipart.MultipartException; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | import com.loizenai.updownloadcsv.message.Error; 14 | import com.loizenai.updownloadcsv.message.Response; 15 | 16 | /** 17 | * 18 | * Copyright by https://loizenai.com 19 | * @author loizenai.com 20 | * 21 | */ 22 | 23 | @ControllerAdvice 24 | public class RestExceptionHandler extends ResponseEntityExceptionHandler { 25 | 26 | // Catch file size exceeded exception! 27 | @SuppressWarnings({ "rawtypes", "unchecked" }) 28 | @ExceptionHandler(MultipartException.class) 29 | @ResponseBody 30 | ResponseEntity handleControllerException(HttpServletRequest request, Throwable ex) { 31 | HttpStatus status = getStatus(request); 32 | Error err = new Error("0x123", ex.getMessage()); 33 | Response res = new Response("error", err); 34 | 35 | return new ResponseEntity(res, status); 36 | } 37 | 38 | private HttpStatus getStatus(HttpServletRequest request) { 39 | Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 40 | if (statusCode == null) { 41 | return HttpStatus.INTERNAL_SERVER_ERROR; 42 | } 43 | return HttpStatus.valueOf(statusCode); 44 | } 45 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/message/Error.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class Error { 9 | private String errCode; 10 | private String errDesc; 11 | 12 | public Error(String errCode, String errDesc) { 13 | this.errCode = errCode; 14 | this.errDesc = errDesc; 15 | } 16 | 17 | public void setErrCode(String errCode) { 18 | this.errCode = errCode; 19 | } 20 | 21 | public String getErrCode() { 22 | return this.errCode; 23 | } 24 | 25 | public void setErrDesc(String errDesc) { 26 | this.errDesc = errDesc; 27 | } 28 | 29 | public String getErrDesc() { 30 | return this.errDesc; 31 | } 32 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/message/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/message/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | 9 | public class Message { 10 | private String filename; 11 | private String message; 12 | private String status; 13 | 14 | public Message(String filename, String message, String status) { 15 | this.filename = filename; 16 | this.message = message; 17 | this.status = status; 18 | } 19 | 20 | public void setFilename(String filename) { 21 | this.filename = filename; 22 | } 23 | 24 | public String getFilename() { 25 | return this.filename; 26 | } 27 | 28 | public void setMessage(String message) { 29 | this.message = message; 30 | } 31 | 32 | public String getMessage() { 33 | return this.message; 34 | } 35 | 36 | public void setStatus(String status) { 37 | this.status = status; 38 | } 39 | 40 | public String getStatus() { 41 | return this.status; 42 | } 43 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/message/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | 12 | public class Response { 13 | private List messages = null; 14 | private List fileInfos = null; 15 | private Error error = null; 16 | private String errStatus = ""; 17 | 18 | public Response() { 19 | this.messages = new ArrayList(); 20 | } 21 | 22 | public Response(List fileInfos) { 23 | this.fileInfos = fileInfos; 24 | } 25 | 26 | public Response(String errStatus, Error err) { 27 | this.errStatus = errStatus; 28 | this.error = err; 29 | } 30 | 31 | public void addFileInfo(FileInfo file) { 32 | this.fileInfos.add(file); 33 | } 34 | 35 | public List getFileInfos(){ 36 | return this.fileInfos; 37 | } 38 | 39 | public void setMessages(List messages) { 40 | this.messages = messages; 41 | } 42 | 43 | public List getMessages() { 44 | return this.messages; 45 | } 46 | 47 | public void addMessage(Message message) { 48 | this.messages.add(message); 49 | } 50 | 51 | public void setErrStatus(String status) { 52 | this.errStatus = status; 53 | } 54 | 55 | public String getErrStatus() { 56 | return this.errStatus; 57 | } 58 | 59 | public void setError(Error error) { 60 | this.error = error; 61 | } 62 | 63 | public Error getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.model; 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 | 11 | /** 12 | * Copyright by https://loizenai.com 13 | * @author loizenai.com 14 | * 15 | */ 16 | @Entity 17 | @Table(name = "customer") 18 | public class Customer { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.AUTO) 22 | private long id; 23 | 24 | @Column(name = "name") 25 | private String name; 26 | 27 | @Column(name = "address") 28 | private String address; 29 | 30 | @Column(name = "age") 31 | private int age; 32 | 33 | public Customer() { 34 | } 35 | 36 | public Customer(long id, String name, String address, int age) { 37 | this.id = id; 38 | this.name = name; 39 | this.address = address; 40 | this.age = age; 41 | } 42 | 43 | public long getId() { 44 | return id; 45 | } 46 | 47 | public void setId(long id) { 48 | this.id = id; 49 | } 50 | 51 | public String getName() { 52 | return name; 53 | } 54 | 55 | public void setName(String name) { 56 | this.name = name; 57 | } 58 | 59 | public String getAddress() { 60 | return address; 61 | } 62 | 63 | public void setAddress(String address) { 64 | this.address = address; 65 | } 66 | 67 | public int getAge() { 68 | return age; 69 | } 70 | 71 | public void setAge(int age) { 72 | this.age = age; 73 | } 74 | 75 | @Override 76 | public String toString() { 77 | return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"; 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/java/com/loizenai/updownloadcsv/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | import org.springframework.stereotype.Repository; 5 | 6 | import com.loizenai.updownloadcsv.model.Customer; 7 | 8 | 9 | /** 10 | * 11 | * Copyright by https://loizenai.com 12 | * @author loizenai.com 13 | * 14 | */ 15 | @Repository 16 | public interface CustomerRepository extends CrudRepository{ 17 | } -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true -------------------------------------------------------------------------------- /SpringBoot-CSV-Upload-Download-csv-Files-to-MySQL/src/test/java/com/loizenai/updownloadcsv/SpringBootUpDownloadCsvMySqlApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.updownloadcsv; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUpDownloadCsvMySqlApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/README.md: -------------------------------------------------------------------------------- 1 | Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 2 | Link: https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 3 | 4 | Related posts: 5 | 1. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 6 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 7 | 8 | 2. SpringBoot Upload Download Multiple Files Examples with Thymeleaf 9 | https://loizenai.com/springboot-upload-multiple-files-examples-with-thymeleaf/ 10 | 11 | 3. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 12 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 13 | -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/SpringBootJwtAuthenticationExamplesApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootJwtAuthenticationExamplesApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootJwtAuthenticationExamplesApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/controller/TestRestAPIs.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.controller; 2 | 3 | import org.springframework.security.access.prepost.PreAuthorize; 4 | import org.springframework.web.bind.annotation.GetMapping; 5 | import org.springframework.web.bind.annotation.RestController; 6 | 7 | @RestController 8 | public class TestRestAPIs { 9 | 10 | @GetMapping("/api/test/user") 11 | @PreAuthorize("hasRole('USER') or hasRole('ADMIN')") 12 | public String userAccess() { 13 | return ">>> User Contents!"; 14 | } 15 | 16 | @GetMapping("/api/test/pm") 17 | @PreAuthorize("hasRole('PM') or hasRole('ADMIN')") 18 | public String projectManagementAccess() { 19 | return ">>> Board Management Project"; 20 | } 21 | 22 | @GetMapping("/api/test/admin") 23 | @PreAuthorize("hasRole('ADMIN')") 24 | public String adminAccess() { 25 | return ">>> Admin Contents"; 26 | } 27 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/message/request/LoginForm.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.request; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.Size; 5 | 6 | public class LoginForm { 7 | @NotBlank 8 | @Size(min=3, max = 60) 9 | private String username; 10 | 11 | @NotBlank 12 | @Size(min = 6, max = 40) 13 | private String password; 14 | 15 | public String getUsername() { 16 | return username; 17 | } 18 | 19 | public void setUsername(String username) { 20 | this.username = username; 21 | } 22 | 23 | public String getPassword() { 24 | return password; 25 | } 26 | 27 | public void setPassword(String password) { 28 | this.password = password; 29 | } 30 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/message/request/SignUpForm.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.request; 2 | 3 | import java.util.Set; 4 | 5 | import javax.validation.constraints.*; 6 | 7 | public class SignUpForm { 8 | @NotBlank 9 | @Size(min = 3, max = 50) 10 | private String name; 11 | 12 | @NotBlank 13 | @Size(min = 3, max = 50) 14 | private String username; 15 | 16 | @NotBlank 17 | @Size(max = 60) 18 | @Email 19 | private String email; 20 | 21 | private Set role; 22 | 23 | @NotBlank 24 | @Size(min = 6, max = 40) 25 | private String password; 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | public String getUsername() { 36 | return username; 37 | } 38 | 39 | public void setUsername(String username) { 40 | this.username = username; 41 | } 42 | 43 | public String getEmail() { 44 | return email; 45 | } 46 | 47 | public void setEmail(String email) { 48 | this.email = email; 49 | } 50 | 51 | public String getPassword() { 52 | return password; 53 | } 54 | 55 | public void setPassword(String password) { 56 | this.password = password; 57 | } 58 | 59 | public Set getRole() { 60 | return this.role; 61 | } 62 | 63 | public void setRole(Set role) { 64 | this.role = role; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/message/response/JwtResponse.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.message.response; 2 | 3 | public class JwtResponse { 4 | private String token; 5 | private String type = "Bearer"; 6 | 7 | public JwtResponse(String accessToken) { 8 | this.token = accessToken; 9 | } 10 | 11 | public String getAccessToken() { 12 | return token; 13 | } 14 | 15 | public void setAccessToken(String accessToken) { 16 | this.token = accessToken; 17 | } 18 | 19 | public String getTokenType() { 20 | return type; 21 | } 22 | 23 | public void setTokenType(String tokenType) { 24 | this.type = tokenType; 25 | } 26 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/model/Role.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.model; 2 | 3 | import javax.persistence.Column; 4 | import javax.persistence.Entity; 5 | import javax.persistence.EnumType; 6 | import javax.persistence.Enumerated; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.Table; 11 | 12 | import org.hibernate.annotations.NaturalId; 13 | 14 | @Entity 15 | @Table(name = "roles") 16 | public class Role { 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.IDENTITY) 19 | private Long id; 20 | 21 | @Enumerated(EnumType.STRING) 22 | @NaturalId 23 | @Column(length = 60) 24 | private RoleName name; 25 | 26 | public Role() {} 27 | 28 | public Role(RoleName name) { 29 | this.name = name; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public RoleName getName() { 41 | return name; 42 | } 43 | 44 | public void setName(RoleName name) { 45 | this.name = name; 46 | } 47 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/model/RoleName.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.model; 2 | 3 | public enum RoleName { 4 | ROLE_USER, 5 | ROLE_PM, 6 | ROLE_ADMIN 7 | } 8 | -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/repository/RoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.loizenai.jwtauthentication.model.Role; 9 | import com.loizenai.jwtauthentication.model.RoleName; 10 | 11 | @Repository 12 | public interface RoleRepository extends JpaRepository { 13 | Optional findByName(RoleName roleName); 14 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.repository; 2 | 3 | import java.util.Optional; 4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.loizenai.jwtauthentication.model.User; 9 | 10 | @Repository 11 | public interface UserRepository extends JpaRepository { 12 | Optional findByUsername(String username); 13 | Boolean existsByUsername(String username); 14 | Boolean existsByEmail(String email); 15 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/security/jwt/JwtAuthEntryPoint.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.security.jwt; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.ServletException; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | import org.springframework.security.core.AuthenticationException; 12 | import org.springframework.security.web.AuthenticationEntryPoint; 13 | import org.springframework.stereotype.Component; 14 | 15 | @Component 16 | public class JwtAuthEntryPoint implements AuthenticationEntryPoint { 17 | 18 | private static final Logger logger = LoggerFactory.getLogger(JwtAuthEntryPoint.class); 19 | 20 | @Override 21 | public void commence(HttpServletRequest request, 22 | HttpServletResponse response, 23 | AuthenticationException e) 24 | throws IOException, ServletException { 25 | 26 | logger.error("Unauthorized error. Message - {}", e.getMessage()); 27 | response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error -> Unauthorized"); 28 | } 29 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/java/com/loizenai/jwtauthentication/security/services/UserDetailsServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication.security.services; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.security.core.userdetails.UserDetails; 5 | import org.springframework.security.core.userdetails.UserDetailsService; 6 | import org.springframework.security.core.userdetails.UsernameNotFoundException; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import com.loizenai.jwtauthentication.model.User; 11 | import com.loizenai.jwtauthentication.repository.UserRepository; 12 | 13 | @Service 14 | public class UserDetailsServiceImpl implements UserDetailsService { 15 | 16 | @Autowired 17 | UserRepository userRepository; 18 | 19 | @Override 20 | @Transactional 21 | public UserDetails loadUserByUsername(String username) 22 | throws UsernameNotFoundException { 23 | 24 | User user = userRepository.findByUsername(username) 25 | .orElseThrow(() -> 26 | new UsernameNotFoundException("User Not Found with -> username or email : " + username) 27 | ); 28 | 29 | return UserPrinciple.build(user); 30 | } 31 | } -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | # App Properties 7 | loizenai.app.jwtSecret=jwtLoizenai.comSecretKey 8 | loizenai.app.jwtExpiration=86400 -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/main/resources/roles.sql: -------------------------------------------------------------------------------- 1 | INSERT INTO roles(name) VALUES('ROLE_USER'); 2 | INSERT INTO roles(name) VALUES('ROLE_PM'); 3 | INSERT INTO roles(name) VALUES('ROLE_ADMIN'); -------------------------------------------------------------------------------- /SpringBoot-Jwt-RestAPIs-Authentication-Examples/src/test/java/com/loizenai/jwtauthentication/SpringBootJwtAuthenticationExamplesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.jwtauthentication; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootJwtAuthenticationExamplesApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.3.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootMongoDBRestAPIs 13 | 1.00 14 | SpringBootMongoDBRestAPIs 15 | SpringBootMongoDBRestAPIs 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-mongodb 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/java/com/loizenai/springboot/mongodb/SpringBootMongoDbRestApIsApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootMongoDbRestApIsApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootMongoDbRestApIsApplication.class, args); 11 | } 12 | } -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/java/com/loizenai/springboot/mongodb/exception/CustomException.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.exception; 2 | 3 | public class CustomException extends Exception { 4 | private static final long serialVersionUID = 1L; 5 | 6 | private String status; 7 | private String message; 8 | 9 | public CustomException(String status, String message) { 10 | super(message); 11 | this.status = status; 12 | this.message = message; 13 | } 14 | 15 | public String getStatus() { 16 | return this.status; 17 | } 18 | 19 | public String getMessage() { 20 | return this.message; 21 | } 22 | } -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/java/com/loizenai/springboot/mongodb/message/ResponseMsg.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import com.loizenai.springboot.mongodb.model.Customer; 7 | 8 | /** 9 | * Copyright by https://loizenai.com 10 | * Youtube loizenai 11 | * 12 | */ 13 | public class ResponseMsg { 14 | private String message; 15 | private String url; 16 | private String error = ""; 17 | private List customers = new ArrayList(); 18 | 19 | public ResponseMsg(String message, String url, List customers) { 20 | this.message = message; 21 | this.url = url; 22 | this.customers = customers; 23 | } 24 | 25 | public ResponseMsg(String message, String url, String error) { 26 | this.message = message; 27 | this.url = url; 28 | this.error = error; 29 | } 30 | 31 | public ResponseMsg(String message, String url) { 32 | this(message, url, List.of()); 33 | } 34 | 35 | public void setMessage(String message) { 36 | this.message = message; 37 | } 38 | 39 | public String getMessage() { 40 | return this.message; 41 | } 42 | 43 | public void setUrl(String url) { 44 | this.url = url; 45 | } 46 | 47 | public String getUrl() { 48 | return this.url; 49 | } 50 | 51 | public void setCustomers(List customers) { 52 | this.customers = customers; 53 | } 54 | 55 | public List getCustomers() { 56 | return this.customers; 57 | } 58 | 59 | public void setError(String error) { 60 | this.error = error; 61 | } 62 | 63 | public String getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/java/com/loizenai/springboot/mongodb/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.loizenai.springboot.mongodb.model.Customer; 6 | 7 | public interface CustomerRepository extends MongoRepository{ 8 | } -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/java/com/loizenai/springboot/mongodb/service/CustomerService.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.service; 2 | 3 | import java.util.List; 4 | import java.util.Optional; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.loizenai.springboot.mongodb.exception.CustomException; 10 | import com.loizenai.springboot.mongodb.model.Customer; 11 | import com.loizenai.springboot.mongodb.repository.CustomerRepository; 12 | 13 | @Service 14 | public class CustomerService { 15 | 16 | @Autowired 17 | CustomerRepository repo; 18 | 19 | public Customer saveCustomer(Customer customer){ 20 | return repo.save(customer); 21 | } 22 | 23 | public List saveListCustomers(List customers) { 24 | return repo.saveAll(customers); 25 | } 26 | 27 | public List retrieveAllCustomers(){ 28 | return repo.findAll(); 29 | } 30 | 31 | public Optional getCustomerByID(String id) { 32 | return repo.findById(id); 33 | } 34 | 35 | public Customer updateCustomer(String id, Customer customer) throws CustomException { 36 | 37 | Optional customerOpt = repo.findById(id); 38 | 39 | if(!customerOpt.isPresent()) { 40 | throw new CustomException("404", "Can not find a customer for updating with id = " + id); 41 | } 42 | 43 | Customer _customer = customerOpt.get(); 44 | 45 | _customer.setFirstname(customer.getFirstname()); 46 | _customer.setLastname(customer.getLastname()); 47 | _customer.setAddress(customer.getAddress()); 48 | _customer.setAge(customer.getAge()); 49 | _customer.setSalary(customer.getSalary()); 50 | 51 | repo.save(_customer); 52 | 53 | return _customer; 54 | } 55 | 56 | public void deleteCustomerById(String id) { 57 | repo.deleteById(id); 58 | } 59 | 60 | public void deleteAll() { 61 | repo.deleteAll(); 62 | } 63 | } -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #mongodb 2 | spring.data.mongodb.uri=mongodb+srv://loizenai:loizenai@cluster0.gmd7e.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /SpringBoot-MongoDB-RestAPIs/src/test/java/com/loizenai/springboot/mongodb/SpringBootMongoDbRestApIsApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootMongoDbRestApIsApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/README.md: -------------------------------------------------------------------------------- 1 | SpringBoot MongoDB Pagination RestAPIs Example 2 | Link: https://loizenai.com/springboot-mongodb-pagination-restapi/ 3 | 4 | Tutorial: SpringBoot MongoDB Pagination RestAPIs Example with MongoRepository (extends PagingAndSortingRepository) 5 | 6 | When we have a large dataset and we want to present it to the user in smaller chunks, pagination and sorting is often helpful solution. So in the tutorial, I introduce how to build “SpringBoot MongoDB Pagination RestAPIs Example” use Spring MongoRepository APIs that extends PagingAndSortingRepository to do the task with SpringBoot project example. 7 | 8 | Here is a to do list for the tutorial: 9 | 10 | – Introduce overview SpringBoot and MongoDB database project. 11 | – Explain a hierarchy diagram of MongoRepository and PagingAndSortingRepository. 12 | – Guide step by step with clearly and running coding examples for how to create a SpringBoot MongoDB Pagination RestAPIs. 13 | – Create an integrative testsuite pagination sorting and filtering requests from Postman client to MongoDB Atlas through SpringBoot Pagination RestAPIs. 14 | -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.4.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootPaginationMongoDB 13 | 1.00 14 | SpringBootPaginationMongoDB 15 | SpringBoot Pagination MongoDB RestAPIs 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-mongodb 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/src/main/java/com/loizenai/springboot/pagination/mongodb/message/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagination.mongodb.message; 2 | 3 | import java.util.List; 4 | 5 | import com.loizenai.springboot.pagination.mongodb.document.Customer; 6 | 7 | public class Response { 8 | private List customers; 9 | private int totalPages; 10 | private int pageNumber; 11 | private int pageSize; 12 | 13 | public Response(){} 14 | 15 | public Response(List customers, int totalPages, 16 | int pageNumber, int pageSize) { 17 | this.customers = customers; 18 | this.totalPages = totalPages; 19 | this.pageNumber = pageNumber; 20 | this.pageSize = pageSize; 21 | } 22 | 23 | public void setCustomers(List customers) { 24 | this.customers = customers; 25 | } 26 | 27 | public List getCustomers() { 28 | return this.customers; 29 | } 30 | 31 | public void setTotalPages(int totalPages) { 32 | this.totalPages = totalPages; 33 | } 34 | 35 | public int getTotalPages() { 36 | return this.totalPages; 37 | } 38 | 39 | public void setPageNumber(int pageNumber) { 40 | this.pageNumber = pageNumber; 41 | } 42 | 43 | public int getPageNumber() { 44 | return this.pageNumber; 45 | } 46 | 47 | public void setPageSize(int pageSize) { 48 | this.pageSize = pageSize; 49 | } 50 | 51 | public int getPageSize() { 52 | return this.pageSize; 53 | } 54 | } -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/src/main/java/com/loizenai/springboot/pagination/mongodb/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagination.mongodb.repository; 2 | 3 | import org.springframework.data.domain.Page; 4 | import org.springframework.data.domain.Pageable; 5 | import org.springframework.data.domain.Slice; 6 | import org.springframework.data.mongodb.repository.MongoRepository; 7 | 8 | import com.loizenai.springboot.pagination.mongodb.document.Customer; 9 | 10 | public interface CustomerRepository extends MongoRepository{ 11 | Slice findAllBySalary (double salary, Pageable pageable); 12 | Page findAllByAgeGreaterThan(int age, Pageable pageable); 13 | } -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #mongodb 2 | spring.data.mongodb.uri=mongodb+srv://loizenai:12345@cluster0.uhqpv.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /SpringBoot-Pagination-MongoDB/src/test/java/com/loizenai/springboot/pagination/mongodb/SpringBootPaginationMongoDbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagination.mongodb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootPaginationMongoDbApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/README.md: -------------------------------------------------------------------------------- 1 | https://loizenai.com/springboot-reactjs-mongodb-crud/ 2 | 3 | In the tutorial, I introduce how to build an “SpringBoot React.js CRUD MongoDB Example” project with the help of SpringData JPA for POST/GET/PUT/DELETE requests with step by step coding examples: 4 | 5 | – SpringBoot project produces CRUD RestAPIs with MongoDB database using the supporting of Spring Data JPA. 6 | – React.js project will consume the SpringBoot CRUD RestAPIs by Ajax then show up on Reactjs component’s views. 7 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/Initializer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.stereotype.Component; 8 | 9 | import com.loizenai.springboot.reactjs.model.Customer; 10 | import com.loizenai.springboot.reactjs.repository.CustomerRepository; 11 | 12 | @Component 13 | class Initializer implements CommandLineRunner { 14 | 15 | private final CustomerRepository repository; 16 | 17 | public Initializer(CustomerRepository repository) { 18 | this.repository = repository; 19 | } 20 | 21 | @Override 22 | public void run(String... strings) { 23 | 24 | ArrayList customers = new ArrayList( Arrays.asList( 25 | new Customer("Jack", "Smith", "374 William S Canning Blvd", 23), 26 | new Customer("Adam", "Johnson", "Fall River MA 2721. 121 Worcester Rd", 31), 27 | new Customer("Dana", "Bay", "Framingham MA 1701. 677 Timpany Blvd", 46)) ); 28 | 29 | System.out.println(customers); 30 | 31 | repository.saveAll(customers); 32 | 33 | repository.findAll().forEach(System.out::println); 34 | } 35 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCrudReactApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCrudReactApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | import org.springframework.data.mongodb.core.mapping.Field; 6 | 7 | @Document(collection = "customers") 8 | public class Customer { 9 | @Id 10 | private String id; 11 | private String firstname; 12 | private String lastname; 13 | private Integer age; 14 | private String address; 15 | 16 | @Field 17 | private String copyrightby = "https://loizenai.com"; 18 | 19 | public Customer(String firstname, String lastname, String address, int age) { 20 | this.firstname = firstname; 21 | this.lastname = lastname; 22 | this.age = age; 23 | this.address = address; 24 | } 25 | 26 | public void setId(String id) { 27 | this.id = id; 28 | } 29 | 30 | public String getId() { 31 | return this.id; 32 | } 33 | 34 | public void setFirstname(String firstname) { 35 | this.firstname = firstname; 36 | } 37 | 38 | public String getFirstname() { 39 | return this.firstname; 40 | } 41 | 42 | public void setLastname(String lastname) { 43 | this.lastname = lastname; 44 | } 45 | 46 | public String getLastname() { 47 | return this.lastname; 48 | } 49 | 50 | public void setAge(int age) { 51 | this.age = age; 52 | } 53 | 54 | public int getAge() { 55 | return this.age; 56 | } 57 | 58 | public void setAddress(String address) { 59 | this.address = address; 60 | } 61 | 62 | public String getAddress() { 63 | return this.address; 64 | } 65 | 66 | public String getCopyrightby() { 67 | return this.copyrightby; 68 | } 69 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.loizenai.springboot.reactjs.model.Customer; 6 | 7 | 8 | public interface CustomerRepository extends MongoRepository{ 9 | Customer findByFirstname(String firstname); 10 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb+srv://loizenai:12345@cluster0.uhqpv.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MongoDB-CRUD-Example/src/test/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootCrudReactApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/README.md: -------------------------------------------------------------------------------- 1 | https://loizenai.com/springboot-react-mysql-crud-example/ 2 | 3 | Tutorial: SpringBoot + React + MySQL: SpringBoot React.js CRUD Example 4 | 5 | In the tutorial, I introduce how to build an “SpringBoot React.js CRUD MySQL Example” project with the help of SpringData JPA for POST/GET/PUT/DELETE requests with step by step coding examples: 6 | 7 | – SpringBoot project produces CRUD RestAPIs with MySQL database using the supporting of Spring Data JPA. 8 | – React.js project will consume the SpringBoot CRUD RestAPIs by Ajax then show up on Reactjs component’s views. 9 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/Initializer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.stereotype.Component; 8 | 9 | import com.loizenai.springboot.reactjs.model.Customer; 10 | import com.loizenai.springboot.reactjs.repository.CustomerRepository; 11 | 12 | @Component 13 | class Initializer implements CommandLineRunner { 14 | 15 | private final CustomerRepository repository; 16 | 17 | public Initializer(CustomerRepository repository) { 18 | this.repository = repository; 19 | } 20 | 21 | @Override 22 | public void run(String... strings) { 23 | 24 | ArrayList customers = new ArrayList( Arrays.asList( 25 | new Customer("Jack", "Smith", "374 William S Canning Blvd", 23), 26 | new Customer("Adam", "Johnson", "Fall River MA 2721. 121 Worcester Rd", 31), 27 | new Customer("Dana", "Bay", "Framingham MA 1701. 677 Timpany Blvd", 46)) ); 28 | 29 | System.out.println(customers); 30 | 31 | repository.saveAll(customers); 32 | 33 | repository.findAll().forEach(System.out::println); 34 | } 35 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCrudReactApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCrudReactApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | import javax.persistence.Table; 7 | 8 | import lombok.Data; 9 | import lombok.NoArgsConstructor; 10 | import lombok.NonNull; 11 | import lombok.RequiredArgsConstructor; 12 | 13 | @Data 14 | @NoArgsConstructor 15 | @RequiredArgsConstructor 16 | @Entity 17 | @Table(name = "customer") 18 | public class Customer { 19 | 20 | @Id 21 | @GeneratedValue 22 | private Long id; 23 | 24 | @NonNull 25 | private String firstname; 26 | @NonNull 27 | private String lastname; 28 | private String address; 29 | private int age; 30 | private String copyright = "https://loizenai.com"; 31 | 32 | public String toString() { 33 | return String.format("id=%d, firstname='%s', lastname'%s', address=%s, age=%d", 34 | id, firstname, lastname, address, age); 35 | } 36 | 37 | public Customer(String firstname, String lastname, String address, int age) { 38 | this.firstname = firstname; 39 | this.lastname = lastname; 40 | this.address = address; 41 | this.age = age; 42 | } 43 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.loizenai.springboot.reactjs.model.Customer; 6 | 7 | public interface CustomerRepository extends JpaRepository { 8 | Customer findByFirstname(String firstname); 9 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /SpringBoot-Reactjs-MySQL-CRUD-Example/src/test/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootCrudReactApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/README.md: -------------------------------------------------------------------------------- 1 | Tutorial: SpringBoot + React + PostgreSQL: SpringBoot React.js CRUD Example 2 | 3 | https://loizenai.com/reactjs-springboot-crud-postgresql/ 4 | 5 | In the tutorial, I introduce how to build an “SpringBoot React.js CRUD PostgreSQL Example” project with the help of SpringData JPA for POST/GET/PUT/DELETE requests with step by step coding examples: 6 | 7 | – SpringBoot project produces CRUD RestAPIs with PostgreSQL database using the supporting of Spring Data JPA. 8 | – React.js project will consume the SpringBoot CRUD RestAPIs by Ajax then show up on Reactjs component’s views. 9 | 10 | What will we do? 11 | 12 | – I draw a fullstack overview Diagram Architecture from React.js Frontend to PostgreSQL database through SpringBoot RestAPI backend. 13 | – Develop SpringBoot CRUD RestAPIs with the supporting of SpringWeb Framework. 14 | – Implement Reactjs CRUD application with Ajax fetching APIs to do CRUD request (Post/Get/Put/Delete) to SpringBoot Backend APIs. 15 | – I create a testsuite with a number of integrative testcases with CRUD RestAPI requests from Reactjs to do CRUD requests to SpringBoot RestAPIs Server and save/retrieve data to PostgreSQL database. 16 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/Initializer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.stereotype.Component; 8 | 9 | import com.loizenai.springboot.reactjs.model.Customer; 10 | import com.loizenai.springboot.reactjs.repository.CustomerRepository; 11 | 12 | @Component 13 | class Initializer implements CommandLineRunner { 14 | 15 | private final CustomerRepository repository; 16 | 17 | public Initializer(CustomerRepository repository) { 18 | this.repository = repository; 19 | } 20 | 21 | @Override 22 | public void run(String... strings) { 23 | 24 | ArrayList customers = new ArrayList( Arrays.asList( 25 | new Customer("Jack", "Smith", "374 William S Canning Blvd", 23), 26 | new Customer("Adam", "Johnson", "Fall River MA 2721. 121 Worcester Rd", 31), 27 | new Customer("Dana", "Bay", "Framingham MA 1701. 677 Timpany Blvd", 46)) ); 28 | 29 | System.out.println(customers); 30 | 31 | repository.saveAll(customers); 32 | 33 | repository.findAll().forEach(System.out::println); 34 | } 35 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootCrudReactApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootCrudReactApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/model/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.model; 2 | 3 | import javax.persistence.Entity; 4 | import javax.persistence.GeneratedValue; 5 | import javax.persistence.Id; 6 | import javax.persistence.Table; 7 | 8 | import lombok.Data; 9 | import lombok.NoArgsConstructor; 10 | import lombok.NonNull; 11 | import lombok.RequiredArgsConstructor; 12 | 13 | @Data 14 | @NoArgsConstructor 15 | @RequiredArgsConstructor 16 | @Entity 17 | @Table(name = "customer") 18 | public class Customer { 19 | 20 | @Id 21 | @GeneratedValue 22 | private Long id; 23 | 24 | @NonNull 25 | private String firstname; 26 | @NonNull 27 | private String lastname; 28 | private String address; 29 | private int age; 30 | private String copyright = "https://loizenai.com"; 31 | 32 | public String toString() { 33 | return String.format("id=%d, firstname='%s', lastname'%s', address=%s, age=%d", 34 | id, firstname, lastname, address, age); 35 | } 36 | 37 | public Customer(String firstname, String lastname, String address, int age) { 38 | this.firstname = firstname; 39 | this.lastname = lastname; 40 | this.address = address; 41 | this.age = age; 42 | } 43 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/main/java/com/loizenai/springboot/reactjs/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs.repository; 2 | 3 | import org.springframework.data.jpa.repository.JpaRepository; 4 | 5 | import com.loizenai.springboot.reactjs.model.Customer; 6 | 7 | public interface CustomerRepository extends JpaRepository { 8 | Customer findByFirstname(String firstname); 9 | } -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /SpringBoot-Reactjs-PostgreSQL-CRUD-Example/src/test/java/com/loizenai/springboot/reactjs/SpringBootCrudReactApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.reactjs; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootCrudReactApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/README.md: -------------------------------------------------------------------------------- 1 | SpringBoot Upload Parse Data from Excel Files to MongoDB 2 | 3 | Link: https://loizenai.com/mongodb-springboot-upload-download-excel-files/ 4 | 5 | Tutorial: [SpringBoot RestAPIs + MongoDB + Excel] How to read data from Excel sheet and insert into MongoDB database in SpringBoot RestAPI Controller (Parse Excel function & Upload RestAPI) – (also Write Excel file Download RestAPI). 6 | 7 | Creating SpringBoot RestAPIs to upload and download multiple Excel files to (Atlas ) MongoDB is one of the most common question in the development world. Going through the tutorial post”SpringBoot Upload Download Multiple Excel files to MySQL/PostgreSQL”, I explain details how to do it by step to step with coding and give you 100% running source code. What we will do? 8 | 9 | – I draw an overview diagram architecture of SpringBoot RestAPI Upload Excel Files. 10 | – I use Spring Web to development Spring RestApis. 11 | – I use ApacheCommon or Open Excel libraries to parse and read Excel files. 12 | – I use SpringData MongoDB to save data from Excel files to MongoDB. 13 | – I implement a SpringBoot Global Exception Handler when uploading with a very big files and fail. 14 | – I use Ajax and Bootstrap to implement a frontend client to upload/download Excel files. 15 | -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.4.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootUploadExcelMongoDB 13 | 1.00 14 | SpringBootUploadExcelMongoDB 15 | SpringBoot Upload Download Excel files to MongoDB 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-mongodb 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | org.apache.poi 32 | poi-ooxml 33 | 4.1.2 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | org.junit.vintage 42 | junit-vintage-engine 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/SpringBootUploadExcelMongoDbApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootUploadExcelMongoDbApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootUploadExcelMongoDbApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/controller/DownloadFileRestAPIs.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.core.io.InputStreamResource; 5 | import org.springframework.http.HttpHeaders; 6 | import org.springframework.http.MediaType; 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.loizenai.springboot.mongodb.excel.services.ExcelFileServices; 12 | 13 | @RestController 14 | public class DownloadFileRestAPIs { 15 | 16 | @Autowired 17 | ExcelFileServices fileServices; 18 | 19 | /* 20 | * Download Files 21 | */ 22 | @GetMapping("/api/download/excel/") 23 | public ResponseEntity downloadFile() { 24 | 25 | HttpHeaders headers = new HttpHeaders(); 26 | headers.add("Content-Disposition", "attachment; filename=customers.xlsx"); 27 | 28 | return ResponseEntity.ok().headers(headers) 29 | .contentType(MediaType.parseMediaType("application/vnd.ms-excel")) 30 | .body(new InputStreamResource(fileServices.loadFile())); 31 | } 32 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/document/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.document; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | @Document(collection = "customers") 12 | public class Customer { 13 | 14 | @Id 15 | private Long id; 16 | 17 | private String name; 18 | 19 | private String address; 20 | 21 | private int age; 22 | 23 | public Customer() {} 24 | 25 | public Customer(Long id, String name, String address, int age) { 26 | this.id = id; 27 | this.name = name; 28 | this.address = address; 29 | this.age = age; 30 | } 31 | 32 | public Long getId() { 33 | return id; 34 | } 35 | 36 | public void setId(Long id) { 37 | this.id = id; 38 | } 39 | 40 | public String getName() { 41 | return name; 42 | } 43 | 44 | public void setName(String name) { 45 | this.name = name; 46 | } 47 | 48 | public String getAddress() { 49 | return address; 50 | } 51 | 52 | public void setAddress(String address) { 53 | this.address = address; 54 | } 55 | 56 | public int getAge() { 57 | return age; 58 | } 59 | 60 | public void setAge(int age) { 61 | this.age = age; 62 | } 63 | 64 | @Override 65 | public String toString() { 66 | return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"; 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/errorhandler/Error.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.errorhandler; 2 | 3 | public class Error { 4 | private String errCode; 5 | private String errDesc; 6 | 7 | public Error(String errCode, String errDesc) { 8 | this.errCode = errCode; 9 | this.errDesc = errDesc; 10 | } 11 | 12 | public void setErrCode(String errCode) { 13 | this.errCode = errCode; 14 | } 15 | 16 | public String getErrCode() { 17 | return this.errCode; 18 | } 19 | 20 | public void setErrDesc(String errDesc) { 21 | this.errDesc = errDesc; 22 | } 23 | 24 | public String getErrDesc() { 25 | return this.errDesc; 26 | } 27 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/errorhandler/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.errorhandler; 2 | 3 | import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 10 | import org.springframework.web.multipart.MultipartException; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | @ControllerAdvice 14 | public class RestExceptionHandler extends ResponseEntityExceptionHandler { 15 | 16 | // Catch file size exceeded exception! 17 | @ExceptionHandler(MultipartException.class) 18 | @ResponseBody 19 | ResponseEntity handleControllerException(HttpServletRequest request, Throwable ex) { 20 | HttpStatus status = getStatus(request); 21 | return new ResponseEntity(new Error("0x123", ex.getMessage()), status); 22 | } 23 | 24 | private HttpStatus getStatus(HttpServletRequest request) { 25 | Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 26 | if (statusCode == null) { 27 | return HttpStatus.INTERNAL_SERVER_ERROR; 28 | } 29 | return HttpStatus.valueOf(statusCode); 30 | } 31 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/message/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/message/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class Message { 9 | private String filename; 10 | private String message; 11 | private String status; 12 | 13 | public Message(String filename, String message, String status) { 14 | this.filename = filename; 15 | this.message = message; 16 | this.status = status; 17 | } 18 | 19 | public void setFilename(String filename) { 20 | this.filename = filename; 21 | } 22 | 23 | public String getFilename() { 24 | return this.filename; 25 | } 26 | 27 | public void setMessage(String message) { 28 | this.message = message; 29 | } 30 | 31 | public String getMessage() { 32 | return this.message; 33 | } 34 | 35 | public void setStatus(String status) { 36 | this.status = status; 37 | } 38 | 39 | public String getStatus() { 40 | return this.status; 41 | } 42 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/message/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | 12 | public class Response { 13 | private List messages = null; 14 | private List fileInfos = null; 15 | private Error error = null; 16 | private String errStatus = ""; 17 | 18 | public Response() { 19 | this.messages = new ArrayList(); 20 | } 21 | 22 | public Response(List fileInfos) { 23 | this.fileInfos = fileInfos; 24 | } 25 | 26 | public Response(String errStatus, Error err) { 27 | this.errStatus = errStatus; 28 | this.error = err; 29 | } 30 | 31 | public void addFileInfo(FileInfo file) { 32 | this.fileInfos.add(file); 33 | } 34 | 35 | public List getFileInfos(){ 36 | return this.fileInfos; 37 | } 38 | 39 | public void setMessages(List messages) { 40 | this.messages = messages; 41 | } 42 | 43 | public List getMessages() { 44 | return this.messages; 45 | } 46 | 47 | public void addMessage(Message message) { 48 | this.messages.add(message); 49 | } 50 | 51 | public void setErrStatus(String status) { 52 | this.errStatus = status; 53 | } 54 | 55 | public String getErrStatus() { 56 | return this.errStatus; 57 | } 58 | 59 | public void setError(Error error) { 60 | this.error = error; 61 | } 62 | 63 | public Error getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.loizenai.springboot.mongodb.excel.document.Customer; 6 | 7 | public interface CustomerRepository extends CrudRepository{ 8 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/java/com/loizenai/springboot/mongodb/excel/services/ExcelFileServices.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel.services; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.io.IOException; 5 | import java.util.List; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.web.multipart.MultipartFile; 10 | 11 | import com.loizenai.springboot.mongodb.excel.document.Customer; 12 | import com.loizenai.springboot.mongodb.excel.repository.CustomerRepository; 13 | import com.loizenai.springboot.mongodb.excel.utils.ExcelUtils; 14 | 15 | @Service 16 | public class ExcelFileServices { 17 | 18 | @Autowired 19 | CustomerRepository customerRepository; 20 | 21 | // Store File Data to Database 22 | public void store(MultipartFile file) { 23 | try { 24 | List lstCustomers = ExcelUtils.parseExcelFile(file.getInputStream()); 25 | // Save Customers to DataBase 26 | customerRepository.saveAll(lstCustomers); 27 | } catch (IOException e) { 28 | throw new RuntimeException("FAIL! -> message = " + e.getMessage()); 29 | } 30 | } 31 | 32 | // Load Data to Excel File 33 | public ByteArrayInputStream loadFile() { 34 | List customers = (List) customerRepository.findAll(); 35 | 36 | try { 37 | ByteArrayInputStream in = ExcelUtils.customersToExcel(customers); 38 | return in; 39 | } catch (IOException e) {} 40 | 41 | return null; 42 | } 43 | } -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb+srv://loizenai:12345@cluster0.uhqpv.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /SpringBoot-Upload-Parse-Excel-MongoDB/src/test/java/com/loizenai/springboot/mongodb/excel/SpringBootUploadExcelMongoDbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.mongodb.excel; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUploadExcelMongoDbApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/README.md: -------------------------------------------------------------------------------- 1 | Spring Boot Tutorial Pagination, Filtering and Sorting RestAPIs Example | Spring JPA – Pageable & Sort object requests 2 | *** 3 | https://loizenai.com/spring-boot-tutorial-pagination-filtering-and-sorting-example 4 | 5 | When we have a large dataset and we want to present it to the user in smaller chunks, pagination and sorting is often helpful solution. So in the tutorial, I introduce how to use Spring JPA APIs of PagingAndSortingRepository to do the task with SpringBoot project example. 6 | 7 | Related post: 8 | 9 | 1. Ajax Pagination Filtering and Sorting with Bootstrap Table & SpringBoot RestAPI Example – JQuery Ajax Tutorial 10 | https://loizenai.com/jquery-ajax-pagination-filtering-sorting-tutorial-bootstrap-table-springboot-restapis-examples/ 11 | 12 | 2. Angular CRUD Application with SpringBoot and MySQL/PostgreSQL RestAPIs – Fullstack Angular 10-9-8 HttpClient Post/Get/Put/Delete 13 | https://loizenai.com/angular-crud-application-with-springboot-and-mysql-postgresql-restapis-fullstack-angular-httpclient-post-get-put-delete/ 14 | 15 | 3. Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 16 | https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 17 | 18 | 4. Spring Boot Tutorial Pagination, Filtering and Sorting RestAPIs Example | Spring JPA – Pageable & Sort object requests 19 | https://loizenai.com/spring-boot-tutorial-pagination-filtering-and-sorting-example 20 | 21 | 5. SpringBoot Upload Download Multiple Files with Rest API + Ajax Tutorial 22 | https://loizenai.com/springboot-upload-download-multiple-files-with-rest-api-ajax/ 23 | -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootPagingAndSorting 13 | 1.00 14 | SpringBootPagingAndSorting 15 | SpringBootPagingAndSorting 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | runtime 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.junit.vintage 43 | junit-vintage-engine 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/src/main/java/com/loizenai/springboot/pagingansorting/model/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.model; 2 | 3 | import java.util.List; 4 | 5 | public class Response { 6 | private List customers; 7 | private int totalPages; 8 | private int pageNumber; 9 | private int pageSize; 10 | 11 | public Response(){} 12 | 13 | public Response(List customers, int totalPages, 14 | int pageNumber, int pageSize) { 15 | this.customers = customers; 16 | this.totalPages = totalPages; 17 | this.pageNumber = pageNumber; 18 | this.pageSize = pageSize; 19 | } 20 | 21 | public void setCustomers(List customers) { 22 | this.customers = customers; 23 | } 24 | 25 | public List getCustomers() { 26 | return this.customers; 27 | } 28 | 29 | public void setTotalPages(int totalPages) { 30 | this.totalPages = totalPages; 31 | } 32 | 33 | public int getTotalPages() { 34 | return this.totalPages; 35 | } 36 | 37 | public void setPageNumber(int pageNumber) { 38 | this.pageNumber = pageNumber; 39 | } 40 | 41 | public int getPageNumber() { 42 | return this.pageNumber; 43 | } 44 | 45 | public void setPageSize(int pageSize) { 46 | this.pageSize = pageSize; 47 | } 48 | 49 | public int getPageSize() { 50 | return this.pageSize; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/src/main/java/com/loizenai/springboot/pagingansorting/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.repository; 2 | 3 | import org.springframework.data.domain.Page; 4 | import org.springframework.data.domain.Pageable; 5 | import org.springframework.data.domain.Slice; 6 | import org.springframework.data.repository.PagingAndSortingRepository; 7 | import org.springframework.stereotype.Repository; 8 | 9 | import com.loizenai.springboot.pagingansorting.model.Customer; 10 | 11 | @Repository 12 | public interface CustomerRepository extends PagingAndSortingRepository{ 13 | Slice findAllBySalary (double salary, Pageable pageable); 14 | Page findAllByAgeGreaterThan(int age, Pageable pageable); 15 | } -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/src/main/resources/static/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Ajax Paging Table 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
18 |
19 |

Bootstrap + Ajax + SpringBoot Pagination

20 |
21 |

22 | @Copyright by 23 | https://loizenai.com 24 | 25 |
26 | youtube: 27 | loizenai 28 | 29 |

30 |
31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
IdFirstnameLastnameAgeSalaryAddressCopyright By
47 | 48 |
    49 |
50 |
51 |
52 |
53 | 54 | -------------------------------------------------------------------------------- /SpringBootPaginationFilteringSortingRestAPIs/src/test/java/com/loizenai/springboot/pagingansorting/SpringBootPagingAndSortingApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootPagingAndSortingApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/README.md: -------------------------------------------------------------------------------- 1 | Articles: Ajax Pagination Filtering and Sorting with Bootstrap Table & SpringBoot RestAPI Example – JQuery Ajax Tutorial 2 | https://loizenai.com/jquery-ajax-pagination-filtering-sorting-tutorial-bootstrap-table-springboot-restapis-examples/ 3 | 4 | ------------------------- 5 | 6 | When we have a large dataset and we want to present it to the user in smaller chunks, pagination and sorting is often helpful solution. So in the tutorial, I introduce how to use JQuery Ajax and Bootstrap to build a table solution for pagination, filtering and sorting the fetching data with SpringBoot RestAPIs examples. 7 | 8 | Related posts: 9 | 10 | 1. Build Angular Table Pagination Filtering Sorting with SpringBoot RestAPIs + Bootstrap Example – Angular 10-9-8-6 Tutorial 11 | https://loizenai.com/build-angular-table-pagination-filtering-sorting-with-springboot-restapis-example-tutorial/ 12 | 13 | 2. Build SpringBoot CRUD Application – FullStack: Frontend (Bootstrap and Ajax) to Backend (SpringBoot and MySQL/PostgreSQL database) 14 | https://loizenai.com/build-springboot-crud-application-fullstack-frontend-bootstrap-and-ajax-to-backend-springboot-and-mysql-postgresql-database/ 15 | 16 | 3. Angular Spring Boot JWT Authentication Example – Angular 6, 8, 9 + Spring Security + MySQL/PostgreSQL 17 | https://loizenai.com/angular-spring-boot-jwt-authentication-example-angular-6-8-9-spring-security-mysql-postgresql/ 18 | -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.3.1.RELEASE 9 | 10 | 11 | com.loizenai.springboot 12 | SpringBootPagingAndSorting 13 | 1.00 14 | SpringBootPagingAndSorting 15 | SpringBootPagingAndSorting 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-data-jpa 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | mysql 32 | mysql-connector-java 33 | runtime 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | org.junit.vintage 43 | junit-vintage-engine 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/src/main/java/com/loizenai/springboot/pagingansorting/model/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.model; 2 | 3 | import java.util.List; 4 | 5 | public class Response { 6 | private List customers; 7 | private int totalPages; 8 | private int pageNumber; 9 | private int pageSize; 10 | 11 | public Response(){} 12 | 13 | public Response(List customers, int totalPages, 14 | int pageNumber, int pageSize) { 15 | this.customers = customers; 16 | this.totalPages = totalPages; 17 | this.pageNumber = pageNumber; 18 | this.pageSize = pageSize; 19 | } 20 | 21 | public void setCustomers(List customers) { 22 | this.customers = customers; 23 | } 24 | 25 | public List getCustomers() { 26 | return this.customers; 27 | } 28 | 29 | public void setTotalPages(int totalPages) { 30 | this.totalPages = totalPages; 31 | } 32 | 33 | public int getTotalPages() { 34 | return this.totalPages; 35 | } 36 | 37 | public void setPageNumber(int pageNumber) { 38 | this.pageNumber = pageNumber; 39 | } 40 | 41 | public int getPageNumber() { 42 | return this.pageNumber; 43 | } 44 | 45 | public void setPageSize(int pageSize) { 46 | this.pageSize = pageSize; 47 | } 48 | 49 | public int getPageSize() { 50 | return this.pageSize; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/src/main/java/com/loizenai/springboot/pagingansorting/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.data.domain.Page; 6 | import org.springframework.data.domain.Pageable; 7 | import org.springframework.data.jpa.repository.Query; 8 | import org.springframework.data.repository.PagingAndSortingRepository; 9 | import org.springframework.stereotype.Repository; 10 | 11 | import com.loizenai.springboot.pagingansorting.model.Customer; 12 | 13 | @Repository 14 | public interface CustomerRepository extends PagingAndSortingRepository{ 15 | Page findAllBySalary (double salary, Pageable pageable); 16 | 17 | @Query("SELECT DISTINCT c.salary FROM Customer c") 18 | List findDistinctSalary(); 19 | } -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.datasource.url=jdbc:mysql://localhost:3306/loizenaidb 2 | spring.datasource.username=root 3 | spring.datasource.password=12345 4 | spring.jpa.generate-ddl=true 5 | 6 | #drop & create table again, good for testing, comment this in production 7 | spring.jpa.hibernate.ddl-auto=create -------------------------------------------------------------------------------- /SpringBootPagingAndSorting/src/test/java/com/loizenai/springboot/pagingansorting/SpringBootPagingAndSortingApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.pagingansorting; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootPagingAndSortingApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/SpringBootUploadCsvMongoDbApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringBootUploadCsvMongoDbApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringBootUploadCsvMongoDbApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/controller/DownloadCsvRestApi.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.controller; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.servlet.http.HttpServletResponse; 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.GetMapping; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | import com.loizenai.springboot.uploadcsv.mongodb.service.CsvFileServices; 12 | 13 | /** 14 | * Copyright https://loizenai.com 15 | * @author loizenai.com 16 | * 17 | */ 18 | @RestController 19 | public class DownloadCsvRestApi { 20 | 21 | @Autowired 22 | CsvFileServices csvFileService; 23 | 24 | /* 25 | * Download CSV Files 26 | */ 27 | @GetMapping("/api/download/csv/") 28 | public void downloadFile(HttpServletResponse response) throws IOException { 29 | response.setContentType("text/csv"); 30 | response.setHeader("Content-Disposition", "attachment; filename=customers.csv"); 31 | csvFileService.loadFile(response.getWriter()); 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/document/Customer.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.document; 2 | 3 | import org.springframework.data.annotation.Id; 4 | import org.springframework.data.mongodb.core.mapping.Document; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | @Document(collection = "customers") 12 | public class Customer { 13 | 14 | @Id 15 | private Long id; 16 | 17 | private String name; 18 | 19 | private String address; 20 | 21 | private int age; 22 | 23 | public Customer(Long id, String name, String address, int age) { 24 | this.id = id; 25 | this.name = name; 26 | this.address = address; 27 | this.age = age; 28 | } 29 | 30 | public Long getId() { 31 | return id; 32 | } 33 | 34 | public void setId(Long 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 getAddress() { 47 | return address; 48 | } 49 | 50 | public void setAddress(String address) { 51 | this.address = address; 52 | } 53 | 54 | public int getAge() { 55 | return age; 56 | } 57 | 58 | public void setAge(int age) { 59 | this.age = age; 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"; 65 | } 66 | 67 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/errorhandler/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.errorhandler; 2 | 3 | import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 10 | import org.springframework.web.multipart.MultipartException; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | import com.loizenai.springboot.uploadcsv.mongodb.message.Error; 14 | import com.loizenai.springboot.uploadcsv.mongodb.message.Response; 15 | 16 | /** 17 | * 18 | * Copyright by https://loizenai.com 19 | * @author loizenai.com 20 | * 21 | */ 22 | @ControllerAdvice 23 | public class RestExceptionHandler extends ResponseEntityExceptionHandler { 24 | 25 | // Catch file size exceeded exception! 26 | @SuppressWarnings({ "rawtypes", "unchecked" }) 27 | @ExceptionHandler(MultipartException.class) 28 | @ResponseBody 29 | ResponseEntity handleControllerException(HttpServletRequest request, Throwable ex) { 30 | HttpStatus status = getStatus(request); 31 | Error err = new Error("0x123", ex.getMessage()); 32 | Response res = new Response("error", err); 33 | 34 | return new ResponseEntity(res, status); 35 | } 36 | 37 | private HttpStatus getStatus(HttpServletRequest request) { 38 | Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 39 | if (statusCode == null) { 40 | return HttpStatus.INTERNAL_SERVER_ERROR; 41 | } 42 | return HttpStatus.valueOf(statusCode); 43 | } 44 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/message/Error.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.message; 2 | 3 | public class Error { 4 | private String errCode; 5 | private String errDesc; 6 | 7 | public Error(String errCode, String errDesc) { 8 | this.errCode = errCode; 9 | this.errDesc = errDesc; 10 | } 11 | 12 | public void setErrCode(String errCode) { 13 | this.errCode = errCode; 14 | } 15 | 16 | public String getErrCode() { 17 | return this.errCode; 18 | } 19 | 20 | public void setErrDesc(String errDesc) { 21 | this.errDesc = errDesc; 22 | } 23 | 24 | public String getErrDesc() { 25 | return this.errDesc; 26 | } 27 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/message/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/message/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class Message { 9 | private String filename; 10 | private String message; 11 | private String status; 12 | 13 | public Message(String filename, String message, String status) { 14 | this.filename = filename; 15 | this.message = message; 16 | this.status = status; 17 | } 18 | 19 | public void setFilename(String filename) { 20 | this.filename = filename; 21 | } 22 | 23 | public String getFilename() { 24 | return this.filename; 25 | } 26 | 27 | public void setMessage(String message) { 28 | this.message = message; 29 | } 30 | 31 | public String getMessage() { 32 | return this.message; 33 | } 34 | 35 | public void setStatus(String status) { 36 | this.status = status; 37 | } 38 | 39 | public String getStatus() { 40 | return this.status; 41 | } 42 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/message/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | 12 | public class Response { 13 | private List messages = null; 14 | private List fileInfos = null; 15 | private Error error = null; 16 | private String errStatus = ""; 17 | 18 | public Response() { 19 | this.messages = new ArrayList(); 20 | } 21 | 22 | public Response(List fileInfos) { 23 | this.fileInfos = fileInfos; 24 | } 25 | 26 | public Response(String errStatus, Error err) { 27 | this.errStatus = errStatus; 28 | this.error = err; 29 | } 30 | 31 | public void addFileInfo(FileInfo file) { 32 | this.fileInfos.add(file); 33 | } 34 | 35 | public List getFileInfos(){ 36 | return this.fileInfos; 37 | } 38 | 39 | public void setMessages(List messages) { 40 | this.messages = messages; 41 | } 42 | 43 | public List getMessages() { 44 | return this.messages; 45 | } 46 | 47 | public void addMessage(Message message) { 48 | this.messages.add(message); 49 | } 50 | 51 | public void setErrStatus(String status) { 52 | this.errStatus = status; 53 | } 54 | 55 | public String getErrStatus() { 56 | return this.errStatus; 57 | } 58 | 59 | public void setError(Error error) { 60 | this.error = error; 61 | } 62 | 63 | public Error getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/java/com/loizenai/springboot/uploadcsv/mongodb/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb.repository; 2 | 3 | import org.springframework.data.mongodb.repository.MongoRepository; 4 | 5 | import com.loizenai.springboot.uploadcsv.mongodb.document.Customer; 6 | 7 | public interface CustomerRepository extends MongoRepository{ 8 | } -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb+srv://loizenai:12345@cluster0.uhqpv.mongodb.net/loizenaidb?retryWrites=true&w=majority -------------------------------------------------------------------------------- /SpringBootUploadCsvMongoDB/src/test/java/com/loizenai/springboot/uploadcsv/mongodb/SpringBootUploadCsvMongoDbApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.springboot.uploadcsv.mongodb; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUploadCsvMongoDbApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.6.RELEASE 9 | 10 | 11 | com.loizenai 12 | SpringBootUploadDownloadMultipartFile 13 | 1 14 | SpringBootUploadDownloadMultipartFile 15 | SpringBootUploadDownloadMultipartFile 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-thymeleaf 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | org.junit.vintage 38 | junit-vintage-engine 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/java/com/loizenai/uploaddownloadfiles/SpringBootUploadDownloadMultipartFileApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import javax.annotation.Resource; 4 | 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 10 | 11 | @SpringBootApplication 12 | public class SpringBootUploadDownloadMultipartFileApplication implements CommandLineRunner { 13 | 14 | @Resource 15 | FileStorage fileStorage; 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(SpringBootUploadDownloadMultipartFileApplication.class, args); 19 | } 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | fileStorage.deleteAll(); 24 | fileStorage.init(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/java/com/loizenai/uploaddownloadfiles/controller/UploadMultipleFileController.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Controller; 8 | import org.springframework.ui.Model; 9 | import org.springframework.web.bind.annotation.GetMapping; 10 | import org.springframework.web.bind.annotation.PostMapping; 11 | import org.springframework.web.bind.annotation.RequestParam; 12 | import org.springframework.web.multipart.MultipartFile; 13 | 14 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 15 | 16 | /** 17 | * Copyright by https://loizenai.com 18 | * @author loizenai.com 19 | * 20 | */ 21 | 22 | @Controller 23 | public class UploadMultipleFileController { 24 | 25 | @Autowired 26 | FileStorage fileStorage; 27 | 28 | @GetMapping("/") 29 | public String index() { 30 | return "uploadmultiplefile.html"; 31 | } 32 | 33 | @PostMapping("/") 34 | public String uploadMultipartFile(@RequestParam("uploadfile") MultipartFile[] files, Model model) { 35 | List messages = new ArrayList(); 36 | 37 | for(MultipartFile file: files) { 38 | try { 39 | if(file.getOriginalFilename().isEmpty()) { 40 | messages.add("Fail! -> Empty File Name"); 41 | continue; 42 | } 43 | fileStorage.store(file); 44 | messages.add("Successfully! -> upload filename: " + file.getOriginalFilename()); 45 | } catch (Exception e) { 46 | messages.add("Fail! -> Existed Uploaded Filename: " + file.getOriginalFilename()); 47 | } 48 | } 49 | model.addAttribute("messages", messages); 50 | 51 | return "uploadmultiplefile.html"; 52 | } 53 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/java/com/loizenai/uploaddownloadfiles/controller/UploadSingleFileController.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.PostMapping; 8 | import org.springframework.web.bind.annotation.RequestParam; 9 | import org.springframework.web.multipart.MultipartFile; 10 | 11 | import com.loizenai.uploaddownloadfiles.service.FileStorage; 12 | 13 | @Controller 14 | public class UploadSingleFileController { 15 | 16 | @Autowired 17 | FileStorage fileStorage; 18 | 19 | @GetMapping("/uploadsinglefile") 20 | public String index() { 21 | return "uploadsinglefile.html"; 22 | } 23 | 24 | @PostMapping("/uploadsinglefile") 25 | public String uploadSingleFile(@RequestParam("uploadfile") MultipartFile file, Model model) { 26 | 27 | if(file.getOriginalFilename().isEmpty()) { 28 | model.addAttribute("message", "Fail -> Upload filename is empty! Please check it!"); 29 | return "uploadsinglefile.html"; 30 | } 31 | 32 | try { 33 | fileStorage.store(file); 34 | model.addAttribute("message", "Successfully! -> upload filename: " + file.getOriginalFilename()); 35 | } catch (Exception e) { 36 | model.addAttribute("message", "Fail! -> Existed Uploaded Filename: " + file.getOriginalFilename()); 37 | } 38 | 39 | return "uploadsinglefile.html"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/java/com/loizenai/uploaddownloadfiles/info/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.info; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/java/com/loizenai/uploaddownloadfiles/service/FileStorage.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.service; 2 | 3 | import java.nio.file.Path; 4 | import java.util.stream.Stream; 5 | 6 | import org.springframework.core.io.Resource; 7 | import org.springframework.web.multipart.MultipartFile; 8 | 9 | /** 10 | * Copyright by https://loizenai.com 11 | * @author loizenai.com 12 | * 13 | */ 14 | 15 | public interface FileStorage { 16 | public void store(MultipartFile file); 17 | 18 | public Resource loadFile(String filename); 19 | 20 | public void deleteAll(); 21 | 22 | public void init(); 23 | 24 | public Stream getFiles(); 25 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.http.multipart.max-file-size=10Mb 2 | spring.http.multipart.max-request-size=10Mb -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/resources/templates/files.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Uploaded Files 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
19 |

Uploaded Files

20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 |
NoFilenameDownload
1FilenameLink
36 | 37 | 41 |
42 |
43 |
44 | 45 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/main/resources/templates/uploadsinglefile.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Upload Files 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
19 |

Upload File to SpringBoot Backend

20 |
21 |
22 | 23 | 24 |
25 | 26 | Files 27 |
28 |
29 |
30 |
32 |
34 | 35 |
36 |
37 |
38 | 39 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipartFile/src/test/java/com/loizenai/uploaddownloadfiles/SpringBootUploadDownloadMultipartFileApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUploadDownloadMultipartFileApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/README.md: -------------------------------------------------------------------------------- 1 | SpringBoot Upload Download Multiple Files with Rest API + Ajax Tutorial 2 | *** 3 | Link: https://loizenai.com/springboot-upload-download-multiple-files-with-rest-api-ajax/ 4 | 5 | Related posts: 6 | *** 7 | 8 | 1. Spring Boot Security JWT Authentication Example – MySQL/PostgreSQL + Spring JPA + RestAPIs 9 | https://loizenai.com/spring-boot-security-jwt-authentication-example-mysql-postgresql-spring-jpa-restapis/ 10 | 11 | 2. Angular Spring Boot JWT Authentication Example – Angular 6, 8, 9 + Spring Security + MySQL/PostgreSQL 12 | https://loizenai.com/angular-spring-boot-jwt-authentication-example-angular-6-8-9-spring-security-mysql-postgresql/ 13 | 14 | 3. SpringBoot RestAPIs Upload Download Multiple CSV files to MySQL/PostgreSQL with Ajax + RestClient 15 | https://loizenai.com/springboot-restapi-upload-download-multiple-csv-files-to-mysql-postgresql-database-with-ajax-restclient/ 16 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.2.7.RELEASE 9 | 10 | 11 | com.loizenai 12 | SpringBootUploadFilesAjax 13 | 1 14 | SpringBootUploadFilesAjax 15 | SpringBootUploadFilesAjax 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | org.junit.vintage 34 | junit-vintage-engine 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/SpringBootUploadFilesAjaxApplication.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import javax.annotation.Resource; 4 | 5 | import org.springframework.boot.CommandLineRunner; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | 9 | import com.loizenai.uploaddownloadfiles.service.FileService; 10 | 11 | @SpringBootApplication 12 | public class SpringBootUploadFilesAjaxApplication implements CommandLineRunner { 13 | 14 | @Resource 15 | FileService fileService; 16 | 17 | public static void main(String[] args) { 18 | SpringApplication.run(SpringBootUploadFilesAjaxApplication.class, args); 19 | } 20 | 21 | @Override 22 | public void run(String... args) throws Exception { 23 | fileService.deleteAll(); 24 | fileService.init(); 25 | } 26 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/errorhandler/RestExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.errorhandler; 2 | 3 | import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 10 | import org.springframework.web.multipart.MultipartException; 11 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 12 | 13 | import com.loizenai.uploaddownloadfiles.message.Error; 14 | import com.loizenai.uploaddownloadfiles.message.Response; 15 | 16 | //@ControllerAdvice 17 | public class RestExceptionHandler extends ResponseEntityExceptionHandler { 18 | 19 | // Catch file size exceeded exception! 20 | @SuppressWarnings({ "rawtypes", "unchecked" }) 21 | @ExceptionHandler(MultipartException.class) 22 | @ResponseBody 23 | ResponseEntity handleControllerException(HttpServletRequest request, Throwable ex) { 24 | HttpStatus status = getStatus(request); 25 | return new ResponseEntity(new Response("error", new Error("0x123", ex.getMessage())), status); 26 | } 27 | 28 | private HttpStatus getStatus(HttpServletRequest request) { 29 | Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); 30 | if (statusCode == null) { 31 | return HttpStatus.INTERNAL_SERVER_ERROR; 32 | } 33 | return HttpStatus.valueOf(statusCode); 34 | } 35 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/message/Error.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.message; 2 | 3 | public class Error { 4 | private String errCode; 5 | private String errDesc; 6 | 7 | public Error(String errCode, String errDesc) { 8 | this.errCode = errCode; 9 | this.errDesc = errDesc; 10 | } 11 | 12 | public void setErrCode(String errCode) { 13 | this.errCode = errCode; 14 | } 15 | 16 | public String getErrCode() { 17 | return this.errCode; 18 | } 19 | 20 | public void setErrDesc(String errDesc) { 21 | this.errDesc = errDesc; 22 | } 23 | 24 | public String getErrDesc() { 25 | return this.errDesc; 26 | } 27 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/message/FileInfo.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | public class FileInfo { 9 | 10 | private String filename; 11 | private String url; 12 | 13 | public FileInfo(String filename, String url) { 14 | this.filename = filename; 15 | this.url = url; 16 | } 17 | 18 | public void setFilename(String filename) { 19 | this.filename = filename; 20 | } 21 | 22 | public String getFilename() { 23 | return this.filename; 24 | } 25 | 26 | public void setUrl(String url) { 27 | this.url = url; 28 | } 29 | 30 | public String getUrl() { 31 | return this.url; 32 | } 33 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/message/Message.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.message; 2 | 3 | /** 4 | * Copyright by https://loizenai.com 5 | * @author loizenai.com 6 | * 7 | */ 8 | 9 | public class Message { 10 | private String filename; 11 | private String message; 12 | private String status; 13 | 14 | public Message(String filename, String message, String status) { 15 | this.filename = filename; 16 | this.message = message; 17 | this.status = status; 18 | } 19 | 20 | public void setFilename(String filename) { 21 | this.filename = filename; 22 | } 23 | 24 | public String getFilename() { 25 | return this.filename; 26 | } 27 | 28 | public void setMessage(String message) { 29 | this.message = message; 30 | } 31 | 32 | public String getMessage() { 33 | return this.message; 34 | } 35 | 36 | public void setStatus(String status) { 37 | this.status = status; 38 | } 39 | 40 | public String getStatus() { 41 | return this.status; 42 | } 43 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/message/Response.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.message; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Copyright by https://loizenai.com 8 | * @author loizenai.com 9 | * 10 | */ 11 | 12 | public class Response { 13 | private List messages = null; 14 | private List fileInfos = null; 15 | private Error error = null; 16 | private String errStatus = ""; 17 | 18 | public Response() { 19 | this.messages = new ArrayList(); 20 | } 21 | 22 | public Response(List fileInfos) { 23 | this.fileInfos = fileInfos; 24 | } 25 | 26 | public Response(String errStatus, Error err) { 27 | this.errStatus = errStatus; 28 | this.error = err; 29 | } 30 | 31 | public void addFileInfo(FileInfo file) { 32 | this.fileInfos.add(file); 33 | } 34 | 35 | public List getFileInfos(){ 36 | return this.fileInfos; 37 | } 38 | 39 | public void setMessages(List messages) { 40 | this.messages = messages; 41 | } 42 | 43 | public List getMessages() { 44 | return this.messages; 45 | } 46 | 47 | public void addMessage(Message message) { 48 | this.messages.add(message); 49 | } 50 | 51 | public void setErrStatus(String status) { 52 | this.errStatus = status; 53 | } 54 | 55 | public String getErrStatus() { 56 | return this.errStatus; 57 | } 58 | 59 | public void setError(Error error) { 60 | this.error = error; 61 | } 62 | 63 | public Error getError() { 64 | return this.error; 65 | } 66 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/java/com/loizenai/uploaddownloadfiles/service/FileService.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles.service; 2 | 3 | import java.nio.file.Path; 4 | import java.util.stream.Stream; 5 | 6 | import org.springframework.core.io.Resource; 7 | import org.springframework.web.multipart.MultipartFile; 8 | 9 | 10 | /** 11 | * Copyright by https://loizenai.com 12 | * @author loizenai.com 13 | * 14 | */ 15 | 16 | public interface FileService { 17 | public void store(MultipartFile file); 18 | 19 | public Resource loadFile(String filename); 20 | 21 | public void deleteAll(); 22 | 23 | public void init(); 24 | 25 | public Stream getFiles(); 26 | } -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /SpringBootUploadDownloadMultipleFilesAjax/src/test/java/com/loizenai/uploaddownloadfiles/SpringBootUploadFilesAjaxApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.loizenai.uploaddownloadfiles; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringBootUploadFilesAjaxApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | --------------------------------------------------------------------------------