Exception while instantiating RestTemplate - spring

Am using Rest Template to consume web services in a Maven project and packaged the project to jar to add as a dependency to my other web application
but throwing Exception to console at instantiating the RestTemplate even though i put the code to catch the exception, its directly throwing the below Exception to console. can anyone know the reason?
try{
RestTemplate restTemplate = new RestTemplate(); //getting the Exception here
}
catch(Exception e){
e.printStackTrace(); // but its not coming here
}
the dependenices i have added in my pom.xml file :
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- JSON-Binding -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>1.1.0.1</version>
<scope>provided</scope>
</dependency>
the Exception at server side is :
Root cause of ServletException.
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.SerializationConfig.withDefaultPrettyPrinter(Lcom/fasterxml/jackson/core/PrettyPrinter;)Lcom/fasterxml/jackson/databind/SerializationConfig;
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:86)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:67)
at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:63)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder$XmlObjectMapperInitializer.create(Jackson2ObjectMapperBuilder.java:807)
at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:585)
Truncated. see log file for complete stacktrace
>

You might want to include the correct version jackson-databind jar file in classpath. The servlet container has not started to hit your code base.

Provide all the below dependencies in your pom.xml with same version:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
This should help.

Related

ClassNotFoundException: com.fasterxml.jackson.core.util.JacksonFeature in Spring boot, upgrading from Elasticsearch HLRC to Java API Client

I wanted to swap out the deprecated High Level Rest Client with the new Java API Client from Elasticsearch. Problem is, I'm not sure if my dependencies are configured correctly! I'm running into this error:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/JacksonFeature
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.JacksonFeature
Elasticsearch just got upgraded to 7.17.3 and Spring Boot is 2.2.8 right now, it will be upgraded later but I'm not sure if that's the problem? Here are my dependencies in the pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-webmvc-core</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-security</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.17.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
I followed the documentation from Elasticsearch here so I tried it with the 2.12.3 version of jackson-databind as well but the error persists. Any idea on what should be changed? Thanks!
Edit: just in case it helps, here's also my client which is where the dependency is needed (I assume)
#Configuration
public class ElasticsearchClient{
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
public ElasticsearchClient client = new ElasticsearchClient(transport);
}
(I had to put the public in front of client because it sits under another package and won't let me call on it unless it's there)
more info after playing around: turns out there is a parent and it would load an older jackson-databind, so I did the following
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-schema-registry-client</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
still doesn't work so I'm 200% out of ideas now
Add jackson-core solved my problem.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>

Integrate Keycloak Admin Client 3.1.0.Final with Spring Boot 1.5.1

