【问题标题】:JIRA REST API - 415 Unsupported Media Type error in ClientResponseJIRA REST API - ClientResponse 中出现 415 不支持的媒体类型错误
【发布时间】:2018-01-18 18:21:33
【问题描述】:

我正在尝试使用 Jersey 客户端通过 WebResource 实例发送 POST 请求。但无论我如何指定请求标头或类型,它都会给我一个415 Unsupported Media Type 错误。下面是我的代码

String SERVICE_URL = "http://hostIpAddress:8080/JiraUpdate/rest/createin/jira/createticket?"; // hostIpaddress is our server ip
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
System.out.println("SERVICE_URL=" + SERVICE_URL);
WebResource webResource = client.resource(UriBuilder.fromUri(SERVICE_URL).build());
String input = "{\"fields\":{\"project\":{\"key\":\"Invoicing\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";
ClientResponse response = webResource.header("Content-Type", "application/json;charset=UTF-8")
      .type(MediaType.TEXT_PLAIN_TYPE).post(ClientResponse.class, input);

我尝试将类型设置为MediaType.APPLICATION_FORM_URLENCODED_TYPEMediaType.APPLICATION_JSON,但它不会改变响应。我在 linux 环境中将此代码作为独立的可执行 JAR 运行。任何帮助表示赞赏。

【问题讨论】:

  • 看看这个来自 Oracle 的格式错误的 example - 我很确定这个 .type(MediaType.TEXT_PLAIN_TYPE) 正在覆盖/撤消这个 .header("Content-Type", "application/json;charset=UTF-8") 我是否建议尝试其中一个,
  • 另外,您的问题看起来像是同一SO question的副本
  • 尝试通过网络调试器(例如telerik.com/fiddler)运行您的请求,看看服务是否正确回复。
  • @JGlass 我已尝试按照您的建议和链接帖子使用,但我仍然收到错误消息。还尝试删除 UTF-8 但没有成功...
  • 1.您是否尝试将其更改为“PUT”而不是 POST? 2. 你确定你的网址是正确的吗?我看到的所有示例都类似于“example.com/rest/api/2”或者 JiraUpdate 是您的应用程序的名称,例如Web 应用程序的上下文?正如@Suenda 所提到的,如果您使用 fiddler 或开发人员工具在浏览器中尝试它,您是否可以让它在那里工作,然后我们可以回溯到 java 代码?另外,很多例子都使用 CURL,你有没有让它在 CURL 中工作?

标签: java json post jersey jira-rest-api


【解决方案1】:

基于我在第二条评论中提到的链接 SO 文章中的 Vijay-Bhushan 示例,您的代码应该是(并且他指出不需要 UTF-8):

String SERVICE_URL = "http://hostIpAddress:8080/JiraUpdate/rest/createin/jira/createticket?"; // hostIpaddress is our server ip
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
System.out.println("SERVICE_URL=" + SERVICE_URL);
WebResource webResource = client.resource(UriBuilder.fromUri(SERVICE_URL).build());
String input = "{\"fields\":{\"project\":{\"key\":\"Invoicing\"},\"summary\":\"REST Test\",\"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\"issuetype\":{\"name\":\"Bug\"}}}";

ClientResponse response = webResource.header("Content-Type","application/json;charset=UTF-8").post(ClientResponse.class,input);

Jersey Client API 文档中有一些很好的示例。

【讨论】:

  • 似乎链接给出了一个找不到的页面。只有添加了 json 数据的 MultivaluedMap 对象才允许我使用 POST 请求成功发送
  • 已更正链接,谢谢!听起来你成功了,干得好!
猜你喜欢
  • 1970-01-01
  • 2018-10-13
  • 2018-07-25
  • 2016-09-09
  • 2015-06-02
  • 2015-10-25
  • 2023-03-13
  • 2020-10-06
  • 1970-01-01
相关资源
最近更新 更多