How to redirect a JAX-RS method to a JSF page?

I have a JAX-RS resource, and after solving the business logic, I want to show the results on the JSF page. How can i do this?

@Path("/rest") public class PaymentServiceRest { @GET @Path("/status") public String method() { // Business logic... return "results.xhtml"; // how to return a jsf page? } } 

The first time the client accesses the application, use url, i.e. http://myApp/rest/status , then follow some logic and based on this, do a redirect.

+7
redirect java-ee jsf jax-rs
source share
3 answers

Well, I found a way to forward from the JAX-RS method to the JSF page:

 @GET @Path("/test") @Produces("text/html") public Response test(@Context ServletContext context, @Context HttpServletRequest request, @Context HttpServletResponse response) { try { String myJsfPage = "/response.xhtml"; context.getRequestDispatcher(myJsfPage).forward(request, response); } catch (ServletException | IOException ex) { return Response.status(NOT_FOUND).build(); } return null; } 

As described here: https://www.java.net//forum/topic/glassfish/glassfish/forwarding-jsf-jax-rs

Injection through the method can also be performed by injection in the fields, which is a matter of "preference"

This has been tested in TomEE (Apache CXF). I'm just a little curious if this is just a dirty โ€œhackโ€ or if there is a better way to do this.


UPDATE

I found a better redirect method that displays JSF tags without any problems (in my case, tags like <p:graphicImage/> did not display properly)

 @GET @Path("/test") @Produces("text/html") public Response test(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException { String myJsfPage = "/response.xhtml"; String contextPath = request.getContextPath(); response.sendRedirect(contextPath + myJsfPage); return Response.status(Status.ACCEPTED).build(); } 
+7
source share

You can choose between these two solutions:

 import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; @Path("/rest") public class PaymentServiceRest { @Context private HttpServletRequest request; @Context private HttpServletResponse response; /** * This method uses the injected request/response in the PaymentServiceRest class * @return * @throws IOException */ @GET @Path("/status1") @Produces("text/html") public Response method1() throws IOException { String myJsfPage = "/views/index.xhtml"; String contextPath = request.getContextPath(); response.sendRedirect(contextPath + myJsfPage); return Response.status(Status.ACCEPTED).build(); } /** * This method uses the injected request/response passed as parameters * @return * @throws IOException */ @GET @Path("/status2") @Produces("text/html") public Response method2(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException { String myJsfPage = "/views/index.xhtml"; String contextPath = request.getContextPath(); response.sendRedirect(contextPath + myJsfPage); return Response.status(Status.ACCEPTED).build(); } } 
0
source share

Java class file

 public String processPage1(){ return "page"; } 

in faces-config.xml

 <navigation-case> <from-action>#{navigationController.processPage1}</from-action> <from-outcome>page</from-outcome> <to-view-id>page1.jsf</to-view-id> </navigation-case> 

visit the link

-one
source share

All Articles