Spring Boot Bootstrap upgrade (#2904)
This commit is contained in:
committed by
Eugen
parent
f1831535cb
commit
95cf16de28
@@ -8,6 +8,7 @@ import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
@@ -18,8 +19,6 @@ public class Book {
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
//
|
||||
|
||||
public Book() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package org.baeldung.persistence.repo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
List<Book> findByTitle(String title);
|
||||
Optional<Book> findOne(long id);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.baeldung.web;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.Book;
|
||||
import org.baeldung.persistence.repo.BookRepository;
|
||||
import org.baeldung.web.exception.BookIdMismatchException;
|
||||
@@ -18,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/books")
|
||||
public class BookController {
|
||||
@@ -36,12 +36,9 @@ public class BookController {
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Book findOne(@PathVariable Long id) {
|
||||
final Book book = bookRepository.findOne(id);
|
||||
if (book == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
return book;
|
||||
public Book findOne(@PathVariable long id) {
|
||||
return bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@@ -51,24 +48,19 @@ public class BookController {
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable Long id) {
|
||||
final Book book = bookRepository.findOne(id);
|
||||
if (book == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
public void delete(@PathVariable long id) {
|
||||
bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
bookRepository.delete(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public Book updateBook(@RequestBody Book book, @PathVariable Long id) {
|
||||
public Book updateBook(@RequestBody Book book, @PathVariable long id) {
|
||||
if (book.getId() != id) {
|
||||
throw new BookIdMismatchException();
|
||||
}
|
||||
final Book old = bookRepository.findOne(id);
|
||||
if (old == null) {
|
||||
throw new BookNotFoundException();
|
||||
}
|
||||
bookRepository.findOne(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,14 +19,18 @@ public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
super();
|
||||
}
|
||||
|
||||
@ExceptionHandler({ BookNotFoundException.class })
|
||||
@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 })
|
||||
@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);
|
||||
return handleExceptionInternal(ex, ex
|
||||
.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class SimpleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
String appName;
|
||||
|
||||
@@ -15,5 +16,4 @@ public class SimpleController {
|
||||
model.addAttribute("appName", appName);
|
||||
return "home";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootBootstrapApplicationTests {
|
||||
public class SpringBootBootstrapApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,8 +21,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { Application.class }, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
public class LiveTest {
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
||||
public class SpringBootBootstrapIntegrationTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Reference in New Issue
Block a user