【问题标题】:Rest Template exchange fails due to response content type being invalid由于响应内容类型无效,Rest Template 交换失败
【发布时间】:2020-01-09 13:35:35
【问题描述】:

我在使用 springs restTemplate 从 AWS 托管 API 检索数据时遇到问题。

以下是我的代码

    public List<AccountLocation> getCircuitViewRooms(String clientApiKey) {
        String uri = circuitApiUrl + "school/rooms?api_key=" + clientApiKey;
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().clear();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        List<AccountLocation> locations = new ArrayList<>();
        HttpEntity entity = getHeaders();
        ResponseEntity<?> resp = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
        /*if(resp.getStatusCode() == HttpStatus.OK) {
            CircuitViewRooms[] rooms = resp.getBody();
             for(CircuitViewRooms room : rooms) {
                AccountLocation loc = new AccountLocation();
                loc.setLocationName(room.getLaundryRooms().getLaundry_room_name());
                loc.setLocationId(room.getLocation());
                locations.add(loc);
            }
        }*/
        return locations;
    } 

    private HttpEntity<?> getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
        headers.add("x-api-key", xApiKey);
        return new HttpEntity<>(null, headers);
    }

我收到 200 OK 响应,但是当它尝试使用 httpMessageConverterExtractor 时会抛出 org.springframework.http.InvalidMediaTypeException: Invalid mime type "method.request.header.Content-Type": does not contain '/'

堆栈跟踪

    at org.springframework.http.MediaType.parseMediaType(MediaType.java:534) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:924) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.getContentType(HttpMessageConverterExtractor.java:133) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:90) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:994) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:977) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:736) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
    at com.upp.uppstudentapp.services.laundry.account_services.CircuitViewService.getCircuitViewRooms(CircuitViewService.java:38) ~[main/:na]

邮递员返回,但我也看到响应标头中的内容类型是“method.request.header.Content-Type”

我也尝试过使用 object.class

有没有人知道解决这个问题的方法或解决这个问题的方法?

这是 api 的文档 sn-p

一个显着的区别是此实现能够以 XML 或 JSON 格式生成输出 而之前的 API 只生成 XML。此外,在此不使用方法名称参数 实现 - 完整的 API 路径决定了适用的方法。

编辑 1:添加内容类型尝试相同的异常

    private HttpEntity<?> getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
        headers.add("Content-Type", "application/json");
        //headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("x-api-key", xApiKey);
        return new HttpEntity<>(null, headers);
    }

也尝试使用 setContent。

【问题讨论】:

  • 把右边Content-Type.
  • 你没有定义内容类型
  • 厌倦了同样的反应
  • 什么是正确的内容类型并显示/编辑您尝试过的内容
  • 嗨@zafirov。我确实做到了,但我很幸运地让我们合作的公司更改了内容类型。

标签: java spring amazon-web-services rest spring-boot


【解决方案1】:

setContentType 期望 MediaType,对于 JSON 添加:

headers.setContentType(MediaType.APPLICATION_JSON);

【讨论】:

  • 返回相同的异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-23
  • 2017-06-08
相关资源
最近更新 更多