json JIRA REST API -客户端响应中出现415不支持的媒体类型错误

nvbavucw  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(95)

我正在尝试使用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_TYPE以及MediaType.APPLICATION_JSON,但它不会改变响应。我在Linux环境下运行这段代码作为一个独立的可执行文件。任何帮助都是感激不尽的。

4ngedf3f

4ngedf3f1#

根据Vijay-Bhushan在this SO article中的示例,尝试以下操作。
注意,他提到不需要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文档中有一些很好的例子。

相关问题