Skip to content

📚 Spring Boot 2 Essentials 07 - Método GET #58

@JayCesar

Description

@JayCesar

Assim mostra o status da requisição, bom para testes.

  @GetMapping
    public ResponseEntity<List<Anime>> list() {;
        return new ResponseEntity<>(animeService.listAll(), HttpStatus.OK);
        // return ResponseEntity.ok(animeService.listAll()); // outro jeito de fazer
    }

By adding / changing the application to yml extension, I can avoid a stack of errors shows up to the frontend:

On application.yml file:

server:
  error:
    include-stacktrace: never

There are others optiosn to add to include-stacktrace!

AnimeService file:

@Service
public class AnimeService {
    private List<Anime> animes = List.of(new Anime(1L, "Dragon Ball (Test - Build - Hot Swap)"), new Anime(2L, "Bersek"));
    public List<Anime> listAll() {
        return animes;
    }
    public Anime findById(long id) {
        return animes.stream()
                .filter(anime -> anime.getId().equals(id))
                .findFirst()
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "Anime not found"));
    }
}

image

image

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions