Using Java Web Services with CFMX

January 21, 2004
coldfusionjava

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.



Related Entries

4 people found this page useful, what do you think?

Comments

Wow! What a discovery. This opens up a whole new set of options for developing web services. How did you find this out? I wonder if this is officially "supported" by the Macromedia crew. Thanks Steve
just for fun, I tried this out. I have no java experience whatsoever, but I do have eclipse installed so I gave it a go. I made a simple hello world class, compiled it, and then moved the .class into my web root and renamed it with a .jws extension. When I try to get the wsdl, AXIS throws: ---------------------------------------- Sorry, something seems to have gone wrong... here are the details: Fault - Error while compiling: C:\CFusionMX\wwwroot\WEB-INF/jwsClasses\helloWorld.java AxisFault faultCode: {http://xml.apache.org/axis/}Server.compileError faultSubcode: faultString: Error while compiling: C:\CFusionMX\wwwroot\WEB-INF/jwsClasses\helloWorld.java faultActor: faultNode: faultDetail: {}Errors: Error compiling C:\CFusionMX\wwwroot\WEB-INF/jwsClasses\helloWorld.java: Line 0, column 0: Please ensure that you have your JDK's rt.jar listed in your classpath. Jikes needs it to operate. Line 0, column 0: Please ensure that you have your JDK's rt.jar listed in your classpath. Jikes needs it to operate. Line 0, column 0: Please ensure that you have your JDK's rt.jar listed in your classpath. Jikes needs it to operate. ------------------------------------- I went into cf administrator and added a direct classpath to rt.jar but that didn't help. Any suggestions (it's probably something simple... but this really is "hello world" in java for me).
never mind, I figured it out (need to rename the .java to .jws, not the compiled class). Cool! btw- seems you may be having the cfqueryparam double quote bug on this blog. Test's.
Can cfmx invoke this web service if is on a different web server?
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!
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).
Can this webservice be on a different server and app server (ie., Oracle 9iAS).
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.
This is really cool thing. I have tried it. but getting an error. it might be something problem in my CFMX7.0 setting. here I am giving the error. Do you have any idea about this. Could not generate stub objects for web service invocation. Name: http://w049228/_com/CaseService.jws?wsdl. WSDL: http://w049228/_com/CaseService.jws?wsdl. org.xml.sax.SAXException: Fatal Error: URI=null Line=1: Content is not allowed in prolog. It is recommended that you use a web browser to retrieve and examine the requested WSDL document for correctness. If the requested WSDL document can't be retrieved or it is dynamically generated, it is likely that the target web service has programming errors. The error occurred in D:\WEBSPACE\BEHRINGER\raffle_stat\invoke.cfm: line 27 25 : ----> 26 : 27 : <cfset case = CreateObject("webservice", "http://w049228/_com/CaseService.jws?wsdl")> 28 : <cfset result = case.toUpper("Case Me uP!")> 29 : <cfoutput>#result#</cfoutput>
I've tried Using Java Web Services with CFMX as with the examples here but, get "Could not generate stub objects for web service invocation" is there anything I need to do (setting, etc) before this will work with CFMX and/or Java? Thanks in advance Paul
k, that's seem to work, despite of Coldfusion. How about returning more complex objects, such as custom classes?
I don't believe this process can reply with anything but String data. Can anyone test this out and send me an email? 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

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?