Avi
Avi

Reputation: 21866

Dynamic web page with RESTful content provider

Maybe it's a newbie question so sorry in adavnce :)

I'm having a REST API that returns JSON objects. I'm trying to build a web-site with 2 page:

  1. First page presents a table that presents article titles (let's say we have 'article id', 'author name', 'title'). The 'article id' column is clickable
  2. Second page presents a single article.

I want to be able to click on the id column from the first page, perform a GET request to get the JSON object for the article and then present it nicely in the single-article page. I want the url after the GET request to be something like: `http://[web-server-name]/article/[id]

If I'm writing a static page the uses jQuery to fetch the JSON object then the url will not be in the resources format I'm looking. If I'll return HTML from the REST server it will be ugly both to maintain such page and to couple the logic with the presentation.

What would be the correct way of doing it? Thanks!

Upvotes: 2

Views: 1245

Answers (1)

gma
gma

Reputation: 2563

You can have to methods declared at the same @Path but that @Produces two different mime types, the good one is selected accordingly to the Accept header sent by the client:

@GET
@Path("/article/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getArticle(@PathParam("id") long id) {
       Article myArticle = getArticleById(id);
       return Response.ok(myArticle);
}

@GET
@Path("/article/{id}")
@Produces(MediaType.TEXT_HTML)
public Response getArticleHtml(@PathParam("id") long id) {
    InputStream myHtml = getClass().getClassLoader().getResourceAsStream("/path/to/html.html");
    return Response.ok(myHtml).build();
}

But that let your Jax-RS implementation serve static resources for which it is not designed to. Or you can use a Single Page Javascript framework like AngularJS

Upvotes: 3

Related Questions