- Adding spring-boot-configuration project to spring-boot-bootstrap module
This commit is contained in:
vatsalgosar
2019-07-16 01:55:08 +05:30
parent 1f4dfef8db
commit f43db12fcb
13 changed files with 9 additions and 69 deletions

View File

@@ -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

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-boot-configuration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-configuration</name>
<description>Demo project for Spring Boot Configuration </description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- h2 database dependency-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<!-- hibernate dependency-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,25 @@
package com.baeldung;
import com.baeldung.service.PersonService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.baeldung.*"})
@SpringBootConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public PersonService personService() {
return new PersonService();
}
}

View File

@@ -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<Person> 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);
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.exception;
public class PersonNotFoundException extends Exception {
public PersonNotFoundException(String message) {
super(message);
}
}

View File

@@ -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<Person, Long> {
}

View File

@@ -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<Person> getPersons() {
return (List<Person>) personRepository.findAll();
}
public Person getPersonById(long id) throws PersonNotFoundException {
Optional<Person> optionalDog = personRepository.findById(id);
return optionalDog.orElseThrow(() -> new PersonNotFoundException("Couldn't find a Person with id: " + id));
}
}

View File

@@ -0,0 +1,3 @@
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

View File

@@ -0,0 +1,5 @@
CREATE TABLE `person` (
`id` INT(11) NOT NULL,
`name` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);

View File

@@ -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() {
}
}