Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8

简介: Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8

强烈推荐一个大神的人工智能的教程:http://www.captainai.net/zhanghan


【前言】


       最近在用restTemplate进行一次http请求时发现了报错(Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8)经过一番尝试后最终解决问题,在此记录一下。


【问题及解决】


       一、封装http调用的方法如下:


public <T> ResponseEntity<T> postJson(String url, String requestJson, Class<T> responseType) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
    ResponseEntity<T> responseEntity;
    System.out.println(requestJson);
    responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    return responseEntity;
  }


       二、提供方为的出参和入参均为json格式;


       三、单步进行调试发现报如下错:


1635dbc69a973fbc560cb747590b9602_watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTI4MjkxMjQ=,size_16,color_FFFFFF,t_70.jpg


       四、在网上查了一些资料并进行了相关验证,对调用方法设置对返回值的格式为json即可解决,相应代码如下:


public <T> ResponseEntity<T> postJson(String url, String requestJson, Class<T> responseType) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
    //设置接收返回值的格式为json
    List<MediaType> mediaTypeList = new ArrayList<>();
    mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
    headers.setAccept(mediaTypeList);
    HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
    ResponseEntity<T> responseEntity;
    System.out.println(requestJson);
    responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
    return responseEntity;
  }


【小结】


      在之前的项目中不用设置接收值的格式也是可以的,之前系统用的SpringBoot版本是1.5.x,现在的项目是将Spring Boot版本升级到2.x;在进行新项目的开发时建议使用比较新的稳定版本的框架,如果是迁移则建议沿用老版本,如果老版本代码向新框架迁移一定要进行全流程的测试,不然可能会有新老版本不兼容而导致的bug。


相关文章
|
4月前
|
XML 数据格式
restTemplat发post请求报错Content type ‘application/xml;charset=UTF-8‘ not supported“
restTemplat发post请求报错Content type ‘application/xml;charset=UTF-8‘ not supported“
|
XML Java 数据格式
xml文件报错Element ‘beans‘ cannot have character [children), because the type‘s content type is element-
xml文件报错Element ‘beans‘ cannot have character [children), because the type‘s content type is element-
xml文件报错Element ‘beans‘ cannot have character [children), because the type‘s content type is element-
|
缓存 Java Android开发
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
268 0
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片
|
Android开发
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片(三)
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片(三)
112 0
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片(三)
|
Android开发
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片(二)
【Android 应用开发】Android开发技巧--Application, ListView排列,格式化浮点数,string.xml占位符,动态引用图片(二)
115 0
|
XML Shell 数据格式
The whole Fiori application is wrapped in a big shell xml view
The whole Fiori application is wrapped in a big shell xml view
79 0
The whole Fiori application is wrapped in a big shell xml view
|
XML 数据格式
SAP UI5 xml view content parse
Created by Wang, Jerry, last modified on May 18, 2015
SAP UI5 xml view content parse
|
XML Java 数据格式
Myeclipse中java web.xml报错cvc-complex-type.2.3: Element 'web-app' cannot have character [children], because the type's content type is element- only.
web.xml文件添加servlet访问限制后出现如下错误:   cvc-complex-type.2.3: Element 'web-app' cannot have character [children], because the type's content type is element- only. 翻译:   cvc-complex-type.2.3:元素'web-app'不能包含character [children],因为该类型的内容类型是仅包含元素的。
3800 0