Thursday, March 9, 2017

ColdFusion 2016 Broke My API (And How I Fixed It)

While on ColdFusion 9, I built a REST API for our an application at work. Everything was working fine until we upgraded to ColdFusion 2016. When I went to test the API, I got an HTTP 500 error. The Apache log gave me no clues to what was happening, so my next stop was the ColdFusion logs. In the exception log, I found this:
Error","ajp-nio-8015-exec-9","03/09/17","07:22:12",,"Application  could not be found. The specific sequence of files included or processed is: '''' "
javax.servlet.ServletException: Application  could not be found.
        at coldfusion.rest.servlet.CFRestServlet.invoke(CFRestServlet.java:512)
        at coldfusion.rest.servlet.RestFilter.invoke(RestFilter.java:60)
        at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
        ...
A quick Google search did not reveal much. However, the fact that it was calling the CFResetServlet gave me an idea. What if the /api directory now had special meaning? So I renamed the /api directory to /API-TEST and sure enough my code was working again.
I found the solution in the web.xml file:
    <servlet-mapping id="coldfusion_mapping_16">
        <servlet-name>CFRestServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
This mapping is forcing everything in the /api directory to be processed by the ColdFusion REST service. To fix this, I simply commented out this section of the configuration and restarted the ColdFusion service.
    <--
    <servlet-mapping id="coldfusion_mapping_16">
        <servlet-name>CFRestServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
    -->

3 comments:

Charlie Arehart said...

Nice job, Chad. Thanks. I'll be pointing others to this when they get that error.

FWIW, folks would get an error saying "application whatever could not be found" if they were asking for /api/whatever/somefile.

And of course, this workaround is only suited to people who know that they will never want to leverage CF's REST feature using such a /api URL. But it will indeed work for them until perhaps Adobe comes up with some better alternative.

Chad said...

Thanks for the feedback! And good point about utilizing CF's REST feature. If I were writing a new app from scratch or overhauling the code on this app, I think it would definitely make more sense to use the built-in features of ColdFusion.

Charlie Arehart said...

Well, some may quibble about whether it "makes sense" to go that route. :-) But my point was just to reiterate (for the sake of those who may consider the tweak you offered) that in doing it, that WOULD now break the documented CF feature, at least in terms of *its* offer to use an /api URL (where there really *is none* in a given web site).

I'll throw out as well that if someone wants a solution that's instead on a site-by-site basis, someone offered such a solution using web server rewrites:

https://stackoverflow.com/questions/39903025/coldfusion-2016-can-you-have-a-folder-in-your-web-root-named-api-or-rest

Post a Comment

 
Blogger Templates