Spring Rest模板中未知的MIME类型?

时间:2019-08-01 13:26:43

标签: java spring resttemplate

我只是想发出一个简单的REST请求,如下所示

    @Test
    public void retcall() {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));
        headers.set(HttpHeaders.ACCEPT, "*/*");
        headers.set("X-IBM-Client-id", CLIENT_ID);
        headers.set(HEADER_AUTHORIZATION_KEY, "Bearer " + tokenInfo);
        HttpEntity<String> entity = new HttpEntity<>(headers);
        String url = VALIDATION_HTTP_PREFIX + SCF_SB + VALIDATION_HTTP_SUFFIX;
        RestTemplate restTemplate = new RestTemplate();
        List<? extends HttpMessageConverter<?>> httpMessageConverters = Collections.singletonList(new MappingJackson2HttpMessageConverter());
        restTemplate.setMessageConverters((List<HttpMessageConverter<?>>) httpMessageConverters);
        log.info("checking authentication {}", entity);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        System.out.println(exchange);

    }

但是我得到了

org.springframework.http.InvalidMediaTypeException: Invalid mime type "unknown": does not contain '/'

    at org.springframework.http.MediaType.parseMediaType(MediaType.java:462)
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:759)
    at org.springframework.web.client.DefaultResponseErrorHandler.getCharset(DefaultResponseErrorHandler.java:163)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:108)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:539)
    at com.inditex.otec.wscnfsvp.test.ChinoTest.retcall(ChinoTest.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "unknown": does not contain '/'
    at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:257)
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:459)
    ... 30 more

我已经尝试了所有方法,例如Invalid mime type in spring rest template?中所述,但仍然无法正常工作...

尽管此“未知”内容类型来自响应;有了邮递员,我就能得到身体的反应...

您有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试更换

headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE));

使用

headers.setContentType(MediaType.APPLICATION_JSON);