/java/

JEE + JAX-RS + Fetch data as component from REST API

2016-02-06 15:36:30

If your fronted is build - based on component approach, you can consider to prepare your rest API to work with components also.

Let's assume that you have something like this on fronted.

Then it will be good to fetch data for this components like this:

@RequestMapping(value = {"/component/product/{id}/author"})
public Component componentAuthor(@PathVariable("id") long id) {
    return productRepository.getOne(id)
            .componentProvider()
            .add(new AuthorProductComponent())
            .buildOne();
}

@RequestMapping(value = {"/component/product/{id}/brand"})
public Component componentBrand(@PathVariable("id") long id) {
    return productRepository.getOne(id)
            .componentProvider()
            .add(new BrandProductComponent())
            .buildOne();
}

@RequestMapping(value = {"/component/product/{id}/info"})
public Component componentInfo(@PathVariable("id") long id) {
    return productRepository.getOne(id)
            .componentProvider()
            .add(new InfoProductComponent())
            .buildOne();
}

or if you would like to fetch all data in one call, you can use this

@RequestMapping(value = {"/component/product/{id}/all/LIST"})
public List

<component> componentAll(@PathVariable("id") long id) {
    return productRepository.getOne(id)
            .componentProvider()
            .add(new BrandProductComponent())
            .add(new InfoProductComponent())
            .add(new AuthorProductComponent())
            .buildList();
}

There are more possibilities and everything is on the GitHub <strong>update 2016-03-13</strong> If you want to play with security try this GitHub Component V3

And more, you have to admit that v2 and v3 are solution were you connect your core / business logic with presentation layer.

In GitHub Component V4, I removed that problem.