org.springframework.util.InvalidMimeTypeException:无效的mime类型“ application:json; charset = utf8”:不包含'/'

时间:2019-04-16 08:25:54

标签: java resttemplate

    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();

    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.APPLICATION_JSON);
    //mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
    mediaTypeList.add(MediaType.TEXT_PLAIN);
    mediaTypeList.add(MediaType.ALL);
    headers.setAccept(mediaTypeList);

    headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");
    headers.add(HttpHeaders.ACCEPT_LANGUAGE, "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7");
    headers.add(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
    headers.add(HttpHeaders.CONTENT_LENGTH,"178");
    headers.add(HttpHeaders.CONTENT_TYPE,"application/x-www-form-urlencoded");
    headers.add(HttpHeaders.REFERER,"http://www.7dingdong.com/");
    headers.add(HttpHeaders.ORIGIN,"http://www.7dingdong.com");
    headers.add(HttpHeaders.CONNECTION,"keep-alive");
    headers.add(HttpHeaders.HOST, "7ddapi.7dingdong.com");
    HttpEntity<String> httpEntity = new HttpEntity<String>("",headers);
    StringBuffer paramsURL = new StringBuffer(goLoginUrl);
    paramsURL.append("?token=" +  token)
            .append("&t="+ t)
            .append("&device="+ device)
            .append("&user_name="+ user_name)
            .append("&password="+ password)
            .append("&company="+ company)
            .append("&api="+ api);


    ResponseEntity<Object> response = restTemplate.exchange(
            paramsURL.toString(),
            HttpMethod.GET,
            httpEntity,
            Object.class);
 /*
    ResponseEntity<String> response = restTemplate.postForEntity(goLoginUrl,httpEntity,String.class);
 */

错误:

Exception in thread "main" org.springframework.http.InvalidMediaTypeException: Invalid mime type "application:json;charset=utf8": does not contain '/'
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:534)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:932)
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:133)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:995)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:978)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:737)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579)
    at Login.goLogin(Login.java:268)
    at Login.main(Login.java:293)
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "application:json;charset=utf8": does not contain '/'
    at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:197)
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:531)
    ... 10 more
Disconnected from the target VM, address: '127.0.0.1:54273', transport: 'socket'

我应该如何解决这个问题?谁可以帮助我?

2 个答案:

答案 0 :(得分:0)

从错误中您可以了解到哑剧类型是错误的application:json;charset=utf8

应为application/json;charset=utf8

您应该使用APPLICATION_JSON_UTF8_VALUE而不是MediaType.APPLICATION_JSON

mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8_VALUE);

答案 1 :(得分:0)

您必须使用 MediaType.APPLICATION_JSON_VALUE 作为应用程序 json application/json 的媒体类型

MediaType.APPLICATION_JSON_VALUE