I am trying to teach myself how to work with JSON but I am having trouble building a JSON object in Java. I am importing javax.json and trying to create something like this:
JsonObject model = Json.createObjectBuilder()
.add("firstName", "Duke")
.add("lastName", "Java")
.add("age", 18)
.build();
I get this error:
Exception in thread "main" java.lang.AbstractMethodError: javax.json.spi.JsonProvider.createObjectBuilder()Ljavax/json/JsonObjectBuilder;
at javax.json.Json.createObjectBuilder(Json.java:266)
at com.example.jsontest.Test.main(Test.java:15)
Any ideas?
You're attempting to use the new javax.json API without a JSON provider. To my knowledge (and some googling) the only one at the moment is the reference implementation from glassfish.
In your pom.xml you would need both dependencies:
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With