introducing jackson
{data:[ "firstName":"Gula", "lastName":"Nurmatova", "height":5.6, "children": [ {"name":"Zain"}, {"name":"Hania"} ] "address":{ "street":"budd circle" "state":"CT" } ], "page":"1" } ;pre>
add jackson dependencies in pom.xml:
under properties:
<jackson.version>2.7.1</jackson.version>
under dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
under properties:
<jackson.version>2.7.1</jackson.version>
under dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
Run:
$ mvn clean package
then:
$ mvn eclipse:eclipse
$ mvn clean package
then:
$ mvn eclipse:eclipse
- import org.codehaus.jackson.map.ObjectMapper; to do bindings
- create one mapper instance to do all your bindings (you can re-use this object)
- You will need to write your object as string
package test; import java.util.HashMap; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { ObjectMapper mapper = new ObjectMapper(); @GET @Produces(MediaType.TEXT_PLAIN) @Path("person") public Response getIt() { try { HashMap<String, String> hm = new HashMap<String, String>(); hm.put("firstName", "Gula"); hm.put("lastName", "Nurmatova"); hm.put("location", "CT"); return Response.status(Response.Status.OK) .entity(mapper.writeValueAsString(hm)).build(); } catch (Exception e) { return Response.status(Response.Status.BAD_REQUEST) .entity("Could not process your request").build(); } } }
Access your application at http://localhost:8080/unh/rest/sandbox/person
You should now see JSON returned in your browser that looks like:
You should now see JSON returned in your browser that looks like:
{ "lastName": "Nurmatova", "location": "CT", "firstName": "Gula" }
Serializing custom objects
- You need Getters and Setters for the Jackson serializer to work or make your fields public
package test; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { ObjectMapper mapper = new ObjectMapper(); @GET @Produces(MediaType.APPLICATION_JSON) @Path("person") public Response getIt() { try { Person gula = new Person("Gula", "Nurmatova", "CT"); String strGula = mapper.writeValueAsString(gula); return Response.status(Response.Status.OK) .entity(strGula).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.BAD_REQUEST) .entity("Could not process your request").build(); } } } class Person { public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } String lastName; String firstName; String location; public Person(String firstName, String lastName, String location) { this.firstName = firstName; this.lastName = lastName; this.location = location; } }
The above example serializes the object as String, we can also serialize it as file:
Person gula = new Person("Gula", "Nurmatova", "CT");
File jsonGula = new File("result.json");
mapper.writeValue(jsonGula, gula);
return Response.status(Response.Status.OK).entity(jsonGula).build();
or as Byte array:
Person gula = new Person("Gula", "Nurmatova", "CT");
byte[] jsonBytes = mapper.writeValueAsBytes(gula);
return Response.status(Response.Status.OK).entity(jsonBytes).build();
Nested Objects and JSON Lists
Person gula = new Person("Gula", "Nurmatova", "CT");
File jsonGula = new File("result.json");
mapper.writeValue(jsonGula, gula);
return Response.status(Response.Status.OK).entity(jsonGula).build();
or as Byte array:
Person gula = new Person("Gula", "Nurmatova", "CT");
byte[] jsonBytes = mapper.writeValueAsBytes(gula);
return Response.status(Response.Status.OK).entity(jsonBytes).build();
Nested Objects and JSON Lists
package test; import java.util.ArrayList; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.fasterxml.jackson.databind.ObjectMapper; @Path("sandbox") public class Sandbox { ObjectMapper mapper = new ObjectMapper(); @GET @Produces(MediaType.APPLICATION_JSON) @Path("person") public Response getIt() { try { ArrayList<String> locations = new ArrayList<String> (); locations.add("CT"); locations.add("CA"); Person gula = new Person("Gula", "Nurmatova", locations); String strGula = mapper.writeValueAsString(gula); return Response.status(Response.Status.OK) .entity(strGula).build(); } catch (Exception e) { e.printStackTrace(); return Response.status(Response.Status.BAD_REQUEST) .entity("Could not process your request").build(); } } } class Person { public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public ArrayListgetLocation() { return locations; } public void setLocation(ArrayList<String> location) { this.locations = location; } String lastName; String firstName; ArrayList<String> locations; public Person(String firstName, String lastName, ArrayList<String> location) { this.firstName = firstName; this.lastName = lastName; this.locations = location; } }
The response now is:
{ "lastName": "Nurmatova", "firstName": "Gula", "location": [ "CT", "CA" ] }