RESTful en Spring Boot

En este otro post hablo de los servicios web, este solo contiene un ejemplo de una estructura de directorios y las anotaciones empleadas en Spring Boot.

Las anotaciones pricipales en Spring Boot son:

@Entity para las clases entidad, que seran propagadas a la base de datos. Generalmente una entidad corresponde a una tabla de la base de datos. @RestController establece una clase como REST API. Es la combinación de @Controller y @ResponseBody. @GetMapping para manejar peticiones GET, funciona como un atajo de @RequestMapping.

Dog.java @Entity public class Dog { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String breed; private String origin; public Dog(Long id, String name, String breed, String origin) { this.id = id; this.name = name; this.breed = breed; this.origin = origin; } public Dog(String name, String breed) { this.name = name; this.breed = breed; } public Dog() {} public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBreed() { return breed; } public void setBreed(String breed) { this.breed = breed; } public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } }
DogRepository.java public interface DogRepository extends CrudRepository { @Query("select d.id, d.breed from Dog d where d.id=:id") String findBreedById(Long id); @Query("select d.id, d.breed from Dog d") List findAllBreed(); @Query("select d.id, d.name from Dog d") List findAllName(); }
DogNotFoundException.java import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Dog not found") public class DogNotFoundException extends RuntimeException { public DogNotFoundException() { } public DogNotFoundException(String message) { super(message); } }
DogService.java public interface DogService { List retrieveDogs(); List retrieveDogBreed(); String retrieveDogBreedById(Long id); List retrieveDogNames(); }
DogServiceImpl.java @Service public class DogServiceImpl implements DogService { @Autowired DogRepository dogRepository; public List retrieveDogs() { return (List) dogRepository.findAll(); } public List retrieveDogBreed() { return (List) dogRepository.findAllBreed(); } public String retrieveDogBreedById(Long id) { Optional optionalBreed = Optional.ofNullable(dogRepository.findBreedById(id)); String breed = optionalBreed.orElseThrow(DogNotFoundException::new); return breed; } public List retrieveDogNames() { return (List) dogRepository.findAllName(); } }
DogController.java @RestController public class DogController { private DogService dogService; @Autowired public void setDogService(DogService dogService) { this.dogService = dogService; } @GetMapping("/dogs") public ResponseEntity> getAllDogs() { List list = dogService.retrieveDogs(); return new ResponseEntity>(list, HttpStatus.OK); } @GetMapping("/dogs/breed") public ResponseEntity> getDogBreeds() { List list = dogService.retrieveDogBreed(); return new ResponseEntity>(list, HttpStatus.OK); } @GetMapping("/{id}/breed") public ResponseEntity getBreedByID(@PathVariable Long id) { String breed = dogService.retrieveDogBreedById(id); return new ResponseEntity(breed, HttpStatus.OK); } @GetMapping("/dogs/name") public ResponseEntity> getDogNames() { List list = dogService.retrieveDogNames(); return new ResponseEntity>(list, HttpStatus.OK); } }

Comentarios

Entradas populares de este blog

Envío de checkboxes o selector multiple por AJAX con jQuery

Conceptos básicos de la POO

Eclipse: Failed to create the Java Virtual Machine