Jersey version issue: MessageBodyReader not found for media type=application/xml - jersey

While writing a simple Jersey client that was consuming XML data, I came across this exception "MessageBodyReader not found for media type=application/xml". All of my settings, including the jersey-client as maven dependencies was just fine.
The version that I was using was 2.17. Once I degraded the version to 2.15 it started working fine. Can anyone explain what dependencies that needs to be included for version 2.17 to work.
Maven Dependency (works on 2.15 and lower)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
Java Code Snippet for consuming the service
Client c = ClientBuilder.newClient();
WebTarget target = null;
target = c.target(Main.BASE_URI_XML);
String customerId = "415D7AB5";
XYZ response = target.path(customerId).request(MediaType.APPLICATION_XML).get(XYZ.class);

Have a look at 27.3. Migrating from Jersey 2.15 to 2.16
27.3.1.1. JAX-B providers separated from the core
From version 2.16 onwards, all JAX-B providers are being bundled in a separate module.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.17</version>
</dependency>

Related

Compatibility (as Spring boot Maven dependencies) between Elasticsearch 7.17, spring data elasticsearch and elasticsearch-rest-high-level-client

I have an Spring Boot + Elasticsearch application that had the now deprecated High Level Rest Client. I am trying to migrate the existing queries/methods to the new Java API Client but want to keep the HLRC for a bit (in case I break anything).
I seem to be running into a dependency problem that I'm not getting. I am currently getting the error java.lang.NoClassDefFoundError: org/elasticsearch/xcontent/ToXContentObject which I assume is due to compatibility issues. These are my current (relevent) dependencies:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.3.4</version>
</dependency>
The Elasticsearch is on 7.17 . As for the maven dependencies... I don't really know what versions to use so that it can run the old HLRC still while I test out newer code? (So I set the versions to the newest possible ones)
Edit infos:
current spring boot version is 2.2.8
before manually setting the version, org.elasticsearch.client was on 6.8.x
is my approach dumb and I should just scrap all the HLRC in favor of implementing/testing Java API Client?
Any pointers/fixes appreciated!
If I remember correctly it was in version 7.15 or 7.16 that Elasticsearch had a breaking change moving the xcontent classes to a different package. You cannot use Spring Data Elasticsearch 4.3.x with Elasticsearch 7.17

How to get Spring to use older version of Guava for specific Dependency, but newer version for "main" project

I have a dependency in my SpringBoot project:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cluster-zookeeper</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
Which requires Guava 16.01
However, my "main" project which I'm including cluster-zookeeper in requires Guava 28.2
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
When I run SpringBoot App, I get error:
The following method did not exist:
com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor()Lcom/google/common/util/concurrent/ListeningExecutorService;
The method's class, com.google.common.util.concurrent.MoreExecutors, is available from the following locations:
jar:file:/blah/.m2/repository/com/google/guava/guava/28.2-jre/guava-28.2-jre.jar!/com/google/common/util/concurrent/MoreExecutors.class
It was loaded from the following location:
file: /blah/.m2/repository/com/google/guava/guava/28.2-jre/guava-28.2-jre.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.common.util.concurrent.MoreExecutors
It seems like a version conflict issue where Spring tries to get cluster-zookeeper to use newer Guava version, when it require older one. How can I get this to work? I.e use 16.01 Guava for ONLY cluster-zookeeper while keeping 28.2 dependency for my main project?

java.lang.NoSuchMethodError: okio.BufferedSource.rangeEquals(JLokio/ByteString;)Z

