Closed rushabh0701 closed 10 months ago
Hi.
What version of jsonschema2pojo are you using? We should check that the version you are using has this feature.
but I am running the code using Maven plugin and there I do not see an option to enable constructors.
I'm not sure what you mean by this, where did you look for the option?
You should be able to enable it like this in your pom.xml, inside the jsonschema2pojo settings:
<includeConstructors>true</includeConstructors>
This should work for JSON and JSON Schema, so I think this setting is not being set correctly.
From your screenshot, it looks like you are running the spring-boot:run
goal. I don't know how your build is configured, but this is probably not going to cause jsonschema2pojo to run. My guess is that you already have generated sources and compiled classes that are being used when you run spring-boot:run
. You need to add this settings during the build that is actually running jsonschema2pojo.
I am running version 1.2.1. Can you show an example on how to include constructor using pom.xml file.
Like this:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>com.example.types</targetPackage>
<includeConstructors>true</includeConstructors>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
I am getting below error:
Execution default of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.has(String)" because "schemaNode" is null (org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate:default:generate-sources)
org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.has(String)" because "schemaNode" is null
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:148)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:336)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.lambda$8(MavenImpl.java:1423)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:117)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1422)
at org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant.build(MojoExecutionBuildParticipant.java:55)
at org.eclipse.m2e.core.internal.builder.MavenBuilderImpl.build(MavenBuilderImpl.java:135)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:169)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:1)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.lambda$1(MavenBuilder.java:114)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:117)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.lambda$0(MavenBuilder.java:105)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:156)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:103)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:88)
at org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:198)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:860)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:232)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:281)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:334)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:337)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:392)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:417)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:161)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:255)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.has(String)" because "schemaNode" is null
at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:66)
at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:76)
at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:38)
at org.jsonschema2pojo.SchemaMapper.generate(SchemaMapper.java:92)
at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:130)
at org.jsonschema2pojo.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:77)
at org.jsonschema2pojo.maven.Jsonschema2PojoMojo.execute(Jsonschema2PojoMojo.java:882)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
... 30 more
I've never seen that error. Did you add a sourceType to the configuration in the pom too? I forgot you are also using JSON.
Yes, I added it, but now I am getting below error.
Execution generateClassesFromJSON of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: Could not process JSON in source file (org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate:generateClassesFromJSON:generate-sources)
org.apache.maven.plugin.PluginExecutionException: Execution generateClassesFromJSON of goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.1.1:generate failed: Could not process JSON in source file
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:148)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:336)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.lambda$8(MavenImpl.java:1423)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:117)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1422)
at org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant.build(MojoExecutionBuildParticipant.java:55)
at org.eclipse.m2e.core.internal.builder.MavenBuilderImpl.build(MavenBuilderImpl.java:135)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:169)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:1)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.lambda$1(MavenBuilder.java:114)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:117)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.lambda$0(MavenBuilder.java:105)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:182)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:156)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:103)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:88)
at org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:198)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:860)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:232)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:281)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:334)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:337)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:392)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:417)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:161)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:255)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.jsonschema2pojo.exception.GenerationException: Could not process JSON in source file
at org.jsonschema2pojo.SchemaGenerator.schemaFromExample(SchemaGenerator.java:59)
at org.jsonschema2pojo.SchemaMapper.readSchema(SchemaMapper.java:106)
at org.jsonschema2pojo.SchemaMapper.generate(SchemaMapper.java:90)
at org.jsonschema2pojo.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:79)
at org.jsonschema2pojo.maven.Jsonschema2PojoMojo.execute(Jsonschema2PojoMojo.java:882)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
... 30 more
Caused by: java.io.FileNotFoundException: C:\Users\rshah5\Downloads\BeanIO-service\target\classes\input (The system cannot find the file specified)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
at com.fasterxml.jackson.core.JsonFactory._optimizedStreamFromURL(JsonFactory.java:1589)
at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:793)
at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2622)
at org.jsonschema2pojo.SchemaGenerator.schemaFromExample(SchemaGenerator.java:56)
... 35 more
Below is the updated entry in the POM file:
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>generateClassesFromJSON</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/target/classes/input</sourceDirectory>
<targetPackage>com.aegbt.amos.beanio.generated.classes</targetPackage>
<includeConstructors>true</includeConstructors>
<includeToString>false</includeToString>
<annotationStyle>jackson2</annotationStyle>
<sourceType>json</sourceType>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-core</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</plugin>
Like like the source directory you specified does not exist.
It does exist and the json file is there but for whatever reason I am getting “Could not process json in source file”.
Caused by: java.io.FileNotFoundException: C:\Users\rshah5\Downloads\BeanIO-service\target\classes\input (The system cannot find the file specified)
Do I need to put the file name at the end?
Looks like it's misconfiguration.
You've configured jsonschema2pojo plugin:
<phase>generate-sources</phase>
${basedir}/target/classes/input
as <sourceDirectory>
Note that ${basedir}/target
creation happens later than plugin execution - hence the error.
It'd be best to keep sources to process in directory that is not generated ie. src/main/resources/input
or smht. similar
Closing issue as there's no feedback. Please feel free to reopen if you still have questions.
Hello all,
I am trying to read my JSON file and generate POJO classes from it with constructors. On the web they have an option to get constructors, but I am running the code using Maven plugin and there I do not see an option to enable constructors. I also researched online and found that there is a parameter that can be used to generate constructors, but it seems to only work with JSON Schema. Here's the link that I found: Include constructor. My project is a Spring Boot project, so I defined the parameters inside Maven build, but that did not work.
Any help is appreciated. Thanks!