BAEL-20887 Move Spring Boot Springdoc module to Spring Boot modules
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.springdoc;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringdocApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringdocApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.springdoc.controller;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
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.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.springdoc.exception.BookNotFoundException;
|
||||
import com.baeldung.springdoc.model.Book;
|
||||
import com.baeldung.springdoc.repository.BookRepository;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/book")
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
private BookRepository repository;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Book findById(@PathVariable long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new BookNotFoundException());
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public Collection<Book> findBooks() {
|
||||
return repository.getBooks();
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Book updateBook(@PathVariable("id") final String id, @RequestBody final Book book) {
|
||||
return book;
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Book patchBook(@PathVariable("id") final String id, @RequestBody final Book book) {
|
||||
return book;
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Book postBook(@NotNull @Valid @RequestBody final Book book) {
|
||||
return book;
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.HEAD, value = "/")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public Book headBook() {
|
||||
return new Book();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public long deleteBook(@PathVariable final long id) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.springdoc.exception;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class BookNotFoundException extends RuntimeException {
|
||||
|
||||
public BookNotFoundException() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.springdoc.exception;
|
||||
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalControllerExceptionHandler {
|
||||
|
||||
@ExceptionHandler(ConversionFailedException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ResponseEntity<String> handleConnversion(RuntimeException ex) {
|
||||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(BookNotFoundException.class)
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public ResponseEntity<String> handleBookNotFound(RuntimeException ex) {
|
||||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.springdoc.model;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class Book {
|
||||
|
||||
private long id;
|
||||
|
||||
@NotBlank
|
||||
@Size(min = 0, max = 20)
|
||||
private String title;
|
||||
|
||||
@NotBlank
|
||||
@Size(min = 0, max = 30)
|
||||
private String 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springdoc.repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.springdoc.model.Book;
|
||||
|
||||
@Repository
|
||||
public class BookRepository {
|
||||
|
||||
private Map<Long, Book> books = new HashMap<>();
|
||||
|
||||
public Optional<Book> findById(long id) {
|
||||
return Optional.ofNullable(books.get(id));
|
||||
}
|
||||
|
||||
public void add(Book book) {
|
||||
books.put(book.getId(), book);
|
||||
}
|
||||
|
||||
public Collection<Book> getBooks() {
|
||||
return books.values();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
# custom path for swagger-ui
|
||||
springdoc.swagger-ui.path=/swagger-ui-custom.html
|
||||
|
||||
# custom path for api docs
|
||||
springdoc.api-docs.path=/api-docs
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="DEBUG" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.springdoc;
|
||||
|
||||
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 SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user