Java JSON 的 Http 415 Unsupported Media type 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22566433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:32:04  来源:igfitidea点击:

Http 415 Unsupported Media type error with JSON

javajsonhttp-status-code-415

提问by user3443794

I am calling REST service with JSON request and it gives Http 415 "Unsupported Media Type" error.

我正在使用 JSON 请求调用 REST 服务,它给出了 Http 415“不支持的媒体类型”错误。

Request content type is set to ("Content-Type", "application/json; charset=utf8").

请求内容类型设置为 ("Content-Type", "application/json; charset=utf8")。

It works fine if I don't include Json object in the request. I am using google-gson-2.2.4 library for json.

如果我不在请求中包含 Json 对象,它工作正常。我正在为 json 使用 google-gson-2.2.4 库。

I tried using a couple of different libraries but it made no difference.

我尝试使用几个不同的库,但没有任何区别。

Can anybody please help me to resolve this?

有人可以帮我解决这个问题吗?

Here is my code:

这是我的代码:

public static void main(String[] args) throws Exception
{

    JsonObject requestJson = new JsonObject();
    String url = "xxx";

    //method call for generating json

    requestJson = generateJSON();
    URL myurl = new URL(url);
    HttpURLConnection con = (HttpURLConnection)myurl.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);

    con.setRequestProperty("Content-Type", "application/json; charset=utf8");
    con.setRequestProperty("Accept", "application/json");
    con.setRequestProperty("Method", "POST");
    OutputStream os = con.getOutputStream();
    os.write(requestJson.toString().getBytes("UTF-8"));
    os.close();


    StringBuilder sb = new StringBuilder();  
    int HttpResult =con.getResponseCode();
    if(HttpResult ==HttpURLConnection.HTTP_OK){
    BufferedReader br = new BufferedReader(new   InputStreamReader(con.getInputStream(),"utf-8"));  

        String line = null;
        while ((line = br.readLine()) != null) {  
        sb.append(line + "\n");  
        }
         br.close(); 
         System.out.println(""+sb.toString());  

    }else{
        System.out.println(con.getResponseCode());
        System.out.println(con.getResponseMessage());  
    }  

}
public static JsonObject generateJSON () throws MalformedURLException

{
   String s = "http://www.abc.com";
        s.replaceAll("/", "\/");
    JsonObject reqparam=new JsonObject();
    reqparam.addProperty("type", "arl");
    reqparam.addProperty("action", "remove");
    reqparam.addProperty("domain", "staging");
    reqparam.addProperty("objects", s);
    return reqparam;

}
}

Value of requestJson.toString is :

requestJson.toString 的值是:

{"type":"arl","action":"remove","domain":"staging","objects":"http://www.abc.com"}

{"type":"arl","action":"remove","domain":"staging","objects":" http://www.abc.com"}

采纳答案by user3443794

Not sure about the reason but Removing lines charset=utf8from con.setRequestProperty("Content-Type", "application/json; charset=utf8")resolved the issue.

不知道的原因,但删除线charset=utf8,从 con.setRequestProperty("Content-Type", "application/json; charset=utf8")解决问题。

回答by Rahul Rastogi

I was sending "delete" rest request and it failed with 415. I saw what content-type my server uses to hit the api. In my case, It was "application/json" instead of "application/json; charset=utf8".

我正在发送“删除”休息请求,但失败了 415。我看到我的服务器使用什么内容类型来访问 api。就我而言,它是“application/json”而不是“application/json; charset=utf8”。

So ask from your api developer And in the meantime try sending request with content-type= "application/json" only.

因此,请询问您的 api 开发人员,同时尝试仅使用 content-type="application/json" 发送请求。

回答by Dhruv

This is because charset=utf8should be without a space after application/json. That will work fine. Use it like application/json;charset=utf-8

这是因为charset=utf8应该没有后一个空间application/json。那会工作得很好。使用它就像application/json;charset=utf-8

回答by karthik

If you are making jquery ajax request, dont forget to add

如果您正在制作 jquery ajax 请求,请不要忘记添加

contentType:'application/json'

回答by Parth Solanki

Add Content-Type: application/jsonand Accept: application/json

添加内容类型:application/json并接受:application/json

回答by spajdo

I had the same issue. My problem was complicated object for serialization. One attribute of my object was Map<Object1, List<Object2>>. I changed this attribute like List<Object3>where Object3contains Object1and Object2and everything works fine.

我遇到过同样的问题。我的问题是序列化的复杂对象。我的对象的一个​​属性是Map<Object1, List<Object2>>. 我改变了这样的属性List<Object3>,其中Object3包含Object1Object2一切工作正常。

回答by Murali Gundappan

Some times Charset Metada breaks the json while sending in the request. Better, not use charset=utf8 in the request type.

有时,Charset Metada 在发送请求时会破坏 json。最好不要在请求类型中使用 charset=utf8 。

回答by Jero Dungog

I know this is way too late to help the OP with his problem, but to all of us who is just encountering this problem, I had solved this issue by removing the constructor with parameters of my Class which was meant to hold the json data.

我知道这对于帮助 OP 解决他的问题为时已晚,但对于我们所有刚刚遇到此问题的人来说,我已经通过删除带有我的类的参数的构造函数来解决这个问题,该参数用于保存 json 数据。

回答by Arjun Duggal

Add the HTTP header manager and add in it your API's header names and values. e.g. Content-type, Accept, etc. That will resolve your issue.

添加 HTTP 标头管理器并将您的 API 的标头名称和值添加到其中。例如内容类型、接受等。这将解决您的问题。

回答by Dulith De Costa

If you are using AJAXjQueryRequest this is a must to apply. If not it will throw you 415Error.

如果您正在使用AJAXjQuery请求,这是必须申请的。如果不是,它会抛出你的415错误。

dataType: "json",
contentType:'application/json'