报错信息:java.lang.NoSuchMethodError: org.json.JSONArray.iterator()Ljava/util/Iterator…
错误原因:https://github.com/spring-projects/spring-boot/issues/8706
主要原因是
org.springframework.boot:spring-boot-starter-test -> 1.5.2.RELEASE has an indirect dependency on com.vaadin.external.google:android-json:0.0.20131108.vaadin1 via org.skyscreamer:jsonassert:1.4.0.
The reference documentation mentions both org.skyscreamer:jsonassert:1.4.0 and org.json:json:20140107. As far as I understand, they should be compatible. However, com.vaadin.external.google:android-json:0.0.20131108.vaadin1 conflicts with org.json:json:20140107 as it represents the re-implementation of some older version of org.json:json.
In particular, org.json.JSONTokener#JSONTokener(java.io.Reader) exists only in org.json:json:20140107 and is absent in com.vaadin.external.google:android-json:0.0.20131108.vaadin1, causing my tests to fail with java.lang.NoSuchMethodError: org.json.JSONTokener.(Ljava/io/Reader;)V exceptions.
The solution to my issue is not to use spring-boot-starter-test, but instead use spring-boot-test, spring-boot-test-autoconfigure and spring-test directly and add all other dependencies as needed.
In a case, that org.skyscreamer:jsonassert:1.4.0 is required, exclude com.vaadin.external.google:android-json:0.0.20131108.vaadin1.
If looking for the JSON assertions library consider net.javacrumbs.json-unit:json-unit-fluent that has similar to AssertJ fluent API.
引入了两个org.json包,其中一个是旧版本,不存在iterator方法。
解决方案1:
标记为不包含:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
</exclusion>
</exclusions>
</dependency>
解决方案2:
不使用foreach循环调用iterator,使用普通for(int index=0; index<length; index++)循环。