When I run a Json test program with jdk 7, it says :
Exception in thread "main" javax.json.JsonException: Provider org.glassfish.json.JsonProviderImpl not found
at javax.json.spi.JsonProvider.provider(JsonProvider.java:97)
at javax.json.Json.createArrayBuilder(Json.java:257)"
I've included "javax.json-api-1.0.jar" in my project.
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class JsonExample
{
public static void main(String[] args)
{
List<User> users=getUsers();
// build JSON
JsonArrayBuilder jsonArrayBuilder=Json.createArrayBuilder();
for (User user : users)
{
jsonArrayBuilder.add(
Json.createObjectBuilder()
.add("id",user.getId())
.add("name",user.getName())
.add("join",user.getJoin().getTime()));
}
JsonArray usersJson=jsonArrayBuilder.build();
System.out.println(usersJson.toString());
}
// dummy users
public static List<User> getUsers()
{
List<User> users=new ArrayList<User>(10);
User user=new User();
user.setId(1);
user.setName("Hero");
user.setJoin(new Date());
users.add(user);
user=new User();
user.setId(2);
user.setName("Citizen");
user.setJoin(new Date());
users.add(user);
return users;
}
// POJO
public static class User implements Serializable
{
private long id;
private String name;
private Date join;
public long getId()
{
return id;
}
public void setId(long id)
{
this.id=id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public Date getJoin()
{
return join;
}
public void setJoin(Date join)
{
this.join=join;
}
}
}
It's a simple program, not using galssfish, why is glassfish mentioned here ?
javax.json-api-1.0.jar
contains only API which is there for compile time dependency. But if you want to run your app, you need a provider / impl
classes.
org.glassfish:javax.json
is the jar you need that contains both api
+ impl
classes.
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>