0

I have a Jersey Application and I am trying to redirect to a html page which shows the api console when I access "/apiconsole" endpoint as shown below.

/**
 * Redirect to API Console
 *
 * @return API Console
 */
@GET
@Path("/apiconsole")
public Response redirectToApiConsole() {
    //redirect path from baseURI to the api console
    URI redirectedURL = UriBuilder.fromPath("/api/console/index.html").build();
    return Response.seeOther(redirectedURL).build();
}

Is this the same as doing this in Spring?

/**
 * Redirect to API Console 
 *
 * @return API Console
 */
@RequestMapping(value = "/apiconsole", method = RequestMethod.GET)
public View redirectToApiConsole() {
    return new RedirectView("/api/console/index.html?raml=/api/console/api.raml");
}
daredadevil
  • 317
  • 1
  • 6
  • 19

1 Answers1

0

You'll have to use Viewable to acheive this and have to update the web.xml to include the servlet filter instead of servlet. I have provided an answer here and here.Hope it helps.

Community
  • 1
  • 1
s7vr
  • 73,656
  • 11
  • 106
  • 127
  • I get an error `o.g.j.m.i.WriterInterceptorExecutor : MessageBodyWriter not found for media type=text/html, type=class org.glassfish.jersey.server.mvc.Viewable, genericType=class org.glassfish.jersey.server.mvc.Viewable.` – daredadevil Jun 01 '16 at 15:43
  • I added the following config Jersey Config : `property(ServletProperties.FILTER_STATIC_CONTENT_REGEX,"/((api/console/.*))");` customConfig: `property(MvcFeature.TEMPLATE_BASE_PATH,"/api/console/"); register(org.glassfish.jersey.server.mvc.MvcFeature.class);` and simply return this `return new Viewable("index");` – daredadevil Jun 01 '16 at 15:51
  • TEMPLATE_BASE_PATH should be the location of directory of html files. Something like '/WEB-INF' and then change the new Viewable to ("/index"). Also, get rid of the ServletProperties.FILTER_STATIC_CONTENT_REGEX property. – s7vr Jun 01 '16 at 16:42
  • My static resources scripts and css are within src/main/java/resources/static/api/console/ folder and so is my index.html. and I am using this FILTER_STATIC_CONTENT_REGEX property to allow jersey to serve up all files under /api/console/ directory. Without this I am unable to view my static resources from local host directly. Note that I am not using FILTER_FORWARD_ON_404 filter. Thanks for your help! – daredadevil Jun 01 '16 at 18:31
  • MvcFeature ust provides the mvc supporting infrastructute and you'll have to implement your custom support for a templating engine. Jersey provides template implementation for jsp, mustache and free marker templates. Please read this link https://jersey.java.net/documentation/latest/mvc.html. You can use JspMvcFeature instead of MvcFeature to serve html pages. – s7vr Jun 01 '16 at 22:31
  • If you're only looking for a redirect than take a look at this example here. http://stackoverflow.com/questions/27728476/jersey-rest-webservice-redirecting-to-the-same-page – s7vr Jun 01 '16 at 22:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113648/discussion-between-shiv-v-and-daredadevil). – s7vr Jun 02 '16 at 13:51