2

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?

Hash
  • 4,517
  • 5
  • 19
  • 36
sabarirajan
  • 157
  • 2
  • 3
  • 13
  • Can you post more of the stacktrace pls? – Rajiv Jun 11 '13 at 06:49
  • is reg_be a json String? can you try to do the unmarshalling of the object in small test/main snipplet with the help of json parser?! Have a look at [this blog post](http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/) – kiwiwings Jun 11 '13 at 07:11
  • no tat is not a json string. tat is java object – sabarirajan Jun 11 '13 at 07:25
  • First, the parameter you are passing is a `@QueryParam("reg_be")`, not a `@PathParam`, and secondly I don't think that you can transmit an object this way, in the url, this is not the object that is concatenated but the result of it's `toString` method. Either you pass the object ID and retrieve it from your backend by id in your getText method, or you will have to transmit it as the body of a `POST` request encoded with Json, XML, ... – gma Jun 11 '13 at 07:32
  • Rajiv this is wat u asked? SEVERE: Servlet /UnionClubWS threw load() exception com.sun.jersey.spi.inject.Errors$ErrorMessagesException at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765) at – sabarirajan Jun 11 '13 at 07:48
  • thank u for ur reply gma.in queryparam also it not working. how can i pass object id and retrieve in gettext method? – sabarirajan Jun 11 '13 at 08:00

2 Answers2

4

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.

seanhodges
  • 16,593
  • 14
  • 67
  • 89
  • thank u seanhodges. but i having 60 variables then i hav to give 60 varibles in url. so only throw java object.i can get all my variable in webservice.and i tried in arraylist also tat also throwing error.u how to pass arraylist.like reg_be=reg_bearray; – sabarirajan Jun 11 '13 at 08:26
  • OK, so my first suggestion is not suitable for what you are trying to do. What about my second suggestion using JSON? – seanhodges Jun 11 '13 at 08:29
  • I've updated my question to include how you can pass an array in your request. – seanhodges Jun 11 '13 at 08:54
  • ok seanhodges i go with ur 1st or 2nd opt but i having inputstream for an image how can i send tat with ur method?.i dont know how to send inputstream to restful service. plzzz help – sabarirajan Jun 11 '13 at 09:35
  • You want to upload the file first, and then reference it. [This tutorial](http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/) should help you get started, after that you can add a URL to the uploaded image in your "getText" request. – seanhodges Jun 11 '13 at 11:51
2

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.

Community
  • 1
  • 1
Abhendra Singh
  • 1,880
  • 4
  • 22
  • 45
  • first thank for your answer.but in my form i having 60 fields.so i get all in my servlet by using getter and setter method.so atlast i having java object.then i have to pass to my webservice then i want to store in db.if i give 60 field in @queryparam it ll work i know. but i want use object. i hav to throw in servlet then i want to receive in my webservice for tat can u give any easy solution plzzzzzz – sabarirajan Jun 11 '13 at 07:47
  • You can not pass your Java Object to query param. You should convert your java object into JSON String and then pass it in query param. You need not to cast manually object into JSON String. There is already created `Jackson` Library that wil convert your Object to JSON String and vice versa. – Abhendra Singh Jun 11 '13 at 08:33
  • hmmm ok Abhendra Singh i having inputstream for an image.so i tried as u told before.i try to covert my obj to json string.by gson but i showing error bcoz of tat inputstream.so only i tried with java object.ok can you tell how to pass input stream in restful? – sabarirajan Jun 11 '13 at 09:40
  • Can you separate your image from your object? because we have a way to pass Input stream or upload file to rest webservice. Please check [this](http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/) and [this](http://stackoverflow.com/questions/3496209/input-and-output-binary-streams-using-jersey) tutorial. I think it will work for you. You need to just separate your image from your object. – Abhendra Singh Jun 11 '13 at 09:58
  • And one more thing you can do that is, convert your image into byte array. And pass this byte array as string in JSON. Like [this](http://stackoverflow.com/questions/11546917/sending-a-byte-array-in-json-using-jackson) – Abhendra Singh Jun 11 '13 at 10:01