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 overview and 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 1.2.2 package comes with xbean-spring-2.6. There can be some problems with that version but at least version 2.5 is working with Spring 2.0.
First, add xfire-servlet.xml into 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> <import resource ="classpath:org/codehaus/xfire/spring/xfire.xml" /> <bean id ="jaxbTypeMappingRegistry" class ="org.codehaus.xfire.jaxb2.JaxbTypeRegistry" init-method ="createDefaultMappings" singleton ="true" /> <bean id ="webAnnotations" class ="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /> <bean id ="handlerMapping" class ="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping" > <property name ="typeMappingRegistry" > <ref bean ="jaxbTypeMappingRegistry" /> </property> <property name ="xfire" > <ref bean ="xfire" /> /property> <property name ="webAnnotations" > <ref bean ="webAnnotations" /> </property> </bean> <bean class ="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" > <property name ="urlMap" > <map> <entry key ="/" > <ref bean ="handlerMapping" /> </entry> </map> </property> </bean> </beans>
Add xfire-servlet.xml into the Spring’s contextConfigLocation and XFireServlet in web.xml file:
<context-param> <param-name> contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml /WEB-INF/xfire-servlet.xml </param-value> </context-param> <servlet> <servlet-name> XFireServlet</servlet-name> <display-name> XFire Servlet</display-name> <servlet-class> org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> XFireServlet</servlet-name> <url-pattern> /servlet/XFireServlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> XFireServlet</servlet-name> <url-pattern> /services/v1/*</url-pattern> </servlet-mapping>
Let’s have a simply MinimizrFacade.java Java interface:
package com.minimizr.service ; import java.util.List ; import com.minimizr.service.domain.ExampleObject ; public interface MinimizrFacade { String getString( ) ; String echoString( String string) ; ExampleObject echoObject( ExampleObject exampleObject) ; List< ExampleObject> loadExampleObjectList( ) ; }
And let’s have another MinimizrService.java Java interface for web services:
package com.minimizr.service ; import java.util.List ; import javax.jws.WebService ; import com.minimizr.service.domain.ExampleObject ; @WebService public interface MinimizrService { String getString( ) ; String echoString( String string) ; ExampleObject echoObject( ExampleObject exampleObject) ; List< ExampleObject> loadExampleObjectList( ) ; }
And for this example a ExampleObject.java Java object:
package com.minimizr.service ; public class ExampleObject { private String name; private Integer age; public Integer getAge( ) { return age; } public void setAge( Integer age) { this .age = age; } public String getName( ) { return name; } public void setName( String name) { this .name = name; } }
And finally a MinimizrImpl.java Java implementation for the interfaces:
package com.minimizr.domain.logic ; import java.util.List ; import javax.jws.WebService ; import com.minimizr.service.ExampleObject ; import com.minimizr.service.MinimizrService ; @WebService( serviceName = "MinimizrService" , endpointInterface = "com.minimizr.service.MinimizrService" ) 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-encoding but you can use XFire web services with PHP with document/literal style of SOAP.
Here is a really simple example to use all the exposed java web services in this example with NuSOAP PHP SOAP library. There are no checks for errors in the code:
<?php require ( "../lib/nusoap.php" ) ; $soapClient = new soapclient( "http://www.minimizr.com/ws/services/v1/MinimizrService?wsdl" , "wsdl" ) ; $proxyClass = $soapClient -> getProxy ( ) ; // getString $string = $proxyClass -> getString ( ) ; print ( "<b>String:</b> " . $string [ "out" ] . "<hr/>" ) ; // echoString $string = $proxyClass -> echoString ( array ( "in0" => "ABC" ) ) ; print ( "<b>String:</b> " . $string [ "out" ] . "<hr/>" ) ; // echoObject $requestObject = array ( "name" => "John" , "age" => 50 ) ; $result = $proxyClass -> echoObject ( array ( "in0" => $requestObject) ) ; $resultObject = $result [ "out" ] ; print ( "<b>Object:</b> name: " . $resultObject [ "name" ] ) ; print ( ", age: " . $resultObject [ "age" ] . "<hr/>" ) ; // loadExampleObjectList $exampleObjectList = $proxyClass -> loadExampleObjectList ( ) ; foreach ( $exampleObjectList [ "out" ] [ "ExampleObject" ] as $key => $value ) { print ( $value [ "name" . " " . $value [ "age" ] . "<br/>" ) ; } ?>
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:
<?php require ( "../lib/nusoap.php" ) ; $username = "username" ; $password = "password" ; $method = "basic" ; $soapClient = new soapclient( "https://$username:$password@www.minimizr.com/ws/services/v1/MinimizrService?wsdl" , "wsdl" ) ; $proxyClass = $soapClient -> getProxy ( ) ; $proxyClass -> setCredentials ( $username , $password , $method ) ; $proxyClass -> useHTTPPersistentConnection ( ) ; ...
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 Security with XFire web services needs a little bit more work. Any suggestions for the easiest way to implement it?
相关推荐
Quickly and productively develop complex Spring applications and microservices out of the box, with minimal concern over things like configurations. This revised book will show you how to fully ...
With the arrival of Spring Boot, developers can really focus on the code and deliver great value, with minimal contour. This book will show you how to build various projects in Spring 5.0, using its...
CHM file Publisher : Addison Wesley ... Written by programmers for programmers, the book will help you successfully utilize these exciting technologies with minimal hassle and maximum speed.
After you read and use this book, you’ll be able to more quickly and most productively develop complex Spring applications and even microservices – out of the box – with minimal fuss on things like...
**adb (Android Debug Bridge) 和 Fastboot 是 Android 开发者常用的两个工具,它们在系统调试、设备管理以及刷机过程中发挥着至关重要的作用。** **ADB(Android Debug Bridge)** 是一个命令行实用程序,它允许...
This document species an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the \Internet ...
Data Science and Machine ... Learn how you can quickly build and deploy sophisticated predictive models as machine learning web services with the new Azure Machine Learning service from Microsoft.
"Minimal ADB and Fastboot 1.4.3 Portable" 是一个轻量级的Android开发者工具包,专为简化Android设备的ADB (Android Debug Bridge) 和Fastboot模式操作而设计。这个工具的主要目的是帮助开发者、爱好者或者普通...
Spring expert Craig Walls uses interesting and practical examples to teach you both how to use the default settings effectively and how to override and customize Spring Boot for your unique ...
《全面解析minimal_adb_fastboot.zip:安卓设备管理与刷机必备工具》 在安卓世界里,ADB(Android Debug Bridge)和fastboot是开发者和高级用户不可或缺的工具,它们为设备管理和系统刷机提供了强大的支持。...
By using concrete examples, minimal theory, and two production-ready Python frameworks—scikit-learn and TensorFlow—author Aurélien Géron helps you gain an intuitive understanding of the concepts ...
In this preface, I’ll tell you about the history of Minimal Perl and the origins of this book. THE HISTORY OF MINIMAL PERL The seeds of this book were sown many years ago, when I was building up my ...
标题 "2.minimal_adb_fastboot_v1.4.3(内含adb命令和卸载列表)" 提供的信息表明,这是一个包含ADB (Android Debug Bridge) 和 Fastboot 工具的压缩包,版本为v1.4.3。这两个工具在Android系统开发、调试和维护中...