4

I'm currently developing a Spring Boot application that exposes endpoints using @RestController and @RequestMapping annotations.

I recently discovered the concept of the projections as defined in Spring Data Rest (@Projection-annotated interfaces and @RepositoryRestResource-annotated JPA repositories) and I'd like to apply this concept to my existing services.

As I understand this post Spring Boot Projection with RestController, (please correct me if I'm wrong), @RestController and @RepositoryRestResource classes both define endpoints. So these annotations seem quite incompatible.

Is there a Spring component which can simply apply the projections concept to the @RestController endpoints ?

Is there a way to manually reroute requests from a endpoint to another ? (for example, using @RestController endpoints as some sort of proxy which performs controls or other operations before rerouting the request to the @RepositoryRestResource endpoints)

EDIT: I add a glimpse of the final code I'd like to have in the end.

@RestController
public class MyController {

    @RequestMapping(value = "/elements/{id}", method = RequestMethod.GET)
    public ResponseEntity<Element> getElements( 
            @PathVariable("id") Integer elementId,
            @RequestParam("projection") String projection,
            @RequestHeader(value = "someHeader") String header{

        // [manual controls on the header then call to a service which returns the result]

    }
}

@Entity
public class Element {

    private Integer id;

    private String shortField;

    private String longField;

    private List<SubElement> subElements;

    // [Getters & setters]

}

@Projection(name = "light", types = {Element.class})
interface ElementLight {

    public Integer getId();
    public String getShortField();

}

If I call /elements/4, I'd get the complete Element having id = 4. If I call /elements/4?projection=light, I'd get only the id and the short field of the Element having id = 4.

Community
  • 1
  • 1
Eria
  • 2,653
  • 6
  • 29
  • 56
  • Can you explain what your goal is? `@RestController`s are for manually creating any RESTful endpoint while `@RepositoryRestResource` is for exposing a respository more or less 1:1 as RESTful endpoint so you don't have to do that manually. The `@Projection` annotation a spring-data specific annotation that allows to customize how entities get exposed. There is a more generic way to use views on Json objects and that is Jackson's `@JsonView`: http://stackoverflow.com/a/30307335/995891 - is that what you're after? Wrt, rerouting, you can `@Autowire` the other contoller and call the java method. – zapl Dec 21 '15 at 11:39
  • @zapl My goal is to expose RESTful endpoints containing custom code but with the concept of projections as it is implemented in Spring Data. I want to expose several "versions" of an entity, but keeping the control on what happens between the endpoint and the database. I'd like to expose these different projections of an entity through the same URL, depending on a request parameter containing the projection wanted. `@JsonView` could be interesting : I will look into that, thank you. Though it might not be exactly what I need. – Eria Dec 21 '15 at 13:06
  • I just edited my question to add details about the result I expect. – Eria Dec 21 '15 at 13:22

1 Answers1

2

This answer gives some detail about how to create projection instances of your entities - https://stackoverflow.com/a/29386907/5371736

So depending on your projection parameter you could generate the given projections.

Hope this is what you are looking for.

Community
  • 1
  • 1
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
  • Not really what I'm looking for, but thnaks. As I understand it, this is based on a method provided by the class Page, which I don't use currently. But it could be a workaround if I don't find another solution. – Eria Jan 04 '16 at 10:29