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));
}
}