zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    BufferedOutputStream
    BufferedInputStream
    IO异常 的处理
    FileOutStream
    FileInputStream
    File常用的方法
    IO流
    枚举
    jdk1.5新特性之-----自动装箱与自动拆箱
    jdk1.5新特性之------->可变参数
  • 原文地址:https://www.cnblogs.com/lkc9/p/11834803.html
Copyright © 2011-2022 走看看