JSF / JSP stop working after adding JAX-RS

65 Views Asked by At

My web app stop working after I added the following JAX-RS class to the project.

@Path("/ws")
public class ResourceLocator {

private static final String ACCOUNT_RESOURCE  = "account";

public ResourceLocator() {
}


@Path("/{resource}")
public Object findWebServiceResource(@PathParam("resource") String resource) {
    
    Object resourceObj = null;
    try {
        resourceObj = getResourceObject(resource);
    }
    catch (Exception ex) {
        Log.category.error("Exception occur when attempting to find Web Service Resource", ex);
        resourceObj = new ResourceLocatorException(ex); 
    }
    
    return resourceObj; 
}
. . . 

The new web-services I added works fine. But it causes the web site to stop working. When I access the site, I see this error in the console.

WARNING: javax.ws.rs.WebApplicationException
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:162)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:91)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:240)
at org.apache.openejb.server.cxf.rs.CxfRsHttpListener.doInvoke(CxfRsHttpListener.java:227)
at org.apache.tomee.webservices.CXFJAXRSFilter.doFilter(CXFJAXRSFilter.java:94)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241
. . . . 

The error seems to tell me that the JAX-RS servlet is intercepting request that is supposed to be handled by the MyFaces and JSP servlets. I think I need to add some servlet mappings in web.xml to correct this, but I'm not sure what to add. Below is the relevant portion of my web.xml:

<servlet>
    <description>
    </description>
    <display-name>MyFacesServlet</display-name>
    <servlet-name>MyFacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup> 
</servlet>

    <servlet-mapping>
    <servlet-name>MyFacesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

I tried adding below, but it causes both the web-site and the ws to stop working:

<servlet>
    <servlet-name>restServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
      <param-name>jaxrs.serviceClasses</param-name>
        <param-value>com.arch.myaccount.ws.ResourceLocator</param-value>
    </init-param>
</servlet>
<servlet-mapping>
   <servlet-name>restServlet</servlet-name>
   <url-pattern>/ws/*</url-pattern>
</servlet-mapping>
1

There are 1 best solutions below

0
HockChai Lim On

It works after I removed @Path("/ws") from the ResourceLocator class and add the following to web.xml

<servlet>
    <servlet-name>restServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
      <param-name>jaxrs.serviceClasses</param-name>
        <param-value>com.arch.myaccount.ws.ResourceLocator</param-value>
    </init-param>
</servlet>

    <servlet-mapping>
   <servlet-name>restServlet</servlet-name>
   <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

My ResourceLocator class now looks like below:

public class ResourceLocator {

private static final String ACCOUNT_RESOURCE  = "account";

public ResourceLocator() {
}


@Path("/{resource}")
public Object findWebServiceResource(@PathParam("resource") String resource) {
    
    Object resourceObj = null;
    try {
        resourceObj = getResourceObject(resource);
    }
    catch (Exception ex) {
        Log.category.error("Exception occur when attempting to find Web Service Resource", ex);
        resourceObj = new ResourceLocatorException(ex); 
    }
    
    return resourceObj; 
}