Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison method violates its general contract while using Spring Rest Template

One of our application is invoking another application via a Spring Rest Template.

HttpEntity<Object> httpEntity = new HttpEntity<>(null);
restTemplate.exchange(URL, HttpMethod.GET, httpEntity,String.class)

We haven't set any headers explicitly for the request. We are encountering the below exception:

Caused by: java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.TimSort.mergeHi(TimSort.java:895)
    at java.util.TimSort.mergeAt(TimSort.java:512)
    at java.util.TimSort.mergeCollapse(TimSort.java:437)
    at java.util.TimSort.sort(TimSort.java:241)
    at java.util.Arrays.sort(Arrays.java:1512)
    at java.util.ArrayList.sort(ArrayList.java:1454)
    at java.util.Collections.sort(Collections.java:175)
    at org.springframework.http.MediaType.sortBySpecificity(MediaType.java:441)
    at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:691)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:743)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:567)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448)

The Java version we are using is: 1.8.0_45 and Spring: 4.1.6

If anyone could help that would be really great. I will be happy to provide any more details on this if required.

Thanking in anticipation.

like image 385
Rahul Avatar asked Dec 20 '17 08:12

Rahul


1 Answers

I had this issue while migrating spring boot from 2.2.X to 2.5.6.

It happened when my other service was returning String from a @RestController method, but without any specified MediaType.

Specifying the media type in produces in the @GetMapping solved it for me:

@GetMapping("/this-returns-string", produces = MediaType.TEXT_PLAIN_VALUE)
like image 85
Léo Schneider Avatar answered Oct 23 '22 22:10

Léo Schneider