jersey 2 multipart pojo is always null

﹥>﹥吖頭↗ 提交于 2019-12-06 15:11:09
Paul Samsotha

See related problem here. The problem is that the Content-Type is not set for the doc part. In that post (answer) I didn't know how to set it in Postman, and I still haven't found a solution.

If you use a tool like cURL (which I'll just say is the best tool ever for REST development :-), you can make set the Content-Type of each part. If you don't know already cURL is a command like tool that you can use to make HTTP (and other protocol) requests. For example, you can do something like

curl -v -X POST http://localhost:8080/api/file \
        -F 'doc={"hello":"world"};type=application/json'

This makes a POST request as multipart and sets the doc part to be of type application/json.

You will also find some useful examples of setting here


UPDATE

Another options, if you simply can't set the individual parts' Content-Type, is to set the type programmatically before deserialing. For example

 @POST
 @Path("/upload1")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response createFile1(@FormDataParam("doc") FormDataBodyPart part) {
    part.setMediaType(MediaType.APPLICATION_JSON_TYPE);
    Test doc = part.getValueAs(Test.class);
    return Response.ok(doc.getName()).build();
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!