K0NRAD
9/7/2015 - 11:17 AM

Get URI info for HATEOAS in RESTful services

Get URI info for HATEOAS in RESTful services

@Provider
@Path("/preferences")
@Api("/preferences")
public class PreferenceResource {

    @Inject
    private PreferenceController preferenceController;

    @Context 
    private UriInfo uriInfo;

    @GET
    @Path("/")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(
            position = 0,
            value = "findAll",
            notes = "Find all preferences",
            response = Preference.class,
            responseContainer = "List"
    )
    public Response findAll() {
        String uri = uriInfo.getBaseUriBuilder()
                .path(this.getClass())
                .build()
                .toString();

        System.out.println("uriInfo. = " + uri);
        // do something with uri e.g. 
        //    Map<String,String> links = new HashMap<>();
        //    links.add(uri, "self");
        return Response.ok(preferenceController.findAll()).build();
    }
}