3

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?

6
  • Can you post more of the stacktrace pls? Commented Jun 11, 2013 at 6: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 Commented Jun 11, 2013 at 7:11
  • no tat is not a json string. tat is java object Commented Jun 11, 2013 at 7: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, ... Commented Jun 11, 2013 at 7: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 Commented Jun 11, 2013 at 7:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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;
OK, so my first suggestion is not suitable for what you are trying to do. What about my second suggestion using JSON?
I've updated my question to include how you can pass an array in your request.
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
You want to upload the file first, and then reference it. This tutorial should help you get started, after that you can add a URL to the uploaded image in your "getText" request.
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.

5 Comments

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
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.
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?
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 and this tutorial. I think it will work for you. You need to just separate your image from your object.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.