├── docker.png ├── swagger.png ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ ├── maven-wrapper.properties │ └── MavenWrapperDownloader.java ├── Dockerfile ├── src ├── main │ ├── resources │ │ ├── application-dev.properties │ │ └── application.properties │ └── java │ │ └── com │ │ └── rasmivan │ │ └── showcase │ │ ├── exception │ │ ├── UserCreateUpdateException.java │ │ ├── UserUpdateDtoEmptyException.java │ │ └── UserDoesnotExistsException.java │ │ ├── constants │ │ ├── GeneralConstantsUtils.java │ │ └── MessageConstantsUtils.java │ │ ├── ShowcaseApplication.java │ │ ├── repositories │ │ └── UserRepository.java │ │ ├── security │ │ └── SecurityConfig.java │ │ ├── service │ │ ├── UserService.java │ │ └── UserServiceImp.java │ │ ├── advice │ │ └── ExceptionControllerAdvice.java │ │ ├── config │ │ └── SwaggerConfig.java │ │ ├── controller │ │ └── UserController.java │ │ ├── domain │ │ └── User.java │ │ └── dto │ │ └── UserDto.java └── test │ └── java │ └── com │ └── rasmivan │ └── showcase │ ├── ShowcaseApplicationTests.java │ └── controller │ └── UserControllerTest.java ├── .gitignore ├── README.md ├── pom.xml ├── mvnw.cmd └── mvnw /docker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramongaldon/spring-boot-mongodb/HEAD/docker.png -------------------------------------------------------------------------------- /swagger.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramongaldon/spring-boot-mongodb/HEAD/swagger.png -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ramongaldon/spring-boot-mongodb/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:8-jdk-alpine 2 | VOLUME /tmp 3 | COPY target/*.jar app.jar 4 | ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] -------------------------------------------------------------------------------- /src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb://dummy:j3xsgXbyUXPI7bx9@fitnessappcluster-shard-00-00-zf0xz.mongodb.net:27017,fitnessappcluster-shard-00-01-zf0xz.mongodb.net:27017,fitnessappcluster-shard-00-02-zf0xz.mongodb.net:27017/test?ssl=true&replicaSet=fitnessappcluster-shard-0&authSource=admin&retryWrites=true 2 | spring.data.mongodb.database=userdetails 3 | spring.security.user.name=user 4 | spring.security.user.password=pwd -------------------------------------------------------------------------------- /src/test/java/com/rasmivan/showcase/ShowcaseApplicationTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase; 5 | 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | @RunWith(SpringRunner.class) 12 | @SpringBootTest 13 | public class ShowcaseApplicationTests { 14 | 15 | @Test 16 | public void contextLoads() { 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/exception/UserCreateUpdateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.exception; 5 | 6 | 7 | /** 8 | * The Class UserCreateUpdateException. 9 | * @author Rasmivan Ilangovan 10 | */ 11 | public class UserCreateUpdateException extends RuntimeException{ 12 | 13 | /** The Constant serialVersionUID. */ 14 | private static final long serialVersionUID = 1515270427760297412L; 15 | 16 | /** 17 | * Instantiates a new user create update exception. 18 | * 19 | * @param message the message 20 | */ 21 | public UserCreateUpdateException(String message) { 22 | super(message); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/exception/UserUpdateDtoEmptyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.exception; 5 | 6 | /** 7 | * The Class UserUpdateDtoException. 8 | * @author Rasmivan Ilangovan 9 | */ 10 | public class UserUpdateDtoEmptyException extends RuntimeException{ 11 | 12 | 13 | /** The Constant serialVersionUID. */ 14 | private static final long serialVersionUID = 7010878285804404672L; 15 | 16 | /** 17 | * Instantiates a new user update dto exception. 18 | * 19 | * @param message the message 20 | */ 21 | public UserUpdateDtoEmptyException(String message) { 22 | super(message); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/exception/UserDoesnotExistsException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.exception; 5 | 6 | 7 | /** 8 | * The Class UserCreatingException. 9 | * @author Rasmivan Ilangovan 10 | */ 11 | public class UserDoesnotExistsException extends RuntimeException{ 12 | 13 | 14 | 15 | /** The Constant serialVersionUID. */ 16 | private static final long serialVersionUID = 7010878285804404672L; 17 | 18 | /** 19 | * Instantiates a new user creating exception. 20 | * 21 | * @param message the message 22 | */ 23 | public UserDoesnotExistsException(String message) { 24 | super(message); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/constants/GeneralConstantsUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.constants; 5 | 6 | 7 | /** 8 | * The Class GeneralConstant. 9 | * @author Rasmivan Ilangovan 10 | * 11 | */ 12 | public final class GeneralConstantsUtils{ 13 | 14 | /** The Constant MEDIA_TYPE. */ 15 | public static final String MEDIA_TYPE = "application"; 16 | 17 | /** The Constant TYPE_JSON. */ 18 | public static final String TYPE_JSON = "json"; 19 | 20 | /** The Constant TYPE_STREAM. */ 21 | public static final String TYPE_STREAM = "octet-stream"; 22 | 23 | /** The Constant FOR_NAME. */ 24 | public static final String FOR_NAME = "UTF-8"; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/ShowcaseApplication.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase; 5 | 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 9 | 10 | /** 11 | * The Class ShowcaseApplication. 12 | * @author Rasmivan Ilangovan 13 | */ 14 | @SpringBootApplication 15 | @EnableWebSecurity 16 | public class ShowcaseApplication { 17 | 18 | /** 19 | * The main method. 20 | * 21 | * @param args the arguments 22 | */ 23 | public static void main(String[] args) { 24 | SpringApplication.run(ShowcaseApplication.class, args); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.data.mongodb.uri=mongodb://dummy:j3xsgXbyUXPI7bx9@fitnessappcluster-shard-00-00-zf0xz.mongodb.net:27017,fitnessappcluster-shard-00-01-zf0xz.mongodb.net:27017,fitnessappcluster-shard-00-02-zf0xz.mongodb.net:27017/test?ssl=true&replicaSet=fitnessappcluster-shard-0&authSource=admin&retryWrites=true 2 | spring.data.mongodb.database=userdetails 3 | spring.security.user.name=user 4 | spring.security.user.password=password 5 | swagger.api.version=1.0 6 | project.organization.name=Rasmivan Ilangovan 7 | project.organization.url=https://rasmivan.com 8 | project.title=ShowcaseSpring 9 | project.description=This Project is to showcase my understanding on Spring and Mango DB 10 | project.contact.emailid=rasmivancse@gmail.com 11 | project.license=Apache 2.0 12 | project.license.url=https://www.apache.org/licenses/LICENSE-2.0 -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/repositories/UserRepository.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.repositories; 5 | 6 | import java.util.Optional; 7 | 8 | import org.springframework.data.mongodb.repository.MongoRepository; 9 | 10 | import com.rasmivan.showcase.domain.User; 11 | 12 | /** 13 | * The Interface UserRepository. 14 | * @author Rasmivan Ilangovan 15 | */ 16 | public interface UserRepository extends MongoRepository{ 17 | 18 | 19 | /* (non-Javadoc) 20 | * @see org.springframework.data.repository.CrudRepository#findById(java.lang.Object) 21 | */ 22 | public Optional findById(String id); 23 | 24 | /* (non-Javadoc) 25 | * @see org.springframework.data.repository.CrudRepository#findByFirstName(java.lang.Object) 26 | */ 27 | public Optional findByFirstName(String firstName); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | 26 | 27 | ########## Sprint 28 | HELP.md 29 | /target/ 30 | !.mvn/wrapper/maven-wrapper.jar 31 | 32 | ### STS ### 33 | .apt_generated 34 | .classpath 35 | .factorypath 36 | .project 37 | .settings 38 | .springBeans 39 | .sts4-cache 40 | 41 | ### IntelliJ IDEA ### 42 | .idea 43 | *.iws 44 | *.iml 45 | *.ipr 46 | 47 | ### NetBeans ### 48 | /nbproject/private/ 49 | /nbbuild/ 50 | /dist/ 51 | /nbdist/ 52 | /.nb-gradle/ 53 | /build/ 54 | 55 | ### VS Code ### 56 | .vscode/ 57 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/constants/MessageConstantsUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This Class has all the static message which will be displayed to the user. 3 | */ 4 | package com.rasmivan.showcase.constants; 5 | 6 | /** 7 | * The Class MessageConstant. 8 | * @author Rasmivan Ilangovan 9 | * 10 | */ 11 | public final class MessageConstantsUtils { 12 | 13 | /** The Constant USERNOT_FOUND. */ 14 | public static final String USERNOT_FOUND = "User Does not Exists"; 15 | 16 | 17 | /** The Constant USERUPDATE_EMPTY. */ 18 | public static final String USERUPDATE_EMPTY = "User DTO is Empty"; 19 | 20 | 21 | /** The Constant USER_NOT_CREATED. */ 22 | public static final String USER_NOT_CREATED = "User Not Created"; 23 | 24 | 25 | /** The Constant USER_NOT_UPDATE. */ 26 | public static final String USER_NOT_UPDATE = "User Not Updated"; 27 | 28 | 29 | /** The Constant USER_DELETED_SUCCESSFULLY. */ 30 | public static final String USER_DELETED_SUCCESSFULLY = "Deleted Successfully"; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/security/SecurityConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.security; 5 | 6 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; 7 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 8 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 9 | 10 | /** 11 | * The Class SecurityConfig. 12 | * @author Rasmivan Ilangovan 13 | */ 14 | @EnableWebSecurity 15 | public class SecurityConfig extends WebSecurityConfigurerAdapter { 16 | 17 | /* 18 | * (non-Javadoc) 19 | * 20 | * @see org.springframework.security.config.annotation.web.configuration. 21 | * WebSecurityConfigurerAdapter#configure(org.springframework.security. 22 | * config.annotation.web.builders.HttpSecurity) 23 | */ 24 | @Override 25 | protected void configure(HttpSecurity http) throws Exception { 26 | http.httpBasic().and().authorizeRequests().antMatchers("/showcase/api/**").fullyAuthenticated().and().csrf() 27 | .disable().headers().frameOptions().disable(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/test/java/com/rasmivan/showcase/controller/UserControllerTest.java: -------------------------------------------------------------------------------- 1 | package com.rasmivan.showcase.controller; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.mockito.InjectMocks; 7 | import org.mockito.Mock; 8 | import org.mockito.MockitoAnnotations; 9 | import org.mockito.runners.MockitoJUnitRunner; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.http.ResponseEntity; 12 | import org.springframework.test.web.servlet.MockMvc; 13 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; 14 | 15 | import com.rasmivan.showcase.domain.User; 16 | import com.rasmivan.showcase.service.UserService; 17 | 18 | @RunWith(MockitoJUnitRunner.class) 19 | public class UserControllerTest { 20 | 21 | @Autowired 22 | private MockMvc mvc; 23 | 24 | @InjectMocks 25 | UserController userController; 26 | 27 | @Mock 28 | UserService userService; 29 | 30 | @Before 31 | public void initTests() { 32 | MockitoAnnotations.initMocks(this); 33 | } 34 | 35 | 36 | @Test 37 | public void BusinessSwitchOffExceptionTest() throws Exception { 38 | //mvc = MockMvcBuilders.standaloneSetup(userController).build(); 39 | ResponseEntity rep = userController.getUserById("12"); 40 | System.out.println(rep); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/service/UserService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This is an interface to our service. 3 | */ 4 | package com.rasmivan.showcase.service; 5 | 6 | import java.util.List; 7 | 8 | import com.rasmivan.showcase.domain.User; 9 | import com.rasmivan.showcase.dto.UserDto; 10 | 11 | /** 12 | * The Interface UserService. 13 | * @author Rasmivan Ilangovan 14 | * 15 | */ 16 | public interface UserService { 17 | 18 | /** 19 | * Gets the user by id. 20 | * 21 | * @param id the id 22 | * @return the user by id 23 | */ 24 | User getUserById(String id); 25 | 26 | /** 27 | * Gets the user by first name. 28 | * 29 | * @param firstName the first name 30 | * @return the user by first name 31 | */ 32 | User getUserByFirstName(String firstName); 33 | 34 | /** 35 | * Gets the all user. 36 | * 37 | * @return the all user 38 | */ 39 | List getAllUser(); 40 | 41 | /** 42 | * Insert or Update user. 43 | * 44 | * @param usr the usr 45 | * @return the user 46 | */ 47 | User addOrUpdateUser(UserDto usr); 48 | 49 | /** 50 | * Update user. 51 | * 52 | * @param usr the usr 53 | * @return the user 54 | */ 55 | User updateUser(UserDto usr); 56 | 57 | 58 | /** 59 | * Delete user by id. 60 | * 61 | * @param id the id 62 | * @return the string 63 | */ 64 | String deleteUserById(String id); 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/advice/ExceptionControllerAdvice.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This is an AOP for all the exception we throw from our service. 3 | */ 4 | package com.rasmivan.showcase.advice; 5 | 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.ControllerAdvice; 9 | import org.springframework.web.bind.annotation.ExceptionHandler; 10 | 11 | import com.rasmivan.showcase.exception.UserDoesnotExistsException; 12 | import com.rasmivan.showcase.exception.UserUpdateDtoEmptyException; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | /** 18 | * The Class ExceptionControllerAdvice. 19 | * @author Rasmivan Ilangovan 20 | */ 21 | @ControllerAdvice 22 | public class ExceptionControllerAdvice { 23 | 24 | /** The logger. */ 25 | private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionControllerAdvice.class); 26 | 27 | 28 | /** 29 | * Handle channel order exception. 30 | * 31 | * @param e the e 32 | * @return the response entity 33 | */ 34 | @ExceptionHandler(value = UserDoesnotExistsException.class) 35 | public ResponseEntity handleUserDoesNotExistsException(UserDoesnotExistsException e) { 36 | LOGGER.error(e.getMessage()); 37 | return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); 38 | } 39 | 40 | /** 41 | * Handle user update empty exception. 42 | * 43 | * @param e the e 44 | * @return the response entity 45 | */ 46 | @ExceptionHandler(value = UserUpdateDtoEmptyException.class) 47 | public ResponseEntity handleUserUpdateEmptyException(UserDoesnotExistsException e) { 48 | LOGGER.error(e.getMessage()); 49 | return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![](https://codescene.io/projects/5196/status.svg) Get more details at **codescene.io**.](https://codescene.io/projects/5196/jobs/latest-successful/results) 3 | 4 | # 5 | # CRUD MongoDB using Spring Boot 6 | 7 | This Application exposes endpoint to GET/POST/PATCH/DELETE into Cloud MongoDB. 8 | 9 | This project is built in Spring Boot and connect to Cloud MongoDB and allows you to get, add, update or delete User details. 10 | 11 | The project also have enabled SWAGGER to interact with the user. 12 | 13 | ## This Project covers below 14 | * Spring Boot 15 | * JPA connection to MongoDB 16 | * Control Advice 17 | * Basic Authorization 18 | * Swagger 19 | * Docker Build 20 | 21 | ## To DO 22 | 23 | * JUnit 24 | * Code Clean Up 25 | 26 | ## Running the application locally 27 | 28 | Below are the steps to run the scripts locally. 29 | 30 | $ git clone https://github.com/IRasmivan/spring-boot-mongodb 31 | $ cd spring-boot-mongodb 32 | $ mvn spring-boot:run 33 | 34 | The above steps should start the Spring Boot Application. 35 | 36 | Open your browser and navigate to the link which should openup Swagger endpoint for this application. 37 | 38 | URL: http://localhost:8080/swagger-ui.html#/user-controller 39 | 40 | ## Project Structure 41 | This project holds the below folder structure 42 | 43 | . 44 | . 45 | ├── src 46 | │ └── main 47 | │ └── java 48 | │ ├── com.rasmivan.showcase 49 | | ├── com.rasmivan.showcase.advice 50 | | ├── com.rasmivan.showcase.config 51 | │ ├── com.rasmivan.showcase.constants 52 | │ ├── com.rasmivan.showcase.controller 53 | │ ├── com.rasmivan.showcase.domain 54 | │ ├── com.rasmivan.showcase.dto 55 | │ ├── com.rasmivan.showcase.exception 56 | │ ├── com.rasmivan.showcase.repositories 57 | │ ├── com.rasmivan.showcase.security 58 | │ └── com.rasmivan.showcase.service 59 | . 60 | . 61 | 62 | 63 | ### Controller 64 | Spring Boot application exposes 6 endpoint below. 65 | 66 | # ![SwaggerController](swagger.png) 67 | 68 | 69 | ## Running as ![Docker container](https://img.icons8.com/color/50/000000/docker.png "Docker Container") 70 | 71 | I have built a docker image and the same is available in [dockerhub](https://cloud.docker.com/repository/docker/rasmivan/spring-mongo/general). Run the below comment to run the docker image as container. 72 | 73 | docker run -p 8080:8080 -t rasmivan/spring-mongo:1.1 74 | 75 | The above line should run the JAR inside a docker container and you should see a simular screen. 76 | # ![docker snapshot](docker.png) 77 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.rasmivan.showcase.config; 2 | 3 | import java.util.Collections; 4 | 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | import springfox.documentation.builders.PathSelectors; 10 | import springfox.documentation.service.ApiInfo; 11 | import springfox.documentation.service.Contact; 12 | import springfox.documentation.spi.DocumentationType; 13 | import springfox.documentation.spring.web.plugins.Docket; 14 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 15 | 16 | /** 17 | * The Class SwaggerConfig. 18 | * @author Rasmivan Ilangovan 19 | */ 20 | @Configuration 21 | @EnableSwagger2 22 | public class SwaggerConfig { 23 | 24 | 25 | /** The title. */ 26 | @Value("${project.title}") 27 | private String title = "ShowcaseSpring"; 28 | 29 | /** The description. */ 30 | @Value("${project.description}") 31 | private String description = "This Project is to showcase my understanding on Spring and Mango DB"; 32 | 33 | /** The contact email id. */ 34 | @Value("${project.contact.emailid}") 35 | private String contactEmailId = "rasmivancse@gmail.com"; 36 | 37 | /** The api version. */ 38 | @Value("${swagger.api.version}") 39 | private String apiVersion = "1.0"; 40 | 41 | /** The api contact name. */ 42 | @Value("${project.organization.name}") 43 | private String apiContactName = "Rasmivan Ilangovan"; 44 | 45 | /** The api contact url. */ 46 | @Value("${project.organization.url}") 47 | private String apiContactUrl = "https://rasmivan.com"; 48 | 49 | /** The license. */ 50 | @Value("${project.license}") 51 | private String license = "Apache 2.0"; 52 | 53 | /** The license url. */ 54 | @Value("${project.license.url}") 55 | private String licenseUrl = "https://www.apache.org/licenses/LICENSE-2.0"; 56 | 57 | /** 58 | * Api. 59 | * 60 | * @return the docket 61 | */ 62 | @Bean 63 | public Docket api() { 64 | return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfo( 65 | title, 66 | description, 67 | apiVersion, 68 | null, 69 | new Contact( 70 | apiContactName, 71 | apiContactUrl, 72 | contactEmailId 73 | ), 74 | license, 75 | licenseUrl, 76 | Collections.emptyList() 77 | )).select().paths(PathSelectors.regex("^/showcase/api/.*$")).build(); 78 | } 79 | } -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/controller/UserController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This Class has all the endpoints that the user can interact. 3 | */ 4 | package com.rasmivan.showcase.controller; 5 | 6 | import java.util.List; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.http.HttpStatus; 10 | import org.springframework.http.ResponseEntity; 11 | import org.springframework.web.bind.annotation.CrossOrigin; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestBody; 14 | import org.springframework.web.bind.annotation.RequestMapping; 15 | import org.springframework.web.bind.annotation.RequestMethod; 16 | import org.springframework.web.bind.annotation.RestController; 17 | 18 | import com.rasmivan.showcase.domain.User; 19 | import com.rasmivan.showcase.dto.UserDto; 20 | import com.rasmivan.showcase.service.UserService; 21 | 22 | 23 | /** 24 | * The Class UserController. 25 | * @author Rasmivan Ilangovan 26 | * 27 | */ 28 | @RestController 29 | @CrossOrigin 30 | @RequestMapping("/showcase/api/") 31 | public class UserController { 32 | 33 | /** The user service. */ 34 | @Autowired 35 | UserService userService; 36 | 37 | /** 38 | * Gets the user by id. 39 | * 40 | * @param id the id 41 | * @return the user by id 42 | */ 43 | @RequestMapping(value = "/id/{id}", method = RequestMethod.GET) 44 | public ResponseEntity getUserById(@PathVariable("id") String id) { 45 | return new ResponseEntity<>(userService.getUserById(id), HttpStatus.OK); 46 | } 47 | 48 | /** 49 | * Gets the user by first name. 50 | * 51 | * @param firstName the first name 52 | * @return the user by first name 53 | */ 54 | @RequestMapping(value = "/firstName/{firstName}", method = RequestMethod.GET) 55 | public ResponseEntity getUserByFirstName(@PathVariable("firstName") String firstName) { 56 | return new ResponseEntity<>(userService.getUserByFirstName(firstName), HttpStatus.OK); 57 | } 58 | 59 | 60 | /** 61 | * Gets the all users. 62 | * 63 | * @return the all users 64 | */ 65 | @RequestMapping(method = RequestMethod.GET) 66 | public ResponseEntity> getAllUsers() { 67 | return new ResponseEntity<>(userService.getAllUser(), HttpStatus.OK); 68 | } 69 | 70 | 71 | /** 72 | * Adds or update user. 73 | * 74 | * @param usr the usr 75 | * @return the response entity 76 | */ 77 | @RequestMapping(method = RequestMethod.POST) 78 | public ResponseEntity addOrUpdateUser(@RequestBody UserDto usr) { 79 | return new ResponseEntity<>(userService.addOrUpdateUser(usr), HttpStatus.OK); 80 | } 81 | 82 | 83 | 84 | /** 85 | * Update user. 86 | * 87 | * @param usr the usr 88 | * @return the response entity 89 | */ 90 | @RequestMapping(method = RequestMethod.PATCH) 91 | public ResponseEntity updateUser(@RequestBody UserDto usr) { 92 | return new ResponseEntity<>(userService.updateUser(usr), HttpStatus.OK); 93 | } 94 | 95 | 96 | @RequestMapping(value = "/id/{id}", method = RequestMethod.DELETE) 97 | public ResponseEntity deleteUserById(@PathVariable("id") String id) { 98 | return new ResponseEntity<>(userService.deleteUserById(id), HttpStatus.OK); 99 | } 100 | 101 | 102 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.4.RELEASE 9 | 10 | 11 | com.rasmivan 12 | showcase 13 | 0.0.1-SNAPSHOT 14 | showcase 15 | Spring Boot - CRUD Mango DB 16 | 17 | 18 | 1.8 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-starter-web 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-mongodb 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-test 35 | test 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-tomcat 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-security 49 | 50 | 51 | 52 | 53 | 54 | org.apache.httpcomponents 55 | httpclient 56 | 57 | 58 | 59 | 60 | 61 | org.springframework.boot 62 | spring-boot 63 | 2.1.4.RELEASE 64 | 65 | 66 | 67 | 68 | 69 | ch.qos.logback 70 | logback-access 71 | 0.9.26 72 | 73 | 74 | 75 | org.springframework.security 76 | spring-security-web 77 | 78 | 79 | org.springframework.security 80 | spring-security-config 81 | 82 | 83 | 84 | 85 | io.springfox 86 | springfox-swagger2 87 | 2.9.2 88 | 89 | 90 | 91 | 92 | io.springfox 93 | springfox-swagger-ui 94 | 2.6.1 95 | compile 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | org.springframework.boot 106 | spring-boot-maven-plugin 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/domain/User.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This User domain 3 | */ 4 | package com.rasmivan.showcase.domain; 5 | 6 | import java.util.Date; 7 | 8 | import org.springframework.data.annotation.Id; 9 | import org.springframework.data.mongodb.core.mapping.Document; 10 | 11 | /** 12 | * The Class User. 13 | * @author Rasmivan Ilangovan 14 | * This holds all the user information like 15 | * User First Name 16 | * User Last Name 17 | * User Email ID 18 | * User Age 19 | * User Date of Birth. 20 | */ 21 | @Document(collection = "user") 22 | public class User { 23 | 24 | 25 | 26 | /** The id. */ 27 | @Id 28 | private String id; 29 | 30 | /** The first name. */ 31 | private String firstName; 32 | 33 | /** The last name. */ 34 | private String lastName; 35 | 36 | /** The email id. */ 37 | private String emailId; 38 | 39 | /** The age. */ 40 | private int age; 41 | 42 | /** The date of birth. */ 43 | private Date dateOfBirth; 44 | 45 | /** 46 | * Gets the id. 47 | * 48 | * @return the id 49 | */ 50 | public String getId() { 51 | return id; 52 | } 53 | 54 | 55 | /** 56 | * Sets the id. 57 | * 58 | * @param id the new id 59 | */ 60 | public void setId(String id) { 61 | this.id = id; 62 | } 63 | 64 | 65 | /** 66 | * Gets the first name. 67 | * 68 | * @return the first name 69 | */ 70 | public String getFirstName() { 71 | return firstName; 72 | } 73 | 74 | /** 75 | * Sets the first name. 76 | * 77 | * @param firstName the new first name 78 | */ 79 | public void setFirstName(String firstName) { 80 | this.firstName = firstName; 81 | } 82 | 83 | /** 84 | * Gets the last name. 85 | * 86 | * @return the last name 87 | */ 88 | public String getLastName() { 89 | return lastName; 90 | } 91 | 92 | /** 93 | * Sets the last name. 94 | * 95 | * @param lastName the new last name 96 | */ 97 | public void setLastName(String lastName) { 98 | this.lastName = lastName; 99 | } 100 | 101 | /** 102 | * Gets the email id. 103 | * 104 | * @return the email id 105 | */ 106 | public String getEmailId() { 107 | return emailId; 108 | } 109 | 110 | /** 111 | * Sets the email id. 112 | * 113 | * @param emailId the new email id 114 | */ 115 | public void setEmailId(String emailId) { 116 | this.emailId = emailId; 117 | } 118 | 119 | /** 120 | * Gets the age. 121 | * 122 | * @return the age 123 | */ 124 | public int getAge() { 125 | return age; 126 | } 127 | 128 | /** 129 | * Sets the age. 130 | * 131 | * @param age the new age 132 | */ 133 | public void setAge(int age) { 134 | this.age = age; 135 | } 136 | 137 | /** 138 | * Gets the date of birth. 139 | * 140 | * @return the date of birth 141 | */ 142 | public Date getDateOfBirth() { 143 | return dateOfBirth; 144 | } 145 | 146 | /** 147 | * Sets the date of birth. 148 | * 149 | * @param dateOfBirth the new date of birth 150 | */ 151 | public void setDateOfBirth(Date dateOfBirth) { 152 | this.dateOfBirth = dateOfBirth; 153 | } 154 | 155 | 156 | /** 157 | * Instantiates a new user. Default Constructor 158 | */ 159 | public User() { 160 | super(); 161 | } 162 | 163 | 164 | /** 165 | * Instantiates a new user. 166 | * 167 | * @param id the id 168 | * @param firstName the first name 169 | * @param lastName the last name 170 | * @param emailId the email id 171 | * @param age the age 172 | * @param dateOfBirth the date of birth 173 | */ 174 | public User(String id, String firstName, String lastName, String emailId, int age, Date dateOfBirth) { 175 | super(); 176 | this.id = id; 177 | this.firstName = firstName; 178 | this.lastName = lastName; 179 | this.emailId = emailId; 180 | this.age = age; 181 | this.dateOfBirth = dateOfBirth; 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/dto/UserDto.java: -------------------------------------------------------------------------------- 1 | /* 2 | * User DTO, this holds all the value that are passed from controller to service 3 | */ 4 | package com.rasmivan.showcase.dto; 5 | 6 | import java.util.Date; 7 | 8 | import io.swagger.annotations.ApiModelProperty; 9 | 10 | /** 11 | * The Class User. 12 | * @author Rasmivan Ilangovan 13 | * This holds all the user information like 14 | * User First Name 15 | * User Last Name 16 | * User Email ID 17 | * User Age 18 | * User Date of Birth. 19 | */ 20 | public class UserDto { 21 | 22 | 23 | 24 | /** The id. */ 25 | @ApiModelProperty(hidden = true) 26 | private String id; 27 | 28 | /** The first name. */ 29 | private String firstName; 30 | 31 | /** The last name. */ 32 | private String lastName; 33 | 34 | /** The email id. */ 35 | private String emailId; 36 | 37 | /** The age. */ 38 | private Integer age; 39 | 40 | /** The date of birth. */ 41 | private Date dateOfBirth; 42 | 43 | /** 44 | * Gets the id. 45 | * 46 | * @return the id 47 | */ 48 | public String getId() { 49 | return id; 50 | } 51 | 52 | 53 | /** 54 | * Sets the id. 55 | * 56 | * @param id the new id 57 | */ 58 | public void setId(String id) { 59 | this.id = id; 60 | } 61 | 62 | 63 | /** 64 | * Gets the first name. 65 | * 66 | * @return the first name 67 | */ 68 | public String getFirstName() { 69 | return firstName; 70 | } 71 | 72 | /** 73 | * Sets the first name. 74 | * 75 | * @param firstName the new first name 76 | */ 77 | public void setFirstName(String firstName) { 78 | this.firstName = firstName; 79 | } 80 | 81 | /** 82 | * Gets the last name. 83 | * 84 | * @return the last name 85 | */ 86 | public String getLastName() { 87 | return lastName; 88 | } 89 | 90 | /** 91 | * Sets the last name. 92 | * 93 | * @param lastName the new last name 94 | */ 95 | public void setLastName(String lastName) { 96 | this.lastName = lastName; 97 | } 98 | 99 | /** 100 | * Gets the email id. 101 | * 102 | * @return the email id 103 | */ 104 | public String getEmailId() { 105 | return emailId; 106 | } 107 | 108 | /** 109 | * Sets the email id. 110 | * 111 | * @param emailId the new email id 112 | */ 113 | public void setEmailId(String emailId) { 114 | this.emailId = emailId; 115 | } 116 | 117 | /** 118 | * Gets the age. 119 | * 120 | * @return the age 121 | */ 122 | public Integer getAge() { 123 | return age; 124 | } 125 | 126 | /** 127 | * Sets the age. 128 | * 129 | * @param age the new age 130 | */ 131 | public void setAge(Integer age) { 132 | this.age = age; 133 | } 134 | 135 | /** 136 | * Gets the date of birth. 137 | * 138 | * @return the date of birth 139 | */ 140 | public Date getDateOfBirth() { 141 | return dateOfBirth; 142 | } 143 | 144 | /** 145 | * Sets the date of birth. 146 | * 147 | * @param dateOfBirth the new date of birth 148 | */ 149 | public void setDateOfBirth(Date dateOfBirth) { 150 | this.dateOfBirth = dateOfBirth; 151 | } 152 | 153 | 154 | /** 155 | * Instantiates a new user. Default Constructor 156 | */ 157 | public UserDto() { 158 | super(); 159 | } 160 | 161 | 162 | /** 163 | * Instantiates a new user. 164 | * 165 | * @param id the id 166 | * @param firstName the first name 167 | * @param lastName the last name 168 | * @param emailId the email id 169 | * @param age the age 170 | * @param dateOfBirth the date of birth 171 | */ 172 | public UserDto(String id, String firstName, String lastName, String emailId, Integer age, Date dateOfBirth) { 173 | super(); 174 | this.id = id; 175 | this.firstName = firstName; 176 | this.lastName = lastName; 177 | this.emailId = emailId; 178 | this.age = age; 179 | this.dateOfBirth = dateOfBirth; 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | https://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | import java.io.File; 21 | import java.io.FileInputStream; 22 | import java.io.FileOutputStream; 23 | import java.io.IOException; 24 | import java.net.URL; 25 | import java.nio.channels.Channels; 26 | import java.nio.channels.ReadableByteChannel; 27 | import java.util.Properties; 28 | 29 | public class MavenWrapperDownloader { 30 | 31 | /** 32 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 33 | */ 34 | private static final String DEFAULT_DOWNLOAD_URL = 35 | "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"; 36 | 37 | /** 38 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 39 | * use instead of the default one. 40 | */ 41 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 42 | ".mvn/wrapper/maven-wrapper.properties"; 43 | 44 | /** 45 | * Path where the maven-wrapper.jar will be saved to. 46 | */ 47 | private static final String MAVEN_WRAPPER_JAR_PATH = 48 | ".mvn/wrapper/maven-wrapper.jar"; 49 | 50 | /** 51 | * Name of the property which should be used to override the default download url for the wrapper. 52 | */ 53 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 54 | 55 | public static void main(String args[]) { 56 | System.out.println("- Downloader started"); 57 | File baseDirectory = new File(args[0]); 58 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 59 | 60 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 61 | // wrapperUrl parameter. 62 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 63 | String url = DEFAULT_DOWNLOAD_URL; 64 | if(mavenWrapperPropertyFile.exists()) { 65 | FileInputStream mavenWrapperPropertyFileInputStream = null; 66 | try { 67 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 68 | Properties mavenWrapperProperties = new Properties(); 69 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 70 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 71 | } catch (IOException e) { 72 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 73 | } finally { 74 | try { 75 | if(mavenWrapperPropertyFileInputStream != null) { 76 | mavenWrapperPropertyFileInputStream.close(); 77 | } 78 | } catch (IOException e) { 79 | // Ignore ... 80 | } 81 | } 82 | } 83 | System.out.println("- Downloading from: : " + url); 84 | 85 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 86 | if(!outputFile.getParentFile().exists()) { 87 | if(!outputFile.getParentFile().mkdirs()) { 88 | System.out.println( 89 | "- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 90 | } 91 | } 92 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 93 | try { 94 | downloadFileFromURL(url, outputFile); 95 | System.out.println("Done"); 96 | System.exit(0); 97 | } catch (Throwable e) { 98 | System.out.println("- Error downloading"); 99 | e.printStackTrace(); 100 | System.exit(1); 101 | } 102 | } 103 | 104 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 105 | URL website = new URL(urlString); 106 | ReadableByteChannel rbc; 107 | rbc = Channels.newChannel(website.openStream()); 108 | FileOutputStream fos = new FileOutputStream(destination); 109 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 110 | fos.close(); 111 | rbc.close(); 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/com/rasmivan/showcase/service/UserServiceImp.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | */ 4 | package com.rasmivan.showcase.service; 5 | 6 | import java.util.List; 7 | import java.util.Optional; 8 | 9 | import org.springframework.beans.BeanUtils; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import com.rasmivan.showcase.domain.User; 14 | import com.rasmivan.showcase.dto.UserDto; 15 | import com.rasmivan.showcase.exception.UserCreateUpdateException; 16 | import com.rasmivan.showcase.exception.UserDoesnotExistsException; 17 | import com.rasmivan.showcase.exception.UserUpdateDtoEmptyException; 18 | import com.rasmivan.showcase.repositories.UserRepository; 19 | 20 | import com.rasmivan.showcase.constants.MessageConstantsUtils; 21 | 22 | /** 23 | * The Class UserServiceImp. 24 | * @author Rasmivan Ilangovan 25 | */ 26 | @Service 27 | public class UserServiceImp implements UserService { 28 | 29 | 30 | /** The user repository. */ 31 | @Autowired 32 | UserRepository userRepository; 33 | 34 | 35 | /** 36 | * Instantiates a new user service imp. 37 | * Default Constructor 38 | */ 39 | public UserServiceImp() { 40 | super(); 41 | } 42 | 43 | /** 44 | * (non-Javadoc). 45 | * 46 | * @param id the id 47 | * @return the user by id 48 | * @see com.rasmivan.showcase.service.UserService#getUserById(java.lang.String) 49 | */ 50 | @Override 51 | public User getUserById(String id) { 52 | Optional user = userRepository.findById(id); 53 | if(user.isPresent()) { 54 | return user.get(); 55 | } else { 56 | throw new UserDoesnotExistsException(MessageConstantsUtils.USERNOT_FOUND); 57 | } 58 | 59 | /** The above block is written for readability. the same block can be written as below line. 60 | * 61 | * return user.isPresent() ? user.get() : throw new UserDoesnotExistsException(MessageConstants.USERNOT_FOUND); 62 | * 63 | */ 64 | } 65 | 66 | /** 67 | * (non-Javadoc). 68 | * 69 | * @param usr the usr 70 | * @return the user 71 | * @see com.rasmivan.showcase.service.UserService#addOrUpdateUser(com.rasmivan.showcase.dto.UserDto) 72 | */ 73 | @Override 74 | public User addOrUpdateUser(UserDto usr) { 75 | User userDomain = new User(); 76 | BeanUtils.copyProperties(usr, userDomain); // Copy from DTO to Domain 77 | userDomain = userRepository.save(userDomain); 78 | if(userDomain != null && userDomain.getId() != null) { 79 | return userDomain; 80 | } else { 81 | throw new UserCreateUpdateException(MessageConstantsUtils.USER_NOT_CREATED); 82 | } 83 | } 84 | 85 | /** 86 | * (non-Javadoc). 87 | * 88 | * @param usr the usr 89 | * @return the user 90 | * @see com.rasmivan.showcase.service.UserService#updateUser(com.rasmivan.showcase.dto.UserDto) 91 | */ 92 | @Override 93 | public User updateUser(UserDto usr) { 94 | if(usr != null && usr.getId() != null ) { 95 | User userToUpdate = getUserById(usr.getId()); 96 | populateUserDomainForUpdate(userToUpdate,usr); 97 | userToUpdate = userRepository.save(userToUpdate); 98 | return userToUpdate; 99 | } else { 100 | throw new UserUpdateDtoEmptyException(MessageConstantsUtils.USERUPDATE_EMPTY); 101 | } 102 | } 103 | 104 | 105 | /** 106 | * (non-Javadoc). 107 | * 108 | * @param firstName the first name 109 | * @return the user by first name 110 | * @see com.rasmivan.showcase.service.UserService#getUserByFirstName(java.lang.String) 111 | */ 112 | @Override 113 | public User getUserByFirstName(String firstName) { 114 | Optional user = userRepository.findByFirstName(firstName); 115 | if(user.isPresent()) { 116 | return user.get(); 117 | } else { 118 | throw new UserDoesnotExistsException(MessageConstantsUtils.USERNOT_FOUND); 119 | } 120 | } 121 | 122 | /** 123 | * (non-Javadoc). 124 | * 125 | * @return the all user 126 | * @see com.rasmivan.showcase.service.UserService#getAllUser() 127 | */ 128 | @Override 129 | public List getAllUser() { 130 | return userRepository.findAll(); 131 | } 132 | 133 | 134 | /* (non-Javadoc) 135 | * @see com.rasmivan.showcase.service.UserService#deleteUser(java.lang.String) 136 | */ 137 | @Override 138 | public String deleteUserById(String id) { 139 | if(id != null) { 140 | userRepository.delete(getUserById(id)); 141 | } 142 | return MessageConstantsUtils.USER_DELETED_SUCCESSFULLY; 143 | } 144 | 145 | /** 146 | * Populate user domain for update. 147 | * 148 | * @param userToUpdate the user to update 149 | * @param usr the usr 150 | */ 151 | private void populateUserDomainForUpdate(User userToUpdate, UserDto usr) { 152 | if(!checkIfNull(userToUpdate) && !checkIfNull(usr)) { 153 | 154 | if(!checkIfNull(usr.getAge())) { 155 | userToUpdate.setAge(usr.getAge()); 156 | } 157 | 158 | if(!checkIfNull(usr.getAge())) { 159 | userToUpdate.setDateOfBirth(usr.getDateOfBirth()); 160 | } 161 | 162 | if(!checkIfNull(usr.getAge())) { 163 | userToUpdate.setEmailId(usr.getEmailId()); 164 | } 165 | 166 | if(!checkIfNull(usr.getAge())) { 167 | userToUpdate.setFirstName(usr.getFirstName()); 168 | } 169 | 170 | if(!checkIfNull(usr.getAge())) { 171 | userToUpdate.setLastName(usr.getLastName()); 172 | } 173 | } 174 | } 175 | 176 | /** 177 | * Check if null. 178 | * 179 | * @param obj the obj 180 | * @return true, if successful 181 | */ 182 | private boolean checkIfNull(Object obj){ 183 | return obj == null ? true : false; 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM https://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM set title of command window 39 | title %0 40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 42 | 43 | @REM set %HOME% to equivalent of $HOME 44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 45 | 46 | @REM Execute a user defined script before this one 47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 51 | :skipRcPre 52 | 53 | @setlocal 54 | 55 | set ERROR_CODE=0 56 | 57 | @REM To isolate internal variables from possible post scripts, we use another setlocal 58 | @setlocal 59 | 60 | @REM ==== START VALIDATION ==== 61 | if not "%JAVA_HOME%" == "" goto OkJHome 62 | 63 | echo. 64 | echo Error: JAVA_HOME not found in your environment. >&2 65 | echo Please set the JAVA_HOME variable in your environment to match the >&2 66 | echo location of your Java installation. >&2 67 | echo. 68 | goto error 69 | 70 | :OkJHome 71 | if exist "%JAVA_HOME%\bin\java.exe" goto init 72 | 73 | echo. 74 | echo Error: JAVA_HOME is set to an invalid directory. >&2 75 | echo JAVA_HOME = "%JAVA_HOME%" >&2 76 | echo Please set the JAVA_HOME variable in your environment to match the >&2 77 | echo location of your Java installation. >&2 78 | echo. 79 | goto error 80 | 81 | @REM ==== END VALIDATION ==== 82 | 83 | :init 84 | 85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 86 | @REM Fallback to current working directory if not found. 87 | 88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 90 | 91 | set EXEC_DIR=%CD% 92 | set WDIR=%EXEC_DIR% 93 | :findBaseDir 94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 95 | cd .. 96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 97 | set WDIR=%CD% 98 | goto findBaseDir 99 | 100 | :baseDirFound 101 | set MAVEN_PROJECTBASEDIR=%WDIR% 102 | cd "%EXEC_DIR%" 103 | goto endDetectBaseDir 104 | 105 | :baseDirNotFound 106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 107 | cd "%EXEC_DIR%" 108 | 109 | :endDetectBaseDir 110 | 111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 112 | 113 | @setlocal EnableExtensions EnableDelayedExpansion 114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 116 | 117 | :endReadAdditionalConfig 118 | 119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 122 | 123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO ( 125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B 126 | ) 127 | 128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data. 130 | if exist %WRAPPER_JAR% ( 131 | echo Found %WRAPPER_JAR% 132 | ) else ( 133 | echo Couldn't find %WRAPPER_JAR%, downloading it ... 134 | echo Downloading from: %DOWNLOAD_URL% 135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')" 136 | echo Finished downloading %WRAPPER_JAR% 137 | ) 138 | @REM End of extension 139 | 140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 141 | if ERRORLEVEL 1 goto error 142 | goto end 143 | 144 | :error 145 | set ERROR_CODE=1 146 | 147 | :end 148 | @endlocal & set ERROR_CODE=%ERROR_CODE% 149 | 150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 154 | :skipRcPost 155 | 156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 158 | 159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 160 | 161 | exit /B %ERROR_CODE% 162 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # https://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Mingw, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | ########################################################################################## 204 | # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central 205 | # This allows using the maven wrapper in projects that prohibit checking in binary data. 206 | ########################################################################################## 207 | if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then 208 | if [ "$MVNW_VERBOSE" = true ]; then 209 | echo "Found .mvn/wrapper/maven-wrapper.jar" 210 | fi 211 | else 212 | if [ "$MVNW_VERBOSE" = true ]; then 213 | echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." 214 | fi 215 | jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar" 216 | while IFS="=" read key value; do 217 | case "$key" in (wrapperUrl) jarUrl="$value"; break ;; 218 | esac 219 | done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" 220 | if [ "$MVNW_VERBOSE" = true ]; then 221 | echo "Downloading from: $jarUrl" 222 | fi 223 | wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" 224 | 225 | if command -v wget > /dev/null; then 226 | if [ "$MVNW_VERBOSE" = true ]; then 227 | echo "Found wget ... using wget" 228 | fi 229 | wget "$jarUrl" -O "$wrapperJarPath" 230 | elif command -v curl > /dev/null; then 231 | if [ "$MVNW_VERBOSE" = true ]; then 232 | echo "Found curl ... using curl" 233 | fi 234 | curl -o "$wrapperJarPath" "$jarUrl" 235 | else 236 | if [ "$MVNW_VERBOSE" = true ]; then 237 | echo "Falling back to using Java to download" 238 | fi 239 | javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" 240 | if [ -e "$javaClass" ]; then 241 | if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 242 | if [ "$MVNW_VERBOSE" = true ]; then 243 | echo " - Compiling MavenWrapperDownloader.java ..." 244 | fi 245 | # Compiling the Java class 246 | ("$JAVA_HOME/bin/javac" "$javaClass") 247 | fi 248 | if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then 249 | # Running the downloader 250 | if [ "$MVNW_VERBOSE" = true ]; then 251 | echo " - Running MavenWrapperDownloader.java ..." 252 | fi 253 | ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") 254 | fi 255 | fi 256 | fi 257 | fi 258 | ########################################################################################## 259 | # End of extension 260 | ########################################################################################## 261 | 262 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 263 | if [ "$MVNW_VERBOSE" = true ]; then 264 | echo $MAVEN_PROJECTBASEDIR 265 | fi 266 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 267 | 268 | # For Cygwin, switch paths to Windows format before running java 269 | if $cygwin; then 270 | [ -n "$M2_HOME" ] && 271 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 272 | [ -n "$JAVA_HOME" ] && 273 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 274 | [ -n "$CLASSPATH" ] && 275 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 276 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 277 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 278 | fi 279 | 280 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 281 | 282 | exec "$JAVACMD" \ 283 | $MAVEN_OPTS \ 284 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 285 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 286 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 287 | --------------------------------------------------------------------------------