Swagger is used to generate documentation for REST APIs.
-> Swagger UI is used to test REST API with user interface.
-> Add below 2 dependencies in pom.xml file.
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
-> Add below property in application.properties file
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
-> Create Swagger Config class like below
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket apiDoc() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("in.ashokit.rest"))
.paths(PathSelectors.any())
.build();
}
}
-> We can access swagger ui using below URL
http://localhost:8080/swagger-ui.html#/
-> We can access swagger documentation using below url
http://localhost:8080/v2/api-dcos
+++++++++++++++++++++++++++++++++++++
CRUD Operations using REST API with MySQL DB
+++++++++++++++++++++++++++++++++++++
-> Download & install MySQL Database (DB Server).
-> Download & install MySQL Workbench (DB client).
SQL Queries
DB client ----------------------------> DB server
-> Develop REST API using Layered Architecture.
1) Web Layer
2) Business / Serice Layer
3) DAO Layer
@Data
@Entity
@Table(name = "BOOK_DTLS")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "BOOK_ID")
private Integer bookId;
@Column(name = "BOOK_NAME")
private String bookName;
@Column(name = "BOOK_PRICE")
private Double bookPrice;
}
public interface BookRepository extends JpaRepository<Book, Serializable>{}
public interface BookService {
public String upsertBook(Book book);
public List<Book> getAllBooks();
public String deleteBook(Integer bookId);
}
@Service
public class BookServiceImpl implements BookService {
private BookRepository repository;
public BookServiceImpl(BookRepository repository) {
this.repository = repository;
}
@Override
public String upsertBook(Book book) {
Integer bookId = book.getBookId();
System.out.println(book);
repository.save(book);
System.out.println(book);
if (bookId == null) {
return "Record Inserted";
} else {
return "Record Updated";
}
}
@Override
public List<Book> getAllBooks() {
return repository.findAll();
}
@Override
public String deleteBook(Integer bookId) {
repository.deleteById(bookId);
return "Book Deleted";
}
}
@RestController
public class BookRestController {
@Autowired
private BookService service;
@PostMapping("/book")
public ResponseEntity<String> addBook(@RequestBody Book book) {
String msg = service.upsertBook(book);
return new ResponseEntity<>(msg, HttpStatus.CREATED);
}
@GetMapping("/books")
public ResponseEntity<List<Book>> getAllBooks() {
List<Book> allBooks = service.getAllBooks();
return new ResponseEntity<>(allBooks, HttpStatus.OK);
}
@PutMapping("/book")
public ResponseEntity<String> updateBook(@RequestBody Book book) {
String msg = service.upsertBook(book);
return new ResponseEntity<>(msg, HttpStatus.OK);
}
@DeleteMapping("/book/{bookId}")
public ResponseEntity<String> deleteBook(@PathVariable Integer bookId) {
String msg = service.deleteBook(bookId);
return new ResponseEntity<>(msg, HttpStatus.OK);
}
}
Comments
Post a Comment