spring-boot-plus 使用时的一些问题和部分用法(防止自己搞忘)

mapstruct 运行时报了 java.lang.ClassNotFoundException

使用idea2021.02版本的时候运行没有问题。某天我想升级一下idea版本,安装好后,重新打开项目,遇到了运行报错Handler dispatch failed; nested exception is java.lang.ExceptionInInitializerError的问题,一看是mapstruct的对象类型转换时失败导致的。
虽然俺啥都没改,但项目运行确实在报错,查阅了一些博客,在父级pom文件中添加了如下依赖:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.3.1.Final</version>
</dependency>

然后项目能正常运行了。

spring 测试类 注入依赖 示例

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author joi
 * @date 2023-03-01
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class BusinessTest {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private TableMapper tableMapper;

    /**
     * 获取某个接口的数据信息
     */
    @Test
    public void test01() {
        final String URL = "http://xxxxxx";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String, String> params = new HashMap<>();
        params.put("token", "61tK7s1234567890");
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<JSONObject> resp = restTemplate.exchange(URL + "?token={token}",
                HttpMethod.GET, httpEntity, JSONObject.class, params);
        final JSONObject respBody = resp.getBody();
        assert respBody != null;
        // 先转为json string,再进行对象转换
        final String userStr = JSON.toJSONString(respBody.get("data"));
        List<TableInfo> tableInfos = JSON.parseArray(userStr, TableInfo.class);
        tableInfos.forEach(i -> System.out.println(i.toString()));
        
        System.out.println(tableInfos.size());
        System.out.println("获取的信息条数: " + tableMapper.insertBatchTable(tableInfos));
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值