2014-09-11 4 views
26

Diciamo che ho due repository:risposta su misura per la richiesta radice int primavera REST hateoas sia con RepositoryRestResource-s e controllori regolari

@RepositoryRestResource(collectionResourceRel = "person", path = "person") 
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> { 
    List<Person> findByLastName(@Param("name") String name); 
} 

e

@RepositoryRestResource(collectionResourceRel = "person1", path = "person1") 
public interface PersonRepository1 extends PagingAndSortingRepository<Person1, Long> { 
    List<Person1> findByLastName(@Param("name") String name); 
} 

con un controller normale:

@Controller 
public class HelloController { 
    @RequestMapping("/hello") 
    @ResponseBody 
    public HttpEntity<Hello> hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name) { 
     Hello hello = new Hello(String.format("Hello, %s!", name)); 
     hello.add(linkTo(methodOn(HelloController.class).hello(name)).withSelfRel()); 
     return new ResponseEntity<>(hello, HttpStatus.OK); 
    } 
} 

Ora, una risposta per http://localhost:8080/ è:

{ 
    "_links" : { 
    "person" : { 
     "href" : "http://localhost:8080/person{?page,size,sort}", 
     "templated" : true 
    }, 
    "person1" : { 
     "href" : "http://localhost:8080/person1{?page,size,sort}", 
     "templated" : true 
    } 
    } 
} 

ma voglio ottenere qualcosa del genere:

{ 
    "_links" : { 
    "person" : { 
     "href" : "http://localhost:8080/person{?page,size,sort}", 
     "templated" : true 
    }, 
    "person1" : { 
     "href" : "http://localhost:8080/person1{?page,size,sort}", 
     "templated" : true 
    }, 
    "hello" : { 
     "href" : "http://localhost:8080/hello?name=World" 
    } 
    } 
} 

risposta

29
@Component 
public class HelloResourceProcessor implements ResourceProcessor<RepositoryLinksResource> { 

    @Override 
    public RepositoryLinksResource process(RepositoryLinksResource resource) { 
     resource.add(ControllerLinkBuilder.linkTo(HelloController.class).withRel("hello")); 
     return resource; 
    } 
} 

basano su

1

È necessario disporre di un ResourceProcessory per la vostra risorsa persona registrata come Bean. see https://stackoverflow.com/a/24660635/442773

+2

Ma io non voglio cambiare la risposta per una delle 'http: // localhost: 8080/Person' , 'http: // localhost: 8080/person1' o' http: // localhost: 8080/hello' richieste. Voglio cambiare la risposta generata automaticamente per la richiesta 'http: // localhost: 8080 /'. – jcoig

Problemi correlati