BAEL-20869 Move remaining spring boot modules
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@ServletComponentScan
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung")
|
||||
@EnableJpaRepositories("com.baeldung.persistence.repo")
|
||||
@EntityScan("com.baeldung.persistence.model")
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.cloud.config;
|
||||
|
||||
import org.springframework.cloud.config.java.AbstractCloudConfig;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@Profile("cloud")
|
||||
public class CloudDataSourceConfig extends AbstractCloudConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return connectionFactory().dataSource();
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.anyRequest()
|
||||
.permitAll()
|
||||
.and().csrf().disable();
|
||||
}
|
||||
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
public Book() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Book(String title, String author) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((author == null) ? 0 : author.hashCode());
|
||||
result = prime * result + (int) (id ^ (id >>> 32));
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Book other = (Book) obj;
|
||||
if (author == null) {
|
||||
if (other.author != null)
|
||||
return false;
|
||||
} else if (!author.equals(other.author))
|
||||
return false;
|
||||
if (id != other.id)
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [id=" + id + ", title=" + title + ", author=" + author + "]";
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.persistence.repo;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
List<Book> findByTitle(String title);
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan(basePackages = {"com.baeldung.springbootconfiguration.*"})
|
||||
@SpringBootConfiguration
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersonService personService() {
|
||||
return new PersonServiceImpl();
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
public interface PersonService {
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.repo.BookRepository;
|
||||
import com.baeldung.web.exception.BookIdMismatchException;
|
||||
import com.baeldung.web.exception.BookNotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/books")
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Book> findAll() {
|
||||
return bookRepository.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/title/{bookTitle}")
|
||||
public List<Book> findByTitle(@PathVariable String bookTitle) {
|
||||
return bookRepository.findByTitle(bookTitle);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Book findOne(@PathVariable long id) {
|
||||
return bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Book create(@RequestBody Book book) {
|
||||
Book book1 = bookRepository.save(book);
|
||||
return book1;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable long id) {
|
||||
bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
bookRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public Book updateBook(@RequestBody Book book, @PathVariable long id) {
|
||||
if (book.getId() != id) {
|
||||
throw new BookIdMismatchException();
|
||||
}
|
||||
bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import com.baeldung.web.exception.BookIdMismatchException;
|
||||
import com.baeldung.web.exception.BookNotFoundException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
public RestExceptionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
@ExceptionHandler(BookNotFoundException.class)
|
||||
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({
|
||||
BookIdMismatchException.class,
|
||||
ConstraintViolationException.class,
|
||||
DataIntegrityViolationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleBadRequest(Exception ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex, ex
|
||||
.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class SimpleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
String appName;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String homePage(Model model) {
|
||||
model.addAttribute("appName", appName);
|
||||
return "home";
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public class BookIdMismatchException extends RuntimeException {
|
||||
|
||||
public BookIdMismatchException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public class BookNotFoundException extends RuntimeException {
|
||||
|
||||
public BookNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookNotFoundException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public BookNotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BookNotFoundException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user