java webservice传递对象,如何将java对象作为参数传递给restful webservice

Registration_BE contains many variable like myvariable. I want to get reg_be all variable in here. Like this I have to pass my object.

servlet:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be="+reg_be

webservice:

public String getText(@PathParam("reg_be") Registration_BE reg_be ) {

System.out.println("websevice:" +reg_be.myvariable);

return reg_be.myvariable;

}

The above code throws this Exception:

com.sun.jersey.spi.inject.Errors$ErrorMessagesException.....

How can I solve this problem?

解决方案

There are three typical options available to you.

Pass object variables into request

This is useful if you do not have a large number of variables, or need the ability to populate only a subset of the fields in Registration_BE.

If you want to pass the variables into the request as a typical POST, you will need to do some processing to construct the complex Registration_BE object in the first place:

public String getText(@RequestParam("reg_be.myvariable") String myvariable) {

Registration_BE reg_be = new Registration_BE(myvariable);

System.out.println("websevice:" +reg_be.myvariable);

return reg_be.myvariable;

}

And you can call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be.myvariable=myvalue

Or alternatively by passing in an array of variables:

public String getText(@RequestParam("reg_be.myvariable") String[] myvariables) {

Registration_BE reg_be = new Registration_BE(myvariables);

System.out.println("websevice:" +reg_be.myvariable);

return reg_be.myvariable;

}

And you can call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be.myvariable=myvalue1&reg_be.myvariable=myvalue2

Using a common data interchange format

The second option would be to pass your registration object as JSON (or XML). for this, you will need to enable the Jackson message convertor and make sure the Jackson library is in your classpath:

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

Your method would not change:

public String getText(@RequestParam("reg_be") Registration_BE reg_be ) {

System.out.println("websevice:" +reg_be.myvariable);

return reg_be.myvariable;

}

And you can now call it with:

http://192.168.1.1:8084/UnionClubWS/webresources/customerregistration/?reg_be={"myvariable":"myvalue"}

Custom message convertor

Your third, and most complex, option would be to create your own message convertor. This would give you the most flexibility (your request could take any form you like), but would involve a lot more boilerplate overhead to make it work.

Unless you have a very specific requirement on how the request packet should be constructed, I recommend you opt for one of the above options.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值