json应该用java来写_“实体只允许用JSON内容类型头写”JavaOnDeRiver JeSee

{

"error": {

"code": "BadRequest",

"message": "Entity only allows writes with a JSON Content-Type header.",

"innerError": {

"request-id": "3c0ea4e0-cbcc-4f11-9ca3-fc3fbfdd50bc",

"date": "2019-02-14T21:11:41"

}

}

}

让我分享我的代码:

String metadataJson

= "{"

+ "\"name\":\""+name+"\","

+ "\"file\":{},"

+ "\"@microsoft.graph.sourceUrl\":\"cid:content\","

+ "\"@microsoft.graph.conflictBehavior\":\"rename\""

+ "}";

FormDataContentDisposition fdcdMetadata = FormDataContentDisposition.name("metadata").build();

FormDataBodyPart metadataPart = new FormDataBodyPart(fdcdMetadata, metadataJson, buildChildMultipart("application", "json", "metadata"));

File tempFile = new File(this.temporaryFolderUri, tempFileName);

FormDataContentDisposition fdcdContent = FormDataContentDisposition.name("content").build();

FormDataBodyPart contentPart = new FormDataBodyPart(fdcdContent, tempFile, buildChildMultipart("plain", "text", "content"));

MultiPart nestedMultipart = new MultiPart(buildParentMultipart())

.bodyPart(metadataPart)

.bodyPart(contentPart);

return this.service.post(target, Entity.entity(nestedMultipart, nestedMultipart.getMediaType()));

.

.

.

public MediaType buildParentMultipart() {

final Map parameters = new HashMap();

parameters.put("boundary", "A100x");

MediaType mediaType = new MediaType("multipart", "form-data", parameters);

mediaType.withCharset("UTF-8");

return mediaType;

}

public MediaType buildChildMultipart(String type, String subType, String name) {

final Map parameters = new HashMap();

parameters.put("Content-ID", name);

MediaType mediaType = new MediaType(type, subType, parameters);

mediaType.withCharset("UTF-8");

return mediaType;

}

现在发送post请求的代码:

public JsonObject post(WebTarget target, Entity entity) throws InterruptedException, ExecutionException {

return target.request(MediaType.APPLICATION_JSON).header(AUTHORIZATION_HEADER, getAccessToken())

.header("Content-type", MediaType.APPLICATION_JSON)

.header("accept", MediaType.APPLICATION_JSON)

.buildPost(entity)

.submit(JsonObject.class).get();

}

我在很多论坛上都找过解决方案,但都没有运气。我在Node.js、C和JavaScript中发现了同样的问题,但在Java项目中却没有(Java中没有OnDeRevSDK)。我已经检查了标题、内容类型和边界,但它们似乎没有问题。这个问题似乎与元数据部分有关,但我正在设置ms所要求的内容类型。你们能帮我吗?提前谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值