3

I am investigating JSON processing from JavaEE 7 and i have a question described below.

(before asking i have read below info but still have a question)

http://docs.oracle.com/javaee/7/tutorial/jsonp004.htm

How can I cast a JSONObject to a custom Java class?

How do I convert a JSONObject to class object?

1) I have a REST web service which returns response in a JSON format:

{"id":1141,"email":"user@organisation.com","enabled":"Y"}

2) There is a corresponding JPA Entity called User

@Table(name = "USER")
@Entity
public class User {

@Id
@Column(name = "USER_ID")
private Long id;

@Column(name = "EMAIL")
private String email;

@Column(name = "ENABLED")
private String enabled;

3) I have a client based on Jersey Client API and Java EE JSON Processing which call this web service.

Maven dependencies:

<dependency> 
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.22.1</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.json</artifactId>
    <version>1.0.4</version>
</dependency>

Client code:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:7001/projectname/rest");
WebTarget resourceWebTarget = target.path("users").queryParam("email", "user@organisation.com");
Invocation.Builder invocationBuilder = resourceWebTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.get();

JsonReader reader = Json.createReader(response.readEntity(InputStream.class));           
JsonObject jObject = reader.readObject();

User user = new User();
user.setId(jObject.getJsonNumber("id").longValue());
user.setEmail(jObject.getString("email"));
user.setEnabled(jObject.getString("enabled"));

And finally the question:

Should i have create user like User = new User(); and set all the properties manually or exists more convenient way to create User ?

Community
  • 1
  • 1
Alex A
  • 138
  • 1
  • 8

3 Answers3

0

I think what you're looking for is a constructor method. In your case for the user you have three separate fields which can be populated as soon as you instantiate your User object.

In order to do this add this method (constructor) to your User class:

public User(Long id, String email, String enabled){
   this.id = id;
   this.email = email;
   this.enabled = enabled;
}

When creating an instance of the class you will use the following line substituting my hard values for variables of course:

User user = new User(1141, "user@organisation.com", "Y");

For JSON parsing directly into or out of POJOs you might want to take a look into using a library such as Google Gson or Jackson2.

I hope this answers your question!

  • Thanks for the answer! It's not a problem to add a constructor. But the idea "is it possible in JavaEE 7 processing parsing directly to the POJO" (i.e. not create new POJO manually) I know about Google Gson or Jackson2 but i am interesting in JavaEE 7 JSON processing. – Alex A Sep 23 '16 at 17:54
0

At the moment there is no possibility to make direct mapping using simple javax.json-api If you are using Jersey Client API it's better to use Jackson mapper.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.23.2</version>
    <scope>provided</scope>
</dependency>

You would be able to use such simple construction:

String jsonString = "{'id':1141,'email':'user@organisation.com','enabled':'Y'}";
User user = mapper.readValue(jsonString, User.class);
Tionio
  • 79
  • 3
  • Thanks for the answer! Finally I have used Jackson2 realization `ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(response.readEntity(InputStream.class), User.class); ` – Alex A Sep 26 '16 at 14:35
0

With the release of JSON Binding in Java EE 8 you can now bind JSON to Java objects.

Here's a simple example:

String userJson = "{\"id\":1141,
                    \"email\":\"user@organisation.com\",
                    \"enabled\":\"Y\"}";
User user = JsonbBuilder.create().fromJson(userJson, User.class);

There is no longer the need to depend on third-party APIs. The JSON Processing and JSON Binding work together and are integrated into the JAX-RS API.

So you can adapt your code example above to the following:

User user = JsonbBuilder.create()
              .fromJson(response.readEntity(InputStream.class), User.class);

The JSON-B deserialization process can be customized in a variety of very handy ways with simple annotations or runtime configurations as well as advance lower customization options.

Alex Theedom
  • 1,604
  • 15
  • 16