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.
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.
You have implemented a resource that can unmarshal and marshal JSON data to and from POJOs with Jackson.
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; } }
[{"firstName":"John","lastName":"Doe"},{"firstName":"Fred","lastName":"Thompson"}]
{"firstName":"John","lastName":"Doe"} JSON content, like the following code snippet, is returned in the response. {"greeting":"Hello John Doe"}
In this information ...Related reference
| IBM Redbooks, demos, education, and more(Index) |