users@jersey.java.net

[Jersey] Re: How to give a json string to a webservice method

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Wed, 06 Apr 2011 09:50:42 +0200

Hello,

you don't want to send your data as part of the URI, what you are
looking for is how to set request entity.

See JSON related samples (you can use jersey-client to make requests -
it should be easier that using apache http client directly):

http://download.java.net/maven/2/com/sun/jersey/samples/jacksonjsonprovider/1.7-ea01/jacksonjsonprovider-1.7-ea01-project.zip
http://download.java.net/maven/2/com/sun/jersey/samples/json-from-jaxb/1.7-ea01/json-from-jaxb-1.7-ea01-project.zip

We have JSON related chapter in user guide too
(http://jersey.java.net/nonav/documentation/latest/json.html), you might
want to check it out.

Hope it helps,

Pavel

On 04/05/2011 07:48 PM, Christine Bauers wrote:
> Hi there,
>
> I use a jersey webservice and I want to give the webservice method a jsonstring.
>
> this is the method:
>
> @GET
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_JSON)
> @Path("/search")
> public String search(@QueryParam("token") String token,
> @QueryParam("searchString") String searchParams)
>
> so I define my searchString of the Type JSONObject in the client and call the webservice method:
>
> HttpGet requesttest = new HttpGet("http://test.de/myresource/search?token="+cursor.getString(0)+"&searchString="+searchString);
>
> but trying this throws the exception that there is a illegal character in the query. I´m sure it is because of :
>
> If I change to post it looks like this:
> HttpPost requestPost = new HttpGet("http:/test.de/myresource/search?");
> List<NameValuePair> params = new ArrayList<NameValuePair>();
> params.add(new BasicNameValuePair("token", cursor.getString(0)));
> params.add(newBasicNameValuePair("searchString",searchString.toString()));
>
> but I don´t know how to use the searchstring in the search method of the webservie now.
>
> does anybody know how to solve the problem?
>
> best wishes
>