简体   繁体   中英

how to pass java object as a parameter in 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:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <mvc:annotation-driven />

</beans>

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.

If you want to paas your object as path-param or query-param then you need to pass it as string. For this convert your object into JSON string and pass it as a query param. For this here is an better way to use JSON.

One more better option is that make your request POST . And submit your object to POST method. Please read this for @FormParam .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM