设为首页 收藏本站
查看: 479|回复: 0

[经验分享] Minimal Web Services with XFire, Spring and PHP

[复制链接]

尚未签到

发表于 2017-4-9 08:39:45 | 显示全部楼层 |阅读模式
  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>


<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.xmlfile:

<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.javaJava 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.javaJava 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.javaJava 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.javaJava 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-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:

<?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 Securitywith XFire web services needs a little bit more work. Any suggestions for the easiest way to implement it?

Additional recourses


  • Webservices with Spring, XFire and jsr181
  • Basic Spring Web services with XFire and JSR 181
  • Spring, Hibernate, and XFire

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362227-1-1.html 上篇帖子: PHP中对缓冲区的控制实现代码 下篇帖子: php在win下Socket方式发邮件类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表