users@jersey.java.net

[Jersey] Try to receive multipart which contains json

From: Rindt, Daniel <drindt_at_viselabs.com>
Date: Sun, 1 Mar 2015 16:00:56 +0100

Dear readers,

i try to submit a simple list of json serialized pojo's. The server
throws all the time a 415 unsupported media. Here's my code:
=== 8< ===
@Path("/inventory")
public class Inventory {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response saveItems(FormDataMultiPart items) {
        // TODO implement persistence
        return Response.ok(new ItemReceiveStatus(-1)).build();
    }
}

Test:
        Collection<InventoryItem> items = new ArrayList<>();
        for (int i = 0; i < CONTRACT_MAX_ITEMS; i++) {
            items.add(getDummyItem());
        }
        final MultiPart multiPartEntity = new MultiPart()
                .bodyPart(new BodyPart(items, MediaType.APPLICATION_JSON_TYPE));
        multiPartEntity.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);

        ItemReceiveStatus response = target("inventory")
                .request()
                .post(Entity.entity(multiPartEntity,
multiPartEntity.getMediaType()),
                        ItemReceiveStatus.class); // 415

        assertEquals(CONTRACT_MAX_ITEMS, response.getReceivedItems());

=== 8< ===

Thank you for reading and all hints to solve this!
Daniel