If you have a Java web application implemented with Java 5 and Spring Framework, it is really easy to expose your POJOs as web services. In this example I use XFire and JSR 181 annotations for that. I’ll also make a small web service client example with PHP
.
The goal is to add web services to the existing Java code with absolute
minimal code addition. I was about to add web service authentication
with Acegi Security, but instead for now, there is no authentication in
this example.
XFire has a quite versatile but scarce user’s guide
. But it is a good start, so start with overviewand quick start. Add XFire libraries and the depencies with the help of a Depency quide
. This example works at least with the following libraries:
xfire-all-1.2.2
activation-1.1
commons-codec-1.3
commons-httpclient-3.0
commons.logging-1.0.4
mail-1.4
jaxen-1.1-beta-9
jdom-1.0
junit-3.8.1
servlet-api-2.3
spring-2.0
stax-api-1.0.1
wsdl4j-1.5.2
xbean-spring-2.5
wstx-3.0.1
XmlSchema-1.1
xfire-jsr181-api-1.0-M1
jaxb-xjc-2.0.1
jaxb-impl-2.0.1
jaxb-api-2.0
aopalliance-1.0
commons-beanutils-1.7.0
XFire 1.2.2 package comes with xbean-spring-2.6. There can be some problemswith that version but at least version 2.5 is working with Spring 2.0.
First, add xfire-servlet.xmlinto WEB-INF directory. Here are the default settings from the user’s manual:
<?xml
version
="1.0"
encoding
="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"> <beans>
public class
MinimizrImpl implements
MinimizrFacade, MinimizrService {
public
String
getString(
)
{ return
"Example string"
;
}
public
String
echoString(
String
string)
{ return
string;
}
public
ExampleObject echoObject(
ExampleObject exampleObject)
{
return
exampleObject;
}
public
List
loadExampleObjectList(
)
{
/* Here you would get list of ExampleObjects for example from database
and return it instead of null */ return null
;
}
}
XFire does not support RPC-encodingbut you can use XFire web services with PHPwith document/literal style of SOAP.
Here is a really simple example to use all the exposed java web services in this example with NuSOAPPHP SOAP library. There are nochecks for errors in the code:
Authentication Added November 14, 2006
: Well, easiest and most
straightforward way to secure web service is to use HTTP
Authentication. It doesn’t need any additional code in the server side.
While still looking for solution to use easily Acegi Security, I’ll add
HTTP Authentication to this example. On the server side you’ll have to
add security constraint into web.xml
:
<security-constraint>
<web-resource-collection>
<web-resource-name>
Protected Minimizr Web Services</web-resource-name>
<url-pattern>
/services/v1/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>
minimizr.webservices.client</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>
BASIC</auth-method>
<realm-name>
Minimizr Realm</realm-name>
</login-config>
<security-role>
<description>
Required roles to use the Web Services</description>
<role-name>
minimizr.webservices.client</role-name>
</security-role>
And couple more lines into the PHP file. Credentials must be added
into the wsdl url and proxy class. Notice that it is quite necessary to
use SSL connection (https) with basic authentication since username and
password are in clear text. You can use useHTTPPersistentConnection
method to use persistent connection, if possible:
Conclusion
It is no brainer to expose Java POJOs as web services with Spring,
XFire and JSR-181 annotations. And it is as easy use those web services
with Java or PHP or other platforms. I guess integrating Acegi Securitywith XFire web services needs a little bit more work. Any suggestions for the easiest way to implement it?