parent
f43db12fcb
commit
eaa0c63620
|
@ -4,7 +4,3 @@ SprintBootConfiguration annotation
|
|||
commands:
|
||||
mvn clean install
|
||||
mvn spring-boot:run
|
||||
|
||||
Swagger endpoints:
|
||||
http://localhost:8080/v2/api-docs
|
||||
http://localhost:8080/swagger-ui.html
|
|
@ -12,7 +12,7 @@
|
|||
<artifactId>spring-boot-configuration</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>spring-boot-configuration</name>
|
||||
<description>Demo project for Spring Boot Configuration </description>
|
||||
<description>Demo project for Spring Boot Configuration</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
|
@ -23,24 +23,6 @@
|
|||
<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>
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
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.*"})
|
||||
import com.baeldung.ServiceImpl.PersonServiceImpl;
|
||||
import com.baeldung.service.PersonService;
|
||||
|
||||
@ComponentScan(basePackages = { "com.baeldung.*" })
|
||||
@SpringBootConfiguration
|
||||
public class Application {
|
||||
|
||||
|
@ -19,7 +18,6 @@ public class Application {
|
|||
|
||||
@Bean
|
||||
public PersonService personService() {
|
||||
return new PersonService();
|
||||
return new PersonServiceImpl();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.ServiceImpl;
|
||||
|
||||
import com.baeldung.service.PersonService;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
package com.baeldung.exception;
|
||||
|
||||
public class PersonNotFoundException extends Exception {
|
||||
|
||||
public PersonNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
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> {
|
||||
}
|
|
@ -1,34 +1,4 @@
|
|||
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));
|
||||
}
|
||||
public interface PersonService {
|
||||
}
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
# H2
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.path=/h2
|
|
@ -1,5 +0,0 @@
|
|||
CREATE TABLE `person` (
|
||||
`id` INT(11) NOT NULL,
|
||||
`name` VARCHAR(50) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
);
|
Loading…
Reference in New Issue