Skip to content
Advertisement

Java Rest Template throws java.lang.IllegalArgumentException: Comparison method violates its general contract

I am not using any Comparators/Sorting in my Java code, still it is throwing ” java.lang.IllegalArgumentException: Comparison method violates its general contract!” Exception.

Below is the piece of code that throws exception on the restTemplate.exchange line on debugging.

Started getting this exception when I changed the version of spring-boot-starter-parent from 2.3.9 to 2.5.3

How should I resolve this?

JavaScript

Stack Trace:

JavaScript

Advertisement

Answer

The stacktrace indicates that for some reason the Comparator method for the MediaType.java class is not ok. As you pointed out that this seems to work locally but not in the cloud I would suggest you compare runtimes.

Looking through the source code for the AbstractMessageConverterMethodProcessor.java the sorting method is called when the content type for the response can not be found. Maybe you can force a content type by setting an accept header in the request. Assuming your expecting a JSON response:

headers.setAccept(List.of(MediaType.APPLICATION_JSON));

Not sure if this fixes it though, as I could not reproduce the issue in the first place.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement