-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Añadir endpoint con @RepositoryRestController
- Loading branch information
1 parent
352e7a2
commit c6d935e
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/es/lanyu/eventos/rest/PartidoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package es.lanyu.eventos.rest; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.rest.webmvc.PersistentEntityResource; | ||
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler; | ||
import org.springframework.data.rest.webmvc.RepositoryRestController; | ||
import org.springframework.hateoas.CollectionModel; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import es.lanyu.eventos.repositorios.PartidoConId; | ||
import es.lanyu.eventos.repositorios.PartidoDAO; | ||
|
||
// Si se quiere inyectar PersistentEntityResourceAssembler hace falta | ||
// marcar el controlador con @RepositoryRestController | ||
// https://stackoverflow.com/a/51487736 | ||
// Siguiendo la guia de la documentacion: | ||
// https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers | ||
@RepositoryRestController | ||
// Se puede marcar todo el tipo como respuesta web | ||
// @ResponseBody | ||
public class PartidoController { | ||
|
||
private PartidoDAO partidoDAO; | ||
|
||
PartidoController(PartidoDAO partidoDAO) { | ||
this.partidoDAO = partidoDAO; | ||
} | ||
|
||
@GetMapping("/partidos/search/con-nombre-participante") | ||
// Para devolver una coleccion hay que usar CollectionModel<PersistentEntityResource> | ||
// Diferencia entre un recurso individual y una coleccion | ||
// https://docs.spring.io/spring-hateoas/docs/current/reference/html/#fundamentals.resources | ||
@ResponseBody | ||
public CollectionModel<PersistentEntityResource> getPartidosConParticipanteComo(@RequestParam String txt, | ||
// Inyectar PersistentEntityResourceAssembler para devolver como HATEOAS en el metodo | ||
// https://stackoverflow.com/a/31782016 | ||
PersistentEntityResourceAssembler assembler) { | ||
List<PartidoConId> partidos = partidoDAO.getEventosConParticipanteConTexto(txt); | ||
|
||
return assembler.toCollectionModel(partidos); | ||
} | ||
|
||
} |