• springboot中RestTemplate 的getForObject 报错ClassCastException错误


    1、getForObject的定义如下:

        public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException {
            RequestCallback requestCallback = this.acceptHeaderRequestCallback(responseType);
            HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger);
            return this.execute(url, HttpMethod.GET, requestCallback, responseExtractor);
        }

    当responseType的泛型T中包含 List 的时候,结果无法转换成 List,这时候便会包ClassCastException 错误

    2、解决方案

    使用 exchange() 方法代替 getForObject

    示例:RegulateCommand 为带有 List 的类

    RegulateCommand regulateCommandResult = restTemplate.exchange(customConfig.getBaseUrl() + "/sws/regulate_pressure/executing",
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<RegulateCommand>() {}).getBody();
  • 相关阅读:
    python面试题目
    Mysql综合练习作业50题
    django rest framework自定义返回格式
    mongodb副本集部署
    celery学习笔记
    npm学习笔记
    awk学习笔记
    SaltStack grains学习笔记
    nginx + uwsgi + centos7部署django
    Reids集群安装
  • 原文地址:https://www.cnblogs.com/lkc9/p/11834803.html
Copyright © 2020-2023  润新知