Using Java Web Services with CFMX

by Pete Freitag

JWS files are nothing more than a Java class with a .jws extension. But when placed on a web server with Axis installed (such as ColdFusion MX), you can expose that class as a web service. This works just like CFC's do in ColdFusion. Here's an example JWS we will call CaseService.jws:

public class CaseService
{
	public String toUpper(String str)
	{
		return str.toUpperCase();
	}
	public String toLower(String str)
	{
		return str.toLowerCase();
	}
}

If we add a ?wsdl to the url, it displays the WSDL for the service just like in CFMX. Let's try to invoke it in CFML:

<cfset case = CreateObject("webservice", "http://localhost:8500/CaseService.jws?wsdl")>
<cfset result = case.toUpper("Case Me uP!")>
<cfoutput>#result#</cfoutput>

It should display CASE ME UP!

This may be a better way to go than CFC's if your exposing Java classes as web services in CFMX. JWS files appear to work on both Pro and Enterprise versions of CFMX.

The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.

Comments

Paul Kenney

By the way, this is a function of the underlying JRUN J2EE server, and is not supported on other J2EE platforms. It realy has nothing to do with CFMX. I just through I should share. Really cool, though. One more way to leverage Java within CF--without having to deploy JAR files!

Pete Freitag

This is actually part of Apache AXIS, so if you set things up properly you can use it with any Servlet container (I have it working well with tomcat).

Patrick Whittingham

Can this webservice be on a different server and app server (ie., Oracle 9iAS).

Pete Freitag

Patrick, yes the web service can be on any server with JSP support (ie tomcat, any j2ee server). You just need to download apache axis, and add the jar files to your lib directory, and setup a servlet mapping for .jws files to point to the Axis servlet.

hjubal

k, that's seem to work, despite of Coldfusion. How about returning more complex objects, such as custom classes?

dan

I started out answering a column metadata question and got this interesting little program to work. Its a way to dynamically load classes from source code using JWS and classloaders http://www.cfide.org/getColumnLength.html