RestTemplate實現遠程調用並解決 java.util.LinkedHashMap cannot be cast to


基本簡介

參考過的資料:

案例的介紹:

  • SpringBoot版本:2.3.9.RELEASE
  • Maven 3.6.1
  • jdk 1.8
  • mysql 8 社區
  • 使用到的坐標:
    • myBatis-plus 3.4
    • fastJson 1.2
    • spring-boot-starter-web
  • 案例要實現的功能:根據對應文章表保存的用戶id查詢出該用戶信息
  • 案例包含兩個微服務模塊:文章和用戶
  • 數據庫設計:文章數據庫和用戶數據庫開發,
    文章表
    用戶表
  • 在案例之前 userAuth = null,案例后userAuth結果如下:
    {
      "flag": true,
      "code": 20000,
      "message": "操作成功",
      "data": {
      	"id": 1,
      	"userId": 1,
      	"categoryId": null,
      	"articleCover": null,
      	"articleTitle": "人工標題1",
      	"articleContent": "人工內容",
      	"type": 1,
      	"originalUrl": null,
      	"isTop": 0,
      	"isDelete": 0,
      	"status": 1,
      	"createTime": "2022-04-12T16:55:22",
      	"updateTime": null,
      	"userAuth": {
      		"id": 1,
      		"userInfoId": 1,
      		"username": "shisan@163.com",
      		"password": "$2a$10$jVUy2pE0JcEnx/owx1LZwe9A0y4ZWIcX3ZsieLFUvoLgP9JaJRAnu",
      		"loginType": 1,
      		"ipAddress": "127.0.0.1",
      		"ipSource": "",
      		"createTime": "2021-08-12T15:43:18",
      		"updateTime": "2022-04-11T23:42:55",
      		"lastLoginTime": "2022-04-11T23:42:55"
      	}
      }
    }
    
  • service層實現類代碼
    package com.cduy.blog.service.impl;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.cduy.blog.dao.ArticleDao;
    import com.cduy.blog.domain.Article;
    import com.cduy.blog.domain.UserAuth;
    import com.cduy.blog.service.ArticleService;
    import com.cduy.blog.vo.Result;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author CDUY
     * @version 1.0
     */
    @Service
    public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {
    
        @Autowired
        private ArticleService articleService;
    
        @Autowired
        private RestTemplate restTemplate;
    
        public Article queryArticleById(Integer articleId) {
            //1.查詢文章
            Article article = articleService.getById(articleId);
            //2.遠程查詢user
            //2.1url地址
            String url = "http://localhost:81/users/" + article.getUserId();
            //2.2發起調用 因為返回結果被封裝成了 Result
            Result<UserAuth> result = restTemplate.getForObject(url, Result.class);
            // 因為返回的數據是 Result 需要處理數據
            String jsonObject = JSON.toJSONString(result.getData());
            UserAuth userAuth = JSON.parseObject(jsonObject, UserAuth.class);
            //3.存入article
            article.setUserAuth(userAuth);
            //4.返回
            return article;
        }
    }
    
  • 在黑馬教程中,返回的數據沒有封裝,所以在得到的用戶數據可以直接賦值了article.setUserAuth(userAuth);,但這里需要對Json處理
  • 分布式結構中的微服務最近很是火熱,在學習過程中遇到錯誤java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.cduy.blog.domain.UserAuth,該錯誤是發生在用article.setUserAuth(userAuth);,因為在數據返回時將結果統一封裝成Result類,所以不可以簡單將Result.getData便取出該用戶數據了,而是應該先轉成json對象再轉換成java對象。

完整代碼

22年4月13日第一次提交的分支


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM