java - 由于响应内容类型无效,其余模板交换失败

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

我在使用 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 contains '/'

堆栈跟踪

    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]

Postman 返回,但我还看到响应 header 中的内容类型是“method.request.header.Content-Type”

我也尝试过使用object.class

有谁知道解决这个问题的方法或解决方案吗?

这是 api 的文档片段

One notable difference is this implementation has the ability to produce output in either XML or JSON formats whereas the previous API produced only XML. Additionally, method name parameters are not used in this implementation - the full API path determines the applicable method.

编辑 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。

最佳答案

setContentType 期望 MediaType,对于 JSON 添加:

headers.setContentType(MediaType.APPLICATION_JSON);

关于java - 由于响应内容类型无效,其余模板交换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59665172/

相关文章:

java - 如何将 SlidingDrawer 作为系统覆盖层?

spring - 有没有更好的方法来配置 Bean?

amazon-web-services - 使用 AWS Config 一致性包有什么优势?

java - 在同一端口使用两个本地 ip

java - 在java中以26个字符格式读取db2时间戳?毫秒的最后一个字符丢失

java - Apache Ant : adding and using a . txt 文件

eclipse - ClassNotFoundException::org.springframework.web.context.ContextLoaderListener

java - HibernateJPAVendorAdapter 和 JPAProperty 中的属性之间的优先级

java - 如何在 AWS 上定义 spring profile

amazon-web-services - AWS RDS "migrate snapshot"文档