From 1f4dfef8db56afd2ba9324fc30d6b62159a96dcf Mon Sep 17 00:00:00 2001 From: vatsalgosar Date: Sat, 13 Jul 2019 05:49:41 +0530 Subject: [PATCH] BAEL-2728 | vatsalgosar@gmail.com - Sprint Boot application implementation using @SpringBootConfiguration annotation --- spring-boot-configuration/.gitignore | 31 ++++++++ spring-boot-configuration/README.md | 10 +++ spring-boot-configuration/pom.xml | 72 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 20 ++++++ .../main/java/com/baeldung/SwaggerConfig.java | 22 ++++++ .../baeldung/controller/PersonController.java | 39 ++++++++++ .../main/java/com/baeldung/domain/Person.java | 38 ++++++++++ .../exception/PersonNotFoundException.java | 8 +++ .../baeldung/repository/PersonRepository.java | 9 +++ .../com/baeldung/service/PersonService.java | 34 +++++++++ .../src/main/resources/application.properties | 3 + .../src/main/resources/schema.sql | 5 ++ .../java/com/baeldung/ApplicationTests.java | 16 +++++ 13 files changed, 307 insertions(+) create mode 100644 spring-boot-configuration/.gitignore create mode 100644 spring-boot-configuration/README.md create mode 100644 spring-boot-configuration/pom.xml create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/Application.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/SwaggerConfig.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/controller/PersonController.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/domain/Person.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/exception/PersonNotFoundException.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/repository/PersonRepository.java create mode 100644 spring-boot-configuration/src/main/java/com/baeldung/service/PersonService.java create mode 100644 spring-boot-configuration/src/main/resources/application.properties create mode 100644 spring-boot-configuration/src/main/resources/schema.sql create mode 100644 spring-boot-configuration/src/test/java/com/baeldung/ApplicationTests.java diff --git a/spring-boot-configuration/.gitignore b/spring-boot-configuration/.gitignore new file mode 100644 index 0000000000..a2a3040aa8 --- /dev/null +++ b/spring-boot-configuration/.gitignore @@ -0,0 +1,31 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/spring-boot-configuration/README.md b/spring-boot-configuration/README.md new file mode 100644 index 0000000000..35472d575a --- /dev/null +++ b/spring-boot-configuration/README.md @@ -0,0 +1,10 @@ +Article: +SprintBootConfiguration annotation + +commands: +mvn clean install +mvn spring-boot:run + +Swagger endpoints: +http://localhost:8080/v2/api-docs +http://localhost:8080/swagger-ui.html \ No newline at end of file diff --git a/spring-boot-configuration/pom.xml b/spring-boot-configuration/pom.xml new file mode 100644 index 0000000000..ede2504272 --- /dev/null +++ b/spring-boot-configuration/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + com.baeldung + spring-boot-configuration + 0.0.1-SNAPSHOT + spring-boot-configuration + Demo project for Spring Boot Configuration + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + + com.h2database + h2 + runtime + + + + + org.hibernate + hibernate-entitymanager + + + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-configuration/src/main/java/com/baeldung/Application.java b/spring-boot-configuration/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..019cc5b57c --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/Application.java @@ -0,0 +1,20 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; + +@EnableAutoConfiguration +@ComponentScan(basePackages = {"com.baeldung.*"}) +@SpringBootConfiguration +@Import({SwaggerConfig.class}) +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + } +} + diff --git a/spring-boot-configuration/src/main/java/com/baeldung/SwaggerConfig.java b/spring-boot-configuration/src/main/java/com/baeldung/SwaggerConfig.java new file mode 100644 index 0000000000..c69e65bc4e --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/SwaggerConfig.java @@ -0,0 +1,22 @@ +package com.baeldung; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.context.annotation.Bean; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.RequestHandlerSelectors; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@SpringBootConfiguration +@EnableSwagger2 +public class SwaggerConfig { + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build(); + } +} diff --git a/spring-boot-configuration/src/main/java/com/baeldung/controller/PersonController.java b/spring-boot-configuration/src/main/java/com/baeldung/controller/PersonController.java new file mode 100644 index 0000000000..10d7386132 --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/controller/PersonController.java @@ -0,0 +1,39 @@ +package com.baeldung.controller; + +import com.baeldung.domain.Person; +import com.baeldung.exception.PersonNotFoundException; +import com.baeldung.service.PersonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/persons") +public class PersonController { + + @Autowired + PersonService personService; + + @GetMapping + public List getPersons() { + return personService.getPersons(); + } + + @PostMapping + public void addPerson(@RequestBody Person person) { + personService.add(person); + } + + @GetMapping("/{id}") + public Person getPersonById(@PathVariable(required = true) long id) throws PersonNotFoundException { + return personService.getPersonById(id); + } + + @DeleteMapping("/{id}") + public void removePerson(@PathVariable(required = true) long id) { + personService.delete(id); + } + + +} diff --git a/spring-boot-configuration/src/main/java/com/baeldung/domain/Person.java b/spring-boot-configuration/src/main/java/com/baeldung/domain/Person.java new file mode 100644 index 0000000000..e63836420c --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/domain/Person.java @@ -0,0 +1,38 @@ +package com.baeldung.domain; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private long id; + private String name; + + public Person() {} + + public Person(long id, String name) { + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + diff --git a/spring-boot-configuration/src/main/java/com/baeldung/exception/PersonNotFoundException.java b/spring-boot-configuration/src/main/java/com/baeldung/exception/PersonNotFoundException.java new file mode 100644 index 0000000000..048197a072 --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/exception/PersonNotFoundException.java @@ -0,0 +1,8 @@ +package com.baeldung.exception; + +public class PersonNotFoundException extends Exception { + + public PersonNotFoundException(String message) { + super(message); + } +} diff --git a/spring-boot-configuration/src/main/java/com/baeldung/repository/PersonRepository.java b/spring-boot-configuration/src/main/java/com/baeldung/repository/PersonRepository.java new file mode 100644 index 0000000000..b542b5ea0b --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/repository/PersonRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repository; + +import com.baeldung.domain.Person; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonRepository extends CrudRepository { +} diff --git a/spring-boot-configuration/src/main/java/com/baeldung/service/PersonService.java b/spring-boot-configuration/src/main/java/com/baeldung/service/PersonService.java new file mode 100644 index 0000000000..e5929177de --- /dev/null +++ b/spring-boot-configuration/src/main/java/com/baeldung/service/PersonService.java @@ -0,0 +1,34 @@ +package com.baeldung.service; + +import com.baeldung.domain.Person; +import com.baeldung.exception.PersonNotFoundException; +import com.baeldung.repository.PersonRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +public class PersonService { + + @Autowired + PersonRepository personRepository; + + public void add(Person person) { + personRepository.save(person); + } + + public void delete(long id) { + personRepository.deleteById(id); + } + + public List getPersons() { + return (List) personRepository.findAll(); + } + + public Person getPersonById(long id) throws PersonNotFoundException { + Optional optionalDog = personRepository.findById(id); + return optionalDog.orElseThrow(() -> new PersonNotFoundException("Couldn't find a Person with id: " + id)); + } +} diff --git a/spring-boot-configuration/src/main/resources/application.properties b/spring-boot-configuration/src/main/resources/application.properties new file mode 100644 index 0000000000..7de28fe306 --- /dev/null +++ b/spring-boot-configuration/src/main/resources/application.properties @@ -0,0 +1,3 @@ +# H2 +spring.h2.console.enabled=true +spring.h2.console.path=/h2 diff --git a/spring-boot-configuration/src/main/resources/schema.sql b/spring-boot-configuration/src/main/resources/schema.sql new file mode 100644 index 0000000000..02a7aca232 --- /dev/null +++ b/spring-boot-configuration/src/main/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE `person` ( + `id` INT(11) NOT NULL, + `name` VARCHAR(50) NULL DEFAULT NULL, + PRIMARY KEY (`id`) +); \ No newline at end of file diff --git a/spring-boot-configuration/src/test/java/com/baeldung/ApplicationTests.java b/spring-boot-configuration/src/test/java/com/baeldung/ApplicationTests.java new file mode 100644 index 0000000000..f239fc76cc --- /dev/null +++ b/spring-boot-configuration/src/test/java/com/baeldung/ApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +}