天津制作个人建站,吉林建筑大学本科招生网,网站 服务器 域名,怎样制作公司的网页背景#xff1a;
接收一个springcloud项目#xff0c;UI模块访问其他服务的接口#xff0c;返回数据统一都是使用fastjson进行转换#xff0c;但是新开发了几个新模块之后发现fastjson很多bug#xff08;各种内存溢出#xff09;#xff0c;但是很多地方已经重度依赖fa…背景
接收一个springcloud项目UI模块访问其他服务的接口返回数据统一都是使用fastjson进行转换但是新开发了几个新模块之后发现fastjson很多bug各种内存溢出但是很多地方已经重度依赖fastjson只是升级改掉了内存溢出的某些代码最近突然想起来RestTemplate明明有定义返回值为啥不直接使用因为我是为了统一规范按照以前调用模块的写法进行开发于是说试就试
1. 调用接口被调用接口我是统一返回json也就是使用RestController注解
ReturnT responseEntity restTemplate.postForObject(http://THPWPSERVICE/xx, 实体参数, ReturnT.class);直接使用定义好的统一返回类型ReturnT进行接收返回值然后访问页面无数据后台报错如下
databind.exc.MismatchedInputException: Cannot construct instance of................
网上各种查询发现原因是要想接收指定类型这个指定类型必须继承序列化且还必须有有参构造函数以及午餐构造函数于是添加AllArgsConstructor 、NoArgsConstructor注解再implements Serializable完整统一返回类型的实体定义如下
重启程序再次访问终于拿到数据了 以下记录下一些RestTemplate调用注意事项
1. 被调用接口如果形参是实体那么就在实体之前加上注解RequestBody否则无法接收到参数
2. 被调用接口如果形参是基础类型参数如String、int类型等等那么就在类型前加上注解RequestParam否则无法接收到参数
3. 如果参数非实体类型那么建议使用get请求 String json restTemplate.getForObject(http://XXXSERVICE/getById?idid, String.class);
以下是一些常用调用方法
1. 带实体入参使用post请求layui表格返回值直接返回json格式即可无需解析return restTemplate.postForObject(http://THPWPSERVICE/supplies/getItems, form, String.class); 2. 参数少使用get请求 返回json实体再转成实体类再返回页面String json restTemplate.getForObject(http://THPWPSERVICE/supplies/getByItemCode?itemCodeitemCode, String.class);s JSON.parseObject(json, new TypeReferenceSuppliesItem() {});3. 转list ids JSON.parseObject(json, new TypeReferenceList() { });4 . BookDto bookDto restTemplate.getForObject(url, BookDto.class); -----待尝试5. ResponseEntityBookDto responseEntity restTemplate.getForEntity(url, BookDto.class); -----待尝试//状态码System.out.println(responseEntity.getStatusCode());//获取头System.out.println(头 responseEntity.getHeaders());//获取bodyBookDto bookDto responseEntity.getBody();6. ResponseEntityListBookDto responseEntity -----待尝试restTemplate.exchange(url,HttpMethod.GET,null,new ParameterizedTypeReferenceListBookDto() {});ListBookDto bookDtoList responseEntity.getBody();7. BookDto result restTemplate.postForObject(url, 参数, BookDto.class);