I'm trying to integrate KeyCloak Admin Client with Spring Boot
But there is an exception is thrown when I was trying to create a new account:
Caused by: javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class org.keycloak.representations.AccessTokenResponse
So I tried to explicitly register Jackson Provider for KeyCloak like this:
KeycloakBuilder
.builder()
.serverUrl(SERVER_URL)
.realm(REALM)
.username(USERNAME)
.password(PASSWORD)
.clientId(CLIENT_ID)
.resteasyClient(new ResteasyClientBuilder()
.providerFactory(factory.register(ResteasyJackson2Provider.class))
.connectionPoolSize(10)
.build())
.build();
But I'm unable to import ResteasyJackson2Provider.class
pom.xml
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-security-adapter</artifactId>
<version>3.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>3.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.3.Final</version>
</dependency>
Btw, if I use resteasy-jackson-provider, I got this exception:
javax.ws.rs.client.ResponseProcessingException: javax.ws.rs.ProcessingException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "access_token" (Class org.keycloak.representations.AccessTokenResponse), not marked as ignorable
at [Source: org.jboss.resteasy.client.jaxrs.internal.ClientResponse$InputStreamWrapper#7cc842b0; line: 1, column: 18] (through reference chain: org.keycloak.representations.AccessTokenResponse["access_token"])
After a while, I figured out the solution to this problem:
Firstly, I tested it out with the standalone project (empty maven project), the problem still occurs because of resteasy-jackson-provider. It should be resteasy-jackson2-provider (note that its resteasy-jackson2-provider)
For now, I was known that the problem somehow comes from Spring Boot, RESTeasy. And finally, I come up with this working pom.xml:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>3.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.1.3.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.8</version>
</dependency>
One more thing to note is, you should use the admin-cli client in your Keycloak, because by default, it has Direct Access Grants Enabled on
Btw, this is the Java configuration:
KeycloakBuilder
.builder()
.serverUrl("localhost:8080/auth")
.realm("master")
.username("YOUR_USERNAME")
.password("YOUR_PASSWORD")
.clientId("admin-cli")
.build();
Working example: https://github.com/phuongtailtranminh/Keycloak-Admin-Client-Spring-Boot-Demo

java.lang.NoClassDefFoundError: com/thoughtworks/xstream/io/naming/NameCode error in setting up restful webservice in spring 4.3.1

I want to create rest controller in spring but I get this error :
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0'
defined in ServletContext resource
[/WEB-INF/mvc-dispatcher-servlet.xml]: Initialization of bean failed;
nested exception is java.lang.NoClassDefFoundError:
com/thoughtworks/xstream/io/naming/NameCoder
These jar files were added to my project correctly :
jackson-annotations:2.1.1/
jackson-core:2.1.1/
jackson-databind:2.1.2
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
Try adding this to your pom:
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
I faced same problem in my first spring boot web application, even after adding aforementioned dependency if it does not solve this issue, we could try deleting .m2 local repository, and then do maven build, and maven install.
In my case I had to add:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
<version>2.21.0</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
to make everyone happy.

Apache Wink integration with Spring

I am trying to integrate Apache wink 1.4 with Spring on JBoss EAP 6.4. Below is my Pom.xml excerpt.
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-server</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-spring-support</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-client</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-json4j</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.jboss.archetype.eap</groupId>
<artifactId>jboss-javaee6-webapp-archetype</artifactId>
<version>6.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
I also have configuration in web.xml to look for winkdefault.properties.
While building the application, It is not able to find the "META-INF/wink-default.properties" I am getting following exception in server log.
[org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 96) Context initialization failed: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/wink-default1.properties]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/wink-default.properties] cannot be opened because it does not exist
Could anyone help on this please ?
I decompiled wink-spring-support.jar and changed context with following classpath:META-INF/server/wink-core-context.xml and it was able to register.

Spring MVC Hibernate - Build path is incomplete

I was working on a spring mvc maven project and in initial stage I can across this error.
in my servlet-context.xml, I am trying to generate "HibernateTransactionManager" bean which throws me this errr :
error: "Build path is incomplete. Cannot find class file for org/hibernate/HibernateException"
as this being a maven project I have added the following dependency in pom.xml
<!-- Dependency added for spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Dependency added for Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- Dependency added for AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<!-- Dependency for Hibernate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${org.hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${com.mysql-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>20030825.183949</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- Dependency for Http Client -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>rome</groupId>
<artifactId>rome</artifactId>
<version>0.5</version>
</dependency>
I can even locate all my classes in my Maven dependency tree.
I have also tried to externally add a new .jar for spring-orm but that didnt work as well. I have tried to clean build and re-compile several times but that didn't work out.
I would really appreciate any of your inputs.
This is my first SO answer, so please bear with me.
The error generated is “Cannot find class file for org/hibernate/HibernateException”. The POM you have provided lists all the dependencies your project and including a reference to the hibernate-validator jar.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
If you extract this jar file and search its contents, no class file named HibernateException can be found. This is the root cause of the error and you a repository that contain the HibernateException class file.
As you are using maven, you can see all the repositories related to Hibernate here
http://mvnrepository.com/artifact/org.hibernate
I’ve selected the hibernate-core-4.2.0.Final as core would suggest it has most of the main class files hibernate requires. Downloading and extracting the jar shows that the HibernateException class file exists here.
Therefore, if you update your POM.xml with the following entry
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.0.Final</version>
</dependency>
Now you have to make Maven download the repository to your local machine if it doesn’t exist and use it for your project. If you are running the project from Elcipse, do this by right-clicking the project->Run As-> Maven Clean.
Hope this helps.
if you are sure that maven has downloaded the hibernate jar's and are available in/to the jar/war file generated from the build then the issue could be as follows.
sometimes Maven fails to download the complete JAR file from the server, a class not found exception can occur in those cases as well.
to fix this goto .m2 folder in your home directory, delete the hibernate jar's and make a clean install. this will download the new set of files and things should start to work.
you can check if the files were downloaded correctly by opening them as a zip file jar tf jar-file.jar

Resources