user2324623
user2324623

Reputation: 1

Send JSON data to Jersey Rest Webservice using Worklight Adapter

I tried to call rest service using http adapter, but got unsupported media type exception.

My adapter and server side code is as follows

var invocationData = { adapter : 'MyHttpAdapter', procedure : 'myAdapterProcedure', parameters : [myJSONObject] };

WL.Client.invokeProcedure(invocationData, {
    onSuccess : success,
    onFailure : failure
});

function myAdapterProcedure(prarams) {

var input = {
    method : 'put',
    returnedContentType : 'json',
    path : 'mobile/rest/notes/getMyWebData',
    parameters : prarams

};
return WL.Server.invokeHttp(input);

}

server side: @PUT @Path("addNotes")
@Consumes("application/json") @Produces("application/json") public String addNotes(MyNotes pVo) throws Exception { System.out.println("1231231" + pVo); return pVo; }

Detailed exception: Failed to parse JSON string Apache Tomcat/6.0.35 - Error report

HTTP Status 415 - Unsupported Media Type

type Status report

message Unsupported Media Type

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).

Apache Tomcat/6.0.35

FWLSE0101E: Caused by: [project GMobile]java.io.IOException: Unexpected character '<' on line 1, column 1 com.worklight.common.log.filters.ErrorFilter

Tried with Put, Post method invocations and got the same exception. tried with '@Consumes({ MediaType.APPLICATION_JSON }) on server method still get the same error. Can anyone let me know what am I missing here...

Found the same issue here, without the solution. ref: How to send the JSON data in rest web services?

Upvotes: 0

Views: 969

Answers (1)

ptitjuju69
ptitjuju69

Reputation: 143

Have you tried to check your REST service with a REST client (i'm using firefox add-on "RESTClient" to test my REST services outside of worklight) ?

If it's OK on the REST service side, then you need to check the headers in your http adapter "input" object.

you have to set the content-type params to "application/json" in the http request. To do so, add the "headers" attribute as below:

var input = {
   method : 'put',
   returnedContentType : 'json',
   path : 'mobile/rest/notes/getMyWebData',
   parameters : params,
   headers : 'Content-Type: application/json'

};

Regards.

Upvotes: 0

Related Questions