Network Deployment (Distributed operating systems), v8.0 > End-to-end paths > Web services - RESTful services > Use JSON content in JAX-RS application requests and responses > 4. Implement a resource method using JSON content for a request and response entity.


Use Jackson with plain old Java objects (POJOs)

RESTful services can consume and produce content with the JavaScript Object Notation (JSON) format with the Jackson library.

POJO types (including array types and java.util.Collection types) are Jackson-supported entity types. The Jackson library is included in the runtime environment of this product. You do not need to bundle any additional libraries.

The following Jackson annotations are supported and can be used to annotation POJOs:

org.codehaus.jackson.annotate.JsonAnySetter
org.codehaus.jackson.annotate.JsonAutoDetect
org.codehaus.jackson.annotate.JsonClass
org.codehaus.jackson.annotate.JsonContentClass
org.codehaus.jackson.annotate.JsonCreator
org.codehaus.jackson.annotate.JsonGetter
org.codehaus.jackson.annotate.JsonIgnore
org.codehaus.jackson.annotate.JsonIgnoreProperties
org.codehaus.jackson.annotate.JsonKeyClass
org.codehaus.jackson.annotate.JsonProperty
org.codehaus.jackson.annotate.JsonPropertyOrder
org.codehaus.jackson.annotate.JsonSetter
org.codehaus.jackson.annotate.JsonSubTypes
org.codehaus.jackson.annotate.JsonSubTypes.Type
org.codehaus.jackson.annotate.JsonTypeInfo
org.codehaus.jackson.annotate.JsonTypeName
org.codehaus.jackson.annotate.JsonValue
org.codehaus.jackson.annotate.JsonWriteNullProperties
org.codehaus.jackson.map.annotate.JsonCachable
org.codehaus.jackson.map.annotate.JsonDeserialize
org.codehaus.jackson.map.annotate.JsonSerialize
org.codehaus.jackson.map.annotate.JsonTypeIdResolver
org.codehaus.jackson.map.annotate.JsonTypeResolver
org.codehaus.jackson.map.annotate.JsonView

These annotations can be used for more fine grained control over how POJOs are converted to and from JSON.


Procedure

  1. Use a POJO as a parameter or return type for your resource method.

  2. Add a javax.ws.rs.core.Produces or javax.ws.rs.core.Consumes annotation with the application or JSON media type to your resource method.


Results

You have implemented a resource that can unmarshal and marshal JSON data to and from POJOs with Jackson.


Example

The following example illustrates a resource used to return a collection of people and to process a greeting for a person.

public class Person {

    private String firstName;
    private String lastName;

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}
public class Greeting {

    private String greeting;

    public String getGreeting() {
        return this.greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}
import java.util.List;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/people")
public class JacksonResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List
<Person> getPersonList() {
        List
<Person> personArray = new ArrayList
<Person>();
 
        Person firstPerson = new Person();
        firstPerson.setFirstName("John");
        firstPerson.setLastName(“Doe”);
        personArray.add(firstPerson);
 
        Person secondPerson = new Person();
        secondPerson.setFirstName(“Fred”);
        secondPerson.setLastName("Thompson");
        personArray.add(secondPerson);
 
        return personArray;
    }

    @Path("/greet")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Greeting createGreetingForPerson(Person person) {
        String name = person.getFirstName() + “ “ + person.getLastName();
        Greeting greeting = new Greeting();
        greeting.setGreeting(“Hello “ + name);
        return greeting;
    }

}

When issuing a GET request to the JacksonResource resource, JSON content, like the following code snippet, is returned in the response.

[{"firstName":"John","lastName":"Doe"},{"firstName":"Fred","lastName":"Thompson"}]

When issuing a POST request to the JacksonResource, JSON content, like the following code snippet, is sent in the request and is stored in the Person object.

{"firstName":"John","lastName":"Doe"}
JSON content, like the following code snippet, is returned in the response.
{"greeting":"Hello John Doe"}

Use JSON content in JAX-RS application requests and responses


Related

+

Search Tips   |   Advanced Search