Clara Clara - 1 month ago 12
Java Question

Empty rest path config, cant access app page

I configured restEasy for creating api services in my java application, as follows

Added dependencies in pom

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.2.1.GA</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.2.0.GA</version>
</dependency>


Created a rest config class as follows,

@ApplicationPath("/")
public class RestClass extends Application {
private Set<Object> singletons = new HashSet<Object>();

public RestClass() {
singletons.add(new Stimulant());
}

@Override
public Set<Object> getSingletons() {
return singletons;
}
}


This is my service class which provides json data,

@Path(value = "/")
public class Stimulant {

@GET
@Path(value = "/{param}")
@Produces(MediaType.APPLICATION_JSON)
public Response getMsg(@PathParam(value = "param") String appId) {
String token = documentDAO.mobileAppToken(appId);
return Response.status(200).entity(token).build();
}


I am able to get the json data while hitting the url
localhost:8080/myApp/{whatever}


Problem is When I am accessing my app's home page, it doesn't shows the page (
localhost:8080/myApp
). It went to api call instead of opening app home page.

How do I come out from this problem?

Answer

You should do this with Filter servlet.

Let us continue with your rest config as "/f". When hitting url in browser you can remove the /f and forward your request with appending the "/f" where it needs.

simple code,

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        if (request instanceof HttpServletRequest) {
        String path = ((HttpServletRequest) request).getServletPath();
        String newPath = null;

                newPath = "f" + path;
            }

        } else {
            newPath = path;
        }
        request.getRequestDispatcher(newPath).forward(request, response);
        return;
    }
    chain.doFilter(request, response);
}