使用 Jersey 2 发送多部分 json 字符串

Sending Multipart json String with Jersey 2

我想编写一个 Java REST 客户端来上传 json 字符串。我有关于网络服务和 C++ 代码的描述,但这对我没有帮助。不幸的是,我在网上找到的解决方案对我不起作用。 必须使用 MultiPart 的 Web 服务说明。

这个描述对我帮助不大。我正在努力创建 MultiPart,在尝试 post 数据时,我得到了一个很长的异常堆栈。代码片段如下:

Client client = ClientBuilder.newBuilder()
            .register(MultiPartFeature.class)
            .build();
WebTarget target = client.target(baseUri + "/FileHandler?projectRecId="+projectRecId+"&versionRecId="+versionRecId+"&updateSchedules=true");

MultivaluedMap<String, String> info = new MultivaluedHashMap<String, String>();
info.add("FileTypeRecId", scheduleGuid);
info.add("CreatedUpdatedBy", user);
info.add("actualFileName", scheduleFileName);
info.add("MetaData","PROJECTID:" + projektIdFromJson + ";EINBAUDATUM:" + einbaudatumFromJson);

MultiPart infoPart = new MultiPart();
infoPart.contentDisposition(FormDataContentDisposition.name("ObjectInfo").build());
infoPart.bodyPart(new BodyPart().entity(info));

MultiPart filePart = new MultiPart();
filePart.contentDisposition(FormDataContentDisposition.name(scheduleFileName).build());
filePart.bodyPart(scheduleJson, MediaType.APPLICATION_JSON_TYPE);

MultiPart multiPartEntity = new MultiPart()
    .bodyPart(infoPart)
    .bodyPart(filePart);
Response response = target.request(MediaType.MULTIPART_FORM_DATA)
    .header("Authorization", "xxx")
    .post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));

例外星:

Aug 19, 2015 10:37:14 AM org.glassfish.jersey.internal.Errors logErrors
WARNUNG: The following warnings have been detected: WARNING: HK2 service reification failed for     [org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide]     with an exception:
MultiException stack 1 of 2
java.lang.NoClassDefFoundError: org/jvnet/mimepull/MIMEParsingException
...
Caused by: java.lang.ClassNotFoundException: org.jvnet.mimepull.MIMEParsingException
...

MultiException stack 2 of 2
java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(    implementation=org.glassfish.jersey.media.multipart.internal.MultiPartReaderClientSide
contracts={javax.ws.rs.ext.MessageBodyReader}
scope=javax.inject.Singleton
qualifiers={org.glassfish.jersey.internal.inject.Custom}
descriptorType=CLASS
descriptorVisibility=NORMAL
metadata=
rank=0
loader=null
proxiable=null
proxyForSameScope=null
analysisName=null
id=70
locatorId=1
identityHashCode=1763344271
reified=false)
...

这些HK2错误比较多,我可以提供需要的全部信息。

我现在知道我做错了什么了,你们有谁知道吗?

我认为您的项目中缺少以下 jar。

1. mimepull.jar
2. jersey-multipart.jar