Home > database >  Spring Boot - GET atributes in common from repository jpa
Spring Boot - GET atributes in common from repository jpa

Time:10-25

I started working with spring boot's rest API and ended up having some specific problems returning the last two topics

1- GET animals/number/{number} You should list the animals with the number/code {number}

2- GET animals/name/{name} You should list the animals with the name {name}

3- GET animals/species/{species} You should list the animalsof the species {species}. Note, more than one animal can be returned for each species.

4- GET animals /type/{type} You should list the animals of the type {type}. Note, more than one animal can be returned for each type. Due to the nature of this field, you should perform a substring search. For example, the value “poison” for the {type} should return the animals with the type "reptile/Poison".

what I got

     @RequestMapping(value="/animals/number/{number}", method=RequestMethod.GET)
     public ResponseEntity<?> getNumber(@PathVariable(name = "number") String number) {        
        Optional<Animal> o = repository.findByNumber(number);

        if (!o.isPresent())
            return new ResponseEntity<>(o, HttpStatus.NOT_FOUND);

        return new ResponseEntity<>(o, HttpStatus.FOUND);
    }

     @RequestMapping(value="/animals/name/{name}", method=RequestMethod.GET)
        public ResponseEntity<?> getName(@PathVariable(name = "name") String name) {
            Optional<Animal> o = repository.findByName(name);
    
            if (!o.isPresent())
                return new ResponseEntity<>(o, HttpStatus.NOT_FOUND);
    
            return new ResponseEntity<>(o, HttpStatus.FOUND);
        }

I tried to do topic 3 but I'm not able to:

    @RequestMapping(value="/animals/species/{species}", method=RequestMethod.GET)
        public ResponseEntity<?> getSpecies(@PathVariable(name = "species") String species) {
          
       List<Animal> p = repository.findAll();

      if (species == null)
        repository.findAll().forEach(p::contains);
      else
        repository.findByTitleContaining(species).forEach(p::contains);

      if (p.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
      }

      return new ResponseEntity<>(p, HttpStatus.OK);
    }
    @Repository
    public interface AnimalRepository extends JpaRepository<Animal, Integer> {
    
        Optional<Animal> findByNumber(String number);
        Optional<Animal> findByName(String name);
        Optional<Animal> findByspecie(String species);
}

i put for test //localhost:8081/animals/name/Animalname

CodePudding user response:

You can query all animals matching the searched specie using a Spring Data generated query (as the one you seem to have defined):

List<Animal> findByTitleContaining(String specie);

Then you can group the returned elements using the java.util.stream.Collectors#groupingBy using the Animal types:

@RequestMapping(value="/animals/species/{species}", method=RequestMethod.GET)
public ResponseEntity<?> getSpecies(@PathVariable(name = "species") String species) {
          
    List<Animal> matchingAnimals = repository.findByTitleContaining(species);

    if (!matchingAnimals.isEmpty()) {
        final Map<String, List<Animal>> groupedAnimals = matchingAnimals.stream()
            .collect(Collectors.groupingBy(Animal::getType));  
        return new ResponseEntity<>(groupedAnimals, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}

CodePudding user response:

Thanks for answering --Tmarwen

GET animals/species/{species} You should list the animalsof the species {species}. Note, more than one animal can be returned for each species.

my code didn't work, it just returned [] empty

I don't know if I'm doing the query on the wrong url or what, I started it just a week ago lol

  • Related