From ddeb32885792cf9340c3a2aeb90d9c0bb6c89505 Mon Sep 17 00:00:00 2001 From: Ihor Date: Sun, 8 Dec 2024 22:18:12 +0200 Subject: [PATCH] add swagger --- pom.xml | 20 ++++++++++++++----- .../impl/RickAndMortyApiClientImpl.java | 8 ++++++-- .../controller/CharacterController.java | 14 +++++++++---- .../service/CharacterService.java | 2 +- .../service/impl/CharacterServiceImpl.java | 4 ++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index a63c34cf..c8a571a8 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.4 + 3.4.0 mate.academy @@ -15,26 +15,26 @@ jv-rick-and-morty 17 - 3.1.1 https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml 0.2.0 1.6.3 1.18.36 + 2.7.0 org.springframework.boot - spring-boot-starter + spring-boot-starter-web org.springframework.boot - spring-boot-starter-web + spring-boot-starter-data-jpa org.springframework.boot - spring-boot-starter-data-jpa + spring-boot-starter-validation @@ -57,6 +57,16 @@ lombok-mapstruct-binding ${lombok.mapstruct.binding.version} + + org.springdoc + springdoc-openapi-starter-webmvc-ui + ${openapi.version} + + + org.springdoc + springdoc-openapi-starter-webflux-ui + ${openapi.version} + org.springframework.boot diff --git a/src/main/java/mate/academy/rickandmorty/client/impl/RickAndMortyApiClientImpl.java b/src/main/java/mate/academy/rickandmorty/client/impl/RickAndMortyApiClientImpl.java index ffb03ad4..e054409b 100644 --- a/src/main/java/mate/academy/rickandmorty/client/impl/RickAndMortyApiClientImpl.java +++ b/src/main/java/mate/academy/rickandmorty/client/impl/RickAndMortyApiClientImpl.java @@ -18,6 +18,7 @@ import mate.academy.rickandmorty.exception.MappingException; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; @Component @RequiredArgsConstructor @@ -35,6 +36,7 @@ public List fetchAllCharacters() { int page = FIRST_PAGE; int status; List responseCharacterDtoList = new ArrayList<>(); + boolean isListFinished = false; do { HttpRequest request = HttpRequest.newBuilder() @@ -53,7 +55,7 @@ public List fetchAllCharacters() { } status = response.statusCode(); - if (status == HttpStatus.OK.value()) { + if (status == HttpStatus.OK.value() && StringUtils.hasText(response.body())) { try { ResponseAllCharactersDto charactersDto = objectMapper .readValue(response.body(), new TypeReference<>() {}); @@ -62,9 +64,11 @@ public List fetchAllCharacters() { throw new MappingException("Can't map response body to " + "ResponseAllCharactersDto object. Body: " + response.body(), e); } + } else { + isListFinished = true; } page++; - } while (status == HttpStatus.OK.value()); + } while (status == HttpStatus.OK.value() && !isListFinished); return responseCharacterDtoList; } diff --git a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java index 9dc1c3a6..3a1c02b5 100644 --- a/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java +++ b/src/main/java/mate/academy/rickandmorty/controller/CharacterController.java @@ -1,10 +1,13 @@ package mate.academy.rickandmorty.controller; +import io.swagger.v3.oas.annotations.Operation; import java.util.List; import lombok.RequiredArgsConstructor; import mate.academy.rickandmorty.dto.internal.InternalCharacterDto; import mate.academy.rickandmorty.service.CharacterService; +import org.springdoc.core.annotations.ParameterObject; import org.springframework.data.domain.Pageable; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -16,14 +19,17 @@ public class CharacterController { private final CharacterService characterService; - @GetMapping("/random") + @Operation(summary = "Get random \"Rick and Morty\" character") + @GetMapping(value = "/random", produces = MediaType.APPLICATION_JSON_VALUE) public InternalCharacterDto getRandomCharacter() { return characterService.getRandomCharacter(); } - @GetMapping + @Operation(summary = "Get \"Rick and Morty\" characters list by name like with pagination") + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public List getCharactersWithNameLike( - @RequestParam("partName") String partName, Pageable pageable) { - return characterService.getAllCharactersWithNameLike(partName, pageable); + @RequestParam("namePart") String namePart, + @ParameterObject() Pageable pageable) { + return characterService.getAllCharactersWithNameLike(namePart, pageable); } } diff --git a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java b/src/main/java/mate/academy/rickandmorty/service/CharacterService.java index 654e532c..61f261fa 100644 --- a/src/main/java/mate/academy/rickandmorty/service/CharacterService.java +++ b/src/main/java/mate/academy/rickandmorty/service/CharacterService.java @@ -10,5 +10,5 @@ public interface CharacterService { InternalCharacterDto getRandomCharacter(); - List getAllCharactersWithNameLike(String partName, Pageable pageable); + List getAllCharactersWithNameLike(String namePart, Pageable pageable); } diff --git a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java index b0942ea2..e57bcc7f 100644 --- a/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java +++ b/src/main/java/mate/academy/rickandmorty/service/impl/CharacterServiceImpl.java @@ -30,9 +30,9 @@ public InternalCharacterDto getRandomCharacter() { } @Override - public List getAllCharactersWithNameLike(String partName, + public List getAllCharactersWithNameLike(String namePart, Pageable pageable) { - return repository.getAllByNameLikeIgnoreCase("%" + partName + "%", pageable) + return repository.getAllByNameLikeIgnoreCase("%" + namePart + "%", pageable) .stream() .map(characterMapper::toDto) .toList();