添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

org.springframework.util.InvalidMimeTypeException: Invalid mime type "application:json;charset=utf8": does not contain '/'

Ask Question 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);

Error:

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'
                First of all, welcome to StackOverflow. Following the advices of the community, it is recommended to include context to your source code in order to receive the proper responses. Please check the documentation on how to write a good question. People will be very glad to help.
– Carlos Cavero
                Apr 16, 2019 at 8:34

From the error you can understand that mime type is wrong application:json;charset=utf8

It should be application/json;charset=utf8

You should use APPLICATION_JSON_UTF8_VALUE instead of MediaType.APPLICATION_JSON:

mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8_VALUE);
                yes,The remote interface returned "application:json;charset=utf8" instead of "application/json;charset=utf8". I don't know why,  and I ignored it with "ignoreContentType".  thank you very much!
– 刘教导员
                Apr 17, 2019 at 14:04

you have to use MediaType.APPLICATION_JSON_VALUE as the media type for application json application/json

MediaType.APPLICATION_JSON_VALUE
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.