Search code examples
javaspring-bootkubernetesorg.json

NoSuchMethodError in org.json.JSONObject after kubernetes upgrade from 1.21 to 1.23


I don't know if the kubernetes upgrade has anything to do with the problem, but it's the only change I know of between the time the application was working as expected and when it started throwing this exception.

I have some code in a Spring Boot app that adds a List<Object> to a JSONObject. The line looks like this:

jsonObject.put("some_name", someList);

This code hasn't been touched in a year, but suddenly started throwing this exception after a k8s upgrade from 1.21 to 1.23:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: 'org.json.JSONObject org.json.JSONObject.put(java.lang.String, java.util.Collection)' at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1082)

[...]

Caused by: java.lang.NoSuchMethodError: 'org.json.JSONObject org.json.JSONObject.put(java.lang.String, java.util.Collection)' at com.example.service.MyClass.convertToJson(MyClass.java:109)

The k8s upgrade also included a change from Java 17.0.5+8 to Java 17.0.6+10, but everything runs as expected locally using both of those JDK versions.

I ran a mvn dependency tree -Dverbose and couldn't find anything weird there either. These were the only entries that had to do with json.

org.json:json:jar:20220320

org.springframework.boot:spring-boot-starter-json:jar:2.6.4

I'm out of ideas for what might be causing this issue, so any suggestions on what to look at next are welcome.


Solution

  • It could be that the version of org.json:json included in your project is out of date, and doesn't include the put(String, Collection) method you're trying to use. Try updating this dependency to a more recent version and seeing if that resolves the issue. It's also possible there are some other dependencies in your project which conflict with org.json:json or override it with an older version - check for any similarly named libraries (e.g., json-simple instead of org.json). You may also want to check the version of Spring Boot you're using, as this might be incompatible with the org.json library you're trying to use.