HessianPHP使用注意事项
来源:互联网 发布:aria2 百度网盘windows 编辑:程序博客网 时间:2024/06/01 09:21
HessianPHP_v2.0.3.zip, 在php作客户端,java作服务器端时,使用注意事项:
1.需要在php.ini中配置CURL模块:
extension=php_curl.dll
2.php传中文给java, 和java返回中文给php,都有中文乱码问题:
1)php和java文件本身都采用UTF-8编码
2)在php.ini中开启mbstring模块:
extension=php_mbstring.dll
同时配置:
mbstring.internal_encoding = UTF-8
因为:
Hessian2Writer.php文件:
function writeStringData($string){
returnHessianUtils::writeUTF8($string);
}
HessianUtils.php文件:
publicstatic function isInternalUTF8(){
$encoding= ini_get(' mbstring.internal_encoding');
if(!$encoding)
returnfalse;
return$encoding == 'UTF-8';
}
3).response返回设置为UTF-8:
header("Content-Type: text/html; charset=UTF-8");
或者,统一改php.ini文件:
mbstring.http_output = UTF-8
;启用字符转换功能
output_handler= mb_output_handler
4)默认配置:
HessianFactory.php
define('HESSIAN_PHP_VERSION','2.0'); --------->hessian2.0协议
$this->transports= array(
'CURL'=> 'HessianCURLTransport', ----------->传输协议
'curl'=> 'HessianCURLTransport',
'http'=> 'HessianHttpStreamTranspo rt'
);
3.java返回值问题:
1).java端返回(自定义的)对象,在php端为stdClassObject,它具有一个属性 [__type] => org.model.User
2).而对于普通对象,例如java的Date对象,在php就是DateTime对象:
[birthday] => DateTimeObject ([date] => 2012-07-27 15:27:31 [timezone_type] => 1 [timezone]=> +00:00 )
3).java的List, Set, Map,在php就是Array.
List为php的序号数组:
[productList1] => Array (
[0]=> stdClass Object ( [__type] => org.model.Product [name]=> 布匹 [price] => 300 )
)
Map为php的名称数组:
[productMap] => Array (
[布匹]=> stdClass Object ( [__type] => org.model.Product [name]=> 布匹 [price] => 300 )
)
java的返回值,php端不需要做任何特殊映射,可以直接采用->x->y的方式去取值显示.
4).同一个对象,即放在List中,又放在Map中,返回值中为什么只显示一处有值?
这其实是HessianPHP_v2.0.3.zip的一个bug.
hessian协议中为了减少重复对象的序列化,采用了引用的方式,参见:
Hessian 2.0 序列化协议规范
http://wenku.baidu.com/view/3832199951e79b8968022641.html
经过debug调试,发现:
需要修改Hessian2Parser.php中
function untypedMap($code, $num){
// if(HessianRef::isRef($key))$key =&$this->objectlist->reflist[$key->index];
// if(HessianRef::isRef($value))$value =&$this->objectlist->reflist[$value->index];
if(HessianRef::isRef($key))$key = $this->refmap->objectlist[$value->index];
if(HessianRef::isRef($value))$value = $this->refmap->objectlist[$value->index];
}
同样下面方法也需要修改:
function typedMap($code, $num)
{
...
}
测试结果:
objectstdClass Object ( [__type] => User [name] =>yangwenchao杨文超 [country] => CHINA [birthday] => DateTimeObject ( [date] => 2012-07-28 09:02:32 [timezone_type] => 1[timezone] => +00:00 ) [productList1] => Array ( [0]=> stdClassObject ( [__type] => Product [name] => 布匹 [price] => 300) )[productList2] => Array ( ) [productList3] => [productMap]=> Array ( [布匹一] => stdClassObject ( [__type] => Product [name] => 布匹 [price] => 300) [布匹二]=> stdClassObject ( [__type] => Product [name] => 布匹 [price] => 300) ))
4.php向java传参问题:
5.关于类型映射:
php是无类型的,默认情况下远程的Object都会变成php的stdClass这样的万能对象。持有这样的对象进行访问的时候,关于对象的属性name,price等信息需要尝试指定。(工作正常)
但是如果把远程Object映射成php本地的自定义的phpObject以后,就可以用一个对象的模子class来框住对象的实体形式,而且编辑器还可以支持语法提示功能。
采用这种方式,在解析返回值时,可以得到便利,在向Java传递对象作为参数时,也可以提供一些便利。
AdvancedExample Advancedtype mapping and filtering
http://code.google.com/p/hessianphp/wiki/AdvancedExample
如果不采用自动映射方式,php向java传参时,只能采用php原生的stdClass来传值:
$person= new stdClass();
$person->__type= 'org.model.Person';//参照java返回值的形式,添加上__type属性,其实无效.
$person->id= 1;
$person->firstName= '杨';
$person->lastName= '文超';
//$person->birthDate= '';
$user= $proxy->getUserABC($person);
在java端,总是抛出warning,显然,java端无视了参数$person->__type的意义:
java.lang.ClassNotFoundException: stdClass
2012-7-28 11:13:22 com.caucho.hessian.io.SerializerFactorygetDeserializer
警告: Hessian/Burlap:'stdClass' is an unknown class inWebappClassLoader
context:/springhessian
delegate:false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@1adc30
去掉warning的解决办法:
$options->typeMap['stdClass'] ='org.model.Person';
$options= new HessianOptions();
$options->typeMap['stdClass']='org.model.Person';
$url="http://localhost:8080/springhessian/remoting/hessianService";
$proxy= new HessianClient($url,$options);
echo"
1.需要在php.ini中配置CURL模块:
extension=php_curl.dll
2.php传中文给java, 和java返回中文给php,都有中文乱码问题:
1)php和java文件本身都采用UTF-8编码
2)在php.ini中开启mbstring模块:
extension=php_mbstring.dll
同时配置:
mbstring.internal_encoding = UTF-8
因为:
Hessian2Writer.php文件:
HessianUtils.php文件:
3).response返回设置为UTF-8:
header("Content-Type: text/html; charset=UTF-8");
或者,统一改php.ini文件:
mbstring.http_output = UTF-8
;启用字符转换功能
output_handler= mb_output_handler
4)默认配置:
HessianFactory.php
define('HESSIAN_PHP_VERSION','2.0');
3.java返回值问题:
1).java端返回(自定义的)对象,在php端为stdClassObject,它具有一个属性 [__type] => org.model.User
2).而对于普通对象,例如java的Date对象,在php就是DateTime对象:
[birthday] =>
3).java的List, Set, Map,在php就是Array.
List为php的序号数组:
[productList1] => Array (
)
Map为php的名称数组:
[productMap] => Array (
)
java的返回值,php端不需要做任何特殊映射,可以直接采用->x->y的方式去取值显示.
4).同一个对象,即放在List中,又放在Map中,返回值中为什么只显示一处有值?
这其实是HessianPHP_v2.0.3.zip的一个bug.
hessian协议中为了减少重复对象的序列化,采用了引用的方式,参见:
Hessian 2.0 序列化协议规范
http://wenku.baidu.com/view/3832199951e79b8968022641
经过debug调试,发现:
需要修改Hessian2Parser.php中
function untypedMap($code, $num){
//
//
}
同样下面方法也需要修改:
function typedMap($code, $num)
{
...
}
测试结果:
objectstdClass Object ( [__type] => User [name] =>yangwenchao杨文超 [country] => CHINA [birthday] => DateTimeObject ( [date] => 2012-07-28 09:02:32 [timezone_type] => 1[timezone] => +00:00 ) [productList1] => Array ( [0]=>
4.php向java传参问题:
5.关于类型映射:
php是无类型的,默认情况下远程的Object都会变成php的stdClass这样的万能对象。持有这样的对象进行访问的时候,关于对象的属性name,price等信息需要尝试指定。(工作正常)
但是如果把远程Object映射成php本地的自定义的phpObject以后,就可以用一个对象的模子class来框住对象的实体形式,而且编辑器还可以支持语法提示功能。
采用这种方式,在解析返回值时,可以得到便利,在向Java传递对象作为参数时,也可以提供一些便利。
$options参见例子:= new HessianOptions();$options->typeMap['Person'] = '*.Person';$options->typeMap['CalendarHandler'] = 'com.caucho.hessian.io.CalendarHandle';$options->parseFilters['@CalendarHandler'] = array ('CalendarHandler','calendarToDateTime');$options->writeFilters['@Person']= array ('CalendarHandler','writePerson');
AdvancedExample
http://code.google.com/p/hessianphp/wiki/AdvancedExample
如果不采用自动映射方式,php向java传参时,只能采用php原生的stdClass来传值:
在java端,总是抛出warning,显然,java端无视了参数$person->__type的意义:
java.lang.ClassNotFoundException: stdClass
2012-7-28 11:13:22 com.caucho.hessian.io.SerializerFactorygetDeserializer
警告:
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@1adc30
去掉warning的解决办法:
$options->typeMap['stdClass'] ='org.model.Person';