I am integrating Outlook API and for making HTTP Calls I am using Retrofit version 2.3.0 and okHttp3 version 3.9.1.
However when I'm making an HTTP Call, for example :
// Create a logging interceptor to log request and responses
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel( HttpLoggingInterceptor.Level.BODY );
OkHttpClient client = new OkHttpClient.Builder().addInterceptor( interceptor ).build();
// Create and configure the Retrofit object
Retrofit retrofit = new Retrofit.Builder().baseUrl( authority ).client( client ).addConverterFactory( JacksonConverterFactory.create() ).build();
// Generate the token service
TokenService tokenService = retrofit.create( TokenService.class );
try
{
return tokenService.getAccessTokenFromAuthCode( tenantId, getAppId(), getAppPassword(), "authorization_code", authCode, getRedirectUrl() ).execute().body();
}
catch ( IOException e )
{
TokenResponse error = new TokenResponse();
error.setError( "IOException" );
error.setErrorDescription( e.getMessage() );
return error;
}
I am getting following exception :
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: okio.BufferedSource.rangeEquals(JLokio/ByteString;)Z
Below is my partial pom.xml :
<!-- JACKSON DEPENDENCY FOR JSON PARSING -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<!-- RETROFIT DEPENDENCY FOR SENDING HTTP REQUESTS -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.9.1</version>
</dependency>
Can some one help me figure out, what's wrong with this?
BufferedSource
is in okio project version 1.13.0.
Both dependencies com.squareup.retrofit2 and com.squareup.okhttp3 use this version. Also in this version this method is included. Version-wise it looks okay.
Local Environemnt
Now make sure to clear your maven repository. Maybe an old version got hung up somewhere. After that do a maven update project and a clean install.
Tomcat Environment
If this is happening in your tomcat make also sure to delete the work/Catalina/localhost/ folder, because sometimes things could be cached there.
I experienced a similar issue while executing a MapReduce job via YARN. In my case, an existing downgraded okio version was present which was overriding the external libraries of the application. I changed it to okio 1.13.0 and the issue was fixed.
It was this location for me:
/home/vagrant/bigdata/hadoop/share/hadoop/hdfs/lib
This could be because a conflict with an existing Okio version, provided by a dependency.
See Spark and Influx: OKIO conflict, there is a conflict with Apache Spark.
Use Maven / Gradle dep. tree export to see all transitive dep, or (in my case) :
jar tf /usr/hdp/current/spark-client/lib/spark-assembly-1.6.3.2.6.3.0-235-hadoop2.7.3.2.6.3.0-235.jar | grep okio
This will list:
okio/BufferedSource.class
Then extract the okhttp pom.xml:
jar xf /usr/hdp/current/spark-client/lib/spark-assembly-1.6.3.2.6.3.0-235-hadoop2.7.3.2.6.3.0-235.jar META-INF/maven/com.squareup.okhttp/okhttp/pom.xml
cat META-INF/maven/com.squareup.okhttp/okhttp/pom.xml | grep version
Experienced similar issue executing Spark job on EMR via YARN, as Okio/Okhttp dependencies for an external library used in the application were being overridden by those distributed on a system class path for Spark.
Resolution: Shade/relocate the Okio dependency in the external library's build.

Why Akka-Http still uses older Akka-Actor?

I have added the latest akka-http to my project but that includes the very old 2.4.19 version on akka-actor. Hence I also added akka-actor version 2.5.4 to the dependency. However, that results into following error:-
Detected java.lang.NoSuchMethodError error, which MAY be caused by incompatible Akka versions on the classpath.
My maven config is like below:-
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.11</artifactId>
<version>10.0.9</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
What am I missing? Is there any version of akka-http which uses the latest akka-actor?
Update: Added dependency graph
From the Compatibility Guidelines page in the documentation:
Akka HTTP 10.0.x is (binary) compatible with both Akka 2.4.x as well as Akka 2.5.x, however in order to facilitate this the build (and thus released artifacts) depend on the 2.4 series. Depending on how you structure your dependencies, you may encounter a situation where you depended on akka-actor of the 2.5 series, and you depend on akka-http from the 10.0 series, which in turn would transitively pull in the akka-streams dependency in version 2.4 which breaks the binary compatibility requirement that all Akka modules must be of the same version, so the akka-streams dependency MUST be the same version as akka-actor (so the exact version from the 2.5 series).
In order to resolve this dependency issue, you must depend on akka-streams explicitly, and make it the same version as the rest of your Akka environment....
Change your Maven dependencies to the following:
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.4</version>
</dependency>
<!-- Explicitly depend on akka-streams in same version as akka-actor -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream_2.11</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.11</artifactId>
<version>10.0.9</version>
</dependency>
</dependencies>

Maven dependencies needed for Jersey 2.x (2.6)

I'm trying to migrate from Jersey 1.x (1.2) to 2.x (2.6), I have trouble identifying the exact maven dependencies, jersey documentation is not comprehensive enough, it doesn't mention what maven dependencies are needed for the new version.
Does anyone have comprehensive list of maven dependencies needed for Jersey 2.x (2.6)?
Jersey doc
https://jersey.java.net/documentation/latest/migration.html#mig-1.x
For a servlet environment, the only dependency you need is
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.6</version>
</dependency>
This will pull in all you need. If you are in a servlet 2.5 environment, then you would use this instead
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.6</version>
</dependency>
Further information about 2.5 servlet, can be seen here
Alternatively, you could create a project from a Maven archetype, as seen here
UPDATE
Just as a note, the significance of using Jersey 2.6 is that it is the last version to support Java 6. If this is not a requirement for you, I would recommend using the latest version.

Resources