Http 415 Unsupported Media type error with JSON

2020-01-23 02:55发布

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

Request content type is set to ("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.

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 :

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

15条回答
手持菜刀,她持情操
2楼-- · 2020-01-23 03:27

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 Object3 contains Object1 and Object2 and everything works fine.

查看更多
贼婆χ
3楼-- · 2020-01-23 03:28

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

查看更多
仙女界的扛把子
4楼-- · 2020-01-23 03:30

Add MappingJackson2HttpMessageConverter manually in configuration solved the problem for me :

@EnableWebMvc
@Configuration
@ComponentScan
public class RestConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
        messageConverters.add(new MappingJackson2HttpMessageConverter());
        super.configureMessageConverters(messageConverters);
    }
}
查看更多
迷人小祖宗
5楼-- · 2020-01-23 03:31

The 415 (Unsupported Media Type) status code indicates that the origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource. The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly. DOC

查看更多
男人必须洒脱
6楼-- · 2020-01-23 03:34

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

查看更多
ら.Afraid
7楼-- · 2020-01-23 03:34

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.

查看更多
登录 后发表回答