I am new to Jackson and I was writing some code for practice. I found out the the new version of Jackson library can be found on Fasterxml: Jackson, so I added the below dependencies to my Maven pom file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
I was expecting that I can use the ObjectMapper directly, however after spending a lot of time I found out that to use the ObjectMapper I have to add the old libraries below:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
I am a bit confused. Could someone please tell me why is that?
<properties>
<!-- Use the latest version whenever possible. -->
<jackson.version>2.4.4</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
you have a ObjectMapper (from Jackson Databind package) handy.
if so, you can do:
JsonFactory factory = objectMapper.getFactory();
Source: https://github.com/FasterXML/jackson-core
So, the 3 "fasterxml" dependencies which you already have in u'r pom are enough for ObjectMapper as it includes jackson-databind.
No, you can simply use com.fasterxml.jackson.databind.ObjectMapper.
Most likely you forgot to fix your import-statements, delete all references to codehaus and you're golden.
The package names in Jackson 2.x got changed to com.fasterxml1 from org.codehaus2. So if you just need ObjectMapper, I think Jackson 1.X can satisfy with your needs.
I spent few hours on this.
Even if I had the right dependency the problem was fixed only after I deleted the com.fasterxml.jackson folder in the .m2 repository under C:\Users\username.m2 and updated the project
Apart from fixing the imports, do a fresh maven clean compile -U. Note the -U option, that brings in new dependencies which sometimes the editor has hard time with. Let the compilation fail due to un-imported classes, but at least you have an option to import them after the maven command.
Just doing Maven->Reimport from Intellij did not work for me.
Related
I have recently explored handling JSON data with the org.json library and all went well.
Now I started a bigger Maven project, for which I intend to use the Jackson libraries in stead.
Sadly, it does not seem to work for me. I wanted to try out the ObjectMapper class, that VScode autocompleted for me, which also automatically adds the required import:
import com.fasterxml.jackson.databind.ObjectMapper;
However, I also immediately get an error on that line:
"The type com.fasterxml.jackson.databind.ObjectMapper is not accessible Java (16778666)"
I have added the necessary dependencies to my pom.xml file like so:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
Am I missing something? Are there any other steps that I should have taken?
The only dependency needed for ObjectMapper is
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
This error occurs, because Jackson library is not included in your project's classpath. Probably your project is using the Java Platform Module System (JPMS); then log would also contain:
... declared in the "unnamed module" ... module does not read it.
If this is the case, add a requires directive to module-info.java file to specify that this module requires the jackson-databind library:
module correct.module.name {
requires com.fasterxml.jackson.databind;
}
After adding this requires directive recompile the project again.
This is not a Maven problem. You probably have created a package-info.java for your project and so all your dependencies ended up on the module path but your package-info.java is missing the corresponding declarations.
You have to add a line like this:
requires com.fasterxml.jackson.databind;
See also: How to fix 'Package is declared in module, but module does not read it' error in IntelliJ JavaFX?
I'm trying to use google-vision to fetch text from an image (uploaded to AWS S3) and store it in AWS Dynamo DB. I'm encountering dependency conflicts on jackson-core as both google-api and aws-java-sdk are using two different versions.
Dependency Hierarchy
google-api-client: 1.22.0 uses jackson-core: 2.1.3
google-cloud-vision: 0.22.0-beta uses jackson-core: 2.1.3
aws-java-sdk: 1.11.106 uses jackson-core: 2.6.6
I tried "exclusions" and added explicit dependency in pom.xml to use jackson-core: 2.6.6. Google-vision api works fine with that change. However, AmazonDynamoDBClientBuilder fails with below error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method com.amazonaws.AmazonWebServiceClient.<init>(Lcom/amazonaws/client/AwsSyncClientParams;)V from class com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder.build(AmazonDynamoDBClientBuilder.java:60)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder.build(AmazonDynamoDBClientBuilder.java:26)
at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at com.oneglint.ImageProcessing.AddItem.main(AddItem.java:133)
Following error is displayed when there was version conflict
Exception in thread "main" java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:537)
at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:448)
at com.amazonaws.partitions.PartitionsLoader.<clinit>(PartitionsLoader.java:51)
at com.amazonaws.regions.RegionMetadataFactory.create(RegionMetadataFactory.java:30)
at com.amazonaws.regions.RegionUtils.initialize(RegionUtils.java:64)
at com.amazonaws.regions.RegionUtils.getRegionMetadata(RegionUtils.java:52)
at com.amazonaws.regions.RegionUtils.getRegion(RegionUtils.java:105)
at com.amazonaws.client.builder.AwsClientBuilder.withRegion(AwsClientBuilder.java:239)
at com.oneglint.ImageProcessing.AddItem.main(AddItem.java:132)
What am I missing here? Thanks for the help..
BTW, I'm using example code from github to achieve this. Here are the links:
DynamoDB example: https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/java/example_code/dynamodb
Google Vision DetectText example: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/vision/cloud-client/src/main/java/com/example/vision/Detect.java
Additional Details
Both the examples are working fine if executed as independent projects. The problem occurs ONLY when both PutItem (AWS) & Detect (google-vision) classes are brought together in a single project, with appropriate code changes.
You can only have one version of jackson-core in your project. The easiest way to fix a version is to use <dependencyManagement> to set a version.
Your main problem is that jackson-core: 2.6.6 is not compatible with AmazonDynamoDBClientBuilder. The usual strategy is to try all versions from 2.1.3 to 2.6.6 until one of them works. If not, you can try to find versions of your amazon and google jars that require the same Jackson-core-version. In any case, this stupid and boring try-and-error.
If you do not come to any working solution, you can try to shade classes with maven-shade-plugin (I have not tried this, probably difficult) or you need to change your project in a way that not both dependencies are required.
After a lot of trial and error approach, the issue is finally solved.
It appears that I added multiple versions of aws-java-sdk jars during the process and an opennlp jar was associated with the project for some other module.
I had removed conflicting versions of aws-java-sdk and unnecessary libraries. Also, removed the exclusions and retained only the dependency addition in <dependencymanagement> for jackson-core.
Dependencies in my final pom listed below:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.106</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.6</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-vision</artifactId>
<version>v1-rev358-1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
<exclusions>
<exclusion> <!-- exclude an old version of Guava -->
<groupId>com.google.guava</groupId>
<artifactId>guava-jdk5</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>0.22.0-beta</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
</dependency>
</dependencies>
Hope this helps others..
I'm using auth0-java client library to interact with auth0 v2 service, my codes compile and works fine at my development environment but when I deploy that build in another test environment it throws the following exception:
java.lang.NoSuchMethodError:com.fasterxml.jackson.databind.ObjectMapper.readerFor(Lcom/fasterxml/jackson/databind/JavaType;)Lcom/fasterxml/jackson/databind/ObjectReader;
at com.auth0.json.mgmt.users.UsersPageDeserializer.getArrayElements(UsersPageDeserializer.java:52)
at com.auth0.json.mgmt.users.UsersPageDeserializer.deserialize(UsersPageDeserializer.java:30)
at com.auth0.json.mgmt.users.UsersPageDeserializer.deserialize(UsersPageDeserializer.java:15)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2597)
at com.auth0.net.CustomRequest.parseResponse(CustomRequest.java:63)
at com.auth0.net.BaseRequest.execute(BaseRequest.java:37)
at com.myapp.security.Auth0Service.getUserByEmail(Auth0Service.java:246)
at com.myapp.security.Auth0Service.checkForExsistingAuth0Account(Auth0Service.java:266)
at com.myapp.security.AdminUILayout.lambda$launchProgressUpdater$0(AdminUILayout.java:293)
at java.lang.Thread.run(Thread.java:745)
I've already gone through several stackover flow questions like this and tried by cleaning .m2/repository/com/fasterxml folder of that test environment, but cloudn't solve the no such method error
here is my pom file and related codes are given below:
code
public User getUserByEmail(String email){
UserFilter filter = new UserFilter();
filter.withQuery(email);
Request<UsersPage> request = mgmt.users().list(filter);
try {
UsersPage response = request.execute();
for (User u:response.getItems()) {
if (u.getEmail().equals(email)) {
return u;
}
}
} catch (APIException exception) {
// api error
System.out.println("APIException:::::"+exception.getDescription());
} catch (Auth0Exception exception) {
// request error
System.out.println("Auth0Exception:::::"+exception.getMessage());
}catch(Exception ex){
System.out.println("Other exception:::::"+ex.getMessage());
}
return null;
}
pom.xml
<properties>
<jackson.version>2.8.5</jackson.version>
</properties>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<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.auth0</groupId>
<artifactId>auth0</artifactId>
<version>1.0.0</version>
</dependency>
Update
I've moved those jackson dependencies into <dependencyManagement> section of my pom.xml file,
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
</dependencyManagement>
according to maven documentation ,no matter which version of library jackson-core is requested by a dependency, version ${jackson.version} will always be used, still my codes works in local ,but in that test server
the the exception remains same, even if I deploy my war file in that test server code throws the same exception.
most likely you have compiled your code against a different version of the class, than the one you are using when running it.
Please make sure that on the environment where you run the code there is no any other version of jackson-databind dependency on your classpath.
finally solved the issue , I found that glassfish 4.1.1 has a very old version 2.3.2 of jackson-core jackson-annotation and jackson-databind, so we decided to use Payara server that uses fairly latest version of jackson jackson 2.8.5 and tested the no such method error is solved, the code works fine in that Payara server
Update:
glassfish version 5.0 uses updated version jackson 2.8.9 , I think using that will solve this issue too
Probably one of your other artifacts depends on different version of databind. You can try to exlude it explicitly from each of them to guess
<dependency>
...
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
</exclusions>
</dependency>
Please check version.
Screenshot:
For me, I encountered the same exception due to lower version of jackson-databind:
From:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
Changed to:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
I just update my pom.xml file with higher version of jackson-databind.
Then right-click Project --> Update Maven Project and the exception is resolved.
<!-- faster JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.0</version>
</dependency>
We have a web application that used to be deployed as a war in Jboss AS 7 . This webapp uses both reasteasy and jackson( 2.0 ) to serialize and deserealize data from a mongodb database. Some time ago we had to separate the webapp into 2 different maven modules , and API (jar) and WAR . all our POJOs were on the jar module . The problem is that all the jackson annotation became useless. They are just completely ignored .
More info
jboss-deployment-structure.xml :
<exclusions>
<module name="org.codehaus.jackson.jackson-core-asl"/>
<module name="org.codehaus.jackson.jackson-mapper-asl"/>
</exclusions>
POM (API)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
POM(WAR)
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>
I read that reasteasy modules and jackson modules could somehow conflict since resteasy could be using some old jackson version . How can i check this ? Or is this a problem of the jboss classloaders ? (Jackson 2 annotations ignored in EJB Jar with JBoss (6.2.0 GA))
#NokusFerreira, basically you are correct. I know this is an old question, but I hope this answer will help somebody.
Since you haven't told when problem occurs (during serialization in JAR or during deserialization in WAR). I'm guessing that during deserialization inside WAR module.
If yes, then your WAR is probably using resteasy-client and resteasy-jackson-provider, and here problem lies - because to work with Jackson 2.x RESTEasy needs a different module: resteasy-jackson2-provider (please note "2" suffix) instead of old resteasy-jackson-provider.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.11.Final</version>
</dependency>
But unfortunatelly, resteasy-jackson2-provider is not bundled with JBoss 7.1.
So basically you have following options:
Migrate to a newer server (like WildFly).
Upgrade RESTEasy library on the JBoss.
Downgrade your code to be using Jackson 1.x
It is a common inconvenience in a JEE world, that libraries declared as your dependencies are provided by the application server (and may differ from ones that you are thinking you are using).
EDIT
Also this answer may be useful: How to make Resteasy 2.3.6 use Jackson 2.+
I'm following a tutorial on Vogella on how to work with JAX-RS to create RESTful web applications.
The trouble for me is that I am not able to import the dependencies through Maven. Is it possible?
When I try finding jsr311 or javax.ws.rs as suggested here, Maven doesn't seem to know it exists.
If you are satrting your project from scratch, it would be better to let maven generate your project for your using one of the Maven Archetypes that Jersey provides (More in the Jersey getting-started page) then you can easilly add the eclipse nature to the generated project using below command:
mvn eclipse:eclipse -Dwtpversion=2.0
Choose your suitable Web Tool version, then import that project into your Eclipse IDE.
This method, will leave you out of poviding any dependencies related to Jersey as those are already mentioned in the archetype descriptor.
Otherwise, if you are already working on a project and you want to add the RESTful features (which assume is not true since you mentioned that you are following a tutorial), you will have to provide dependencies to Jersey yourself. All dependencies can be found in Maven Central Repo but you would only need the jersey-server one:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
As #Gimby stated, there is absolutely no sense in declareing the jsr311-api alone, only if you are intending to provide a JSR implementation :)
please try as following:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
Ended up finding a similar question here.
Solved the problem by manually adding the dependency under pom.xml:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
foun it on : (http://mvnrepository.com/artifact/org.glassfish.jersey.archetypes/project/2.22.1)
it will work inchalahh ;D
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>