Facing Exception of MessageBodyWriter while sending JSONObject to Rest web service

88 views Asked by At

I am newbie to web service. Due to requirement I have to send a file[most probably in txt format] to server through REST web service.

I am getting the exception like below.

MessageBodyWriter not found for media type=application/json, type=class gvjava.org.json.JSONObject, genericType=class gvjava.org.json.JSONObject.

Here is my web service method.

@Path("{c}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String convert(@PathParam("c") JSONObject object) throws JSONException {

    String result = "";     
    return "<ctofservice>" +  "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>";
}

Now client code is like below

JSONObject data_file = new JSONObject();
        data_file.put("file_name", uploadFile.getName());
        data_file.put("description", "Something about my file....");
        data_file.put("file", uploadFile);       

        Client client = ClientBuilder.newClient();

        webTarget = client.target(uploadURL).path("ctofservice").path("convert");

          Response value = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(data_file,MediaType.APPLICATION_JSON_TYPE),
                        Response.class);

Please help me with this. Thanks in advance.

------------------------------------------------------------------------

As suggested by peeskillet in the answer below, I tried to send file through multipart. Still I am facing exception of no octet stream found.

Below is my rest api

@Path("{c}")
@POST   
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response convert(@FormDataParam("file") FormDataContentDisposition file) {

    String result = ""; 
Some operation with attached parameter ...
    return Response.status(200).entity(result).build();
}

Here is my test client

FormDataMultiPart  multiPart = new FormDataMultiPart();
        multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

        FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
            uploadFile,MediaType.APPLICATION_OCTET_STREAM_TYPE);
        multiPart.bodyPart(fileDataBodyPart);

        Client client = Client.create();

        WebResource webResource = client
           .resource(uploadURL).path("ctofservice");

        ClientResponse response = webResource.accept("application/json")
                   .post(ClientResponse.class,multiPart);

        if (response.getStatus() != 200) {
           throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatus());
        }

And I am getting the exception below

enter image description here I am not able to understand why I need to send data as MediaType.APPLICATION_OCTET_STREAM_TYPE ? As I have used multipart as media type before ...

I appreciate your help..

1

There are 1 answers

5
Paul Samsotha On

Without needing to configuring anything else, the easiest way to get around this is to just use a String instead of the actual JSONObject (i.e. just passing toString())

.post(Entity.json(data_file.toString()))

The problem with using JSONObject is that there is no provider that knows how to handle the conversion. You will have the same problem on the server side, where there is no provider to handle the conversion to JSONObject. So you will need to just do

@POST
public Response post(String json) {
    JSONObject jsonObject = new JSONObject(json);
}

If you really want to be able to just use JSONObject without needing to use a String, then you should check out this post.

As an aside, this is not valid JSON (it's XML)

"<ctofservice>" +  "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>"

but you are saying that the endpoint returns JSON