我希望你能帮助我.. 我有一个 spring-boot 应用程序,它在一个文件夹中加载一些 jar(带有带注释的类和方法@Test
)@TestFactory
,并在 JSON 中加载所有 jar 名称 - 类名称 - 方法Test
名称。当我调用 get 服务时/getTestList
,我会像这样查看这个 json,例如:
{jarFile:{classFileName:{methodTestName1:"xxxx",methodTestName2:"yyy"},{classFileName2:{methodTestName1:"xxxx",methodTestName2:"yyy"} }
这是Controller.class:
@RestController
public class Controller {
public JarClassMethod getAllTests() throws IllegalArgumentException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException, IOException,LinkageError, ClassNotFoundException {
JarClassMethod testClassObjMap= new JarClassMethod();
LoadLibrary loadLibrary=new LoadLibrary();
List<JarFile> jarList= loadLibrary.getListJar().stream().map(f -> {
try {
return new JarFile(f);
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.toList());
for (JarFile j : jarList) {
for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
JarEntry entry = entries.nextElement();
String file = entry.getName();
if (file.endsWith(".class")) {
String classname = file.replaceAll("/", ".")
.substring(0, file.lastIndexOf("."));
try {
Class<?> currentClass = Class.forName(classname);
List<String> testMethods = new ArrayList<>();
for (int i = 0; i < currentClass.getMethods().length; i++) {
Method method = currentClass.getMethods()[i];
Annotation annTest = method.getAnnotation(Test.class);
Annotation annTestFactory = method.getAnnotation(TestFactory.class);
if (annTest != null || annTestFactory != null) {
testMethods.add(method.getName());
}
}//fine for metodi
if (testMethods.size() >=1) {
testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\")+1),classname,testMethods);
System.out.format("%s %s %s",j.toString(),classname,testMethods);
}
}catch (NoClassDefFoundError e) {
System.out.println("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
}
catch (Throwable e) {
System.out.println("WARNING: failed to instantiate " + classname + " from " + file);
}
}//if .class
}//chiudo jarentry for
}//chiudo jarfile for
return testClassObjMap;
}
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/getTestList", method = RequestMethod.GET)
public ResponseEntity<JarClassMethod> getTestList() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
JarClassMethod testClassObjMap=this.getAllTests();
return new ResponseEntity<>(testClassObjMap, HttpStatus.OK);
}
使用方法getAllTests()
我首先像外部库一样加载 jar,然后使用一个用于迭代所有 jarsFile,另一个用于迭代所有类,最后使用另一个用于使用注释@Test
或@TestFactory
.
在我的 Windows 电脑上它可以正常工作,但是当我打包项目并将这个 jar 放在 Linux 服务器上时,我遇到了这个错误:
There was an unexpected error (type=Internal Server Error, status=500).
loader constraint violation: when resolving method
"com.fasterxml.jackson.databind.ser.impl.ReadOnlyClassToSerializerMap.untypedValueSerializer(Ljava/lang/Class;)Lcom/fasterxml/jackson/databind/JsonSerializer;" the class loader (instance of
org/springframework/boot/loader/LaunchedURLClassLoader) of the current class,
com/fasterxml/jackson/databind/SerializerProvider, and the class loader (instance of
sun/misc/Launcher$AppClassLoader) for the method's defining class,
com/fasterxml/jackson/databind/ser/impl/ReadOnlyClassToSerializerMap, have different Class objects for
the type com/fasterxml/jackson/databind/JsonSerializer used in the signature
我试图从 spring-boot 依赖项中排除 Jackson,但 Response ofgetTestList()
不起作用。其他 Jars 文件也使用了 Jackson 库。
这是我的 pom.xml:pomxml
请帮助我.. 我累了 3 天。:-P 谢谢和
问候朋友..