3

I am using CXF with JacksonJsonProvider for my REST Services. I have a test method as follows

@POST
@Path("/book/{id}")
@Consumes({"application/json, multipart/form-data, application/x-www-urlencoded"})
@Produces({"application/json"})
public boolean setOwner(Book book) {
    System.out.println(book.getName());
    return true;
}

Now if I make a POST request with a raw JSON string as follows

    {"Book":{"name":"Book name","publisher":"Book publisher"}}

The request is processed correctly as I use Content-Type as 'application/json' while making the request.

But since I am integrating with an external service, I recieve either multipart/form-data OR application/x-www-urlencoded for which there is nothing afaik in Jackson that can handle it. If someone can point me to the right direction that would be great.

I can manage multipart/form-data with Jettison (part of CXF) but I would like to use Jackson.

fge
  • 119,121
  • 33
  • 254
  • 329
andthereitgoes
  • 263
  • 1
  • 6
  • 15

1 Answers1

0

I was looking to do exactly the same thing, almost to years later! I didn't have much luck using one method to handle multiple mime times, but I did get it to work using two methods, for example:

@POST
@Path("/book/{id}")
@Consumes({"multipart/form-data"})
@Produces({"application/json"})
public boolean setOwnerFromUpload(@FormDataParam("file") InputStream inputStream) {
    // decode
    final ObjectMapper mapper = new ObjectMapper();
    final Book book = (Book) mapper.readValue(inputStream, Book.class);
    System.out.println(book.getName());
    return true;
}

With two methods, jackson can now handle the the two different mime types.

Blazes
  • 4,721
  • 2
  • 22
  • 29