jackson-annotations-2.2.3.jar
=============== WORKING =====================
c:>curl -H "Accept:application/json" http://localhost:8080/emberGF4/rest/customers
{"customers":[{"id":1,"first_name":"F1","last_name":"L1","order_ids":[1]},{"id":2,"first_name":"F2","last_name":"L2","order_ids":[]}]}
produced by:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Customers get(@QueryParam("ids[]") List ids) {
if (ids != null && !ids.isEmpty()) {
return new Customers(manager.findByIds(ids));
}
return new Customers(manager.findAll());
}
@JsonRootName("customers")
class Customers extends ArrayList<Customer> {
private static final long serialVersionUID = 1L;
public Customers(Collection<? extends Customer> c) {
addAll(c);
}
}
=============== NOT WORKING =====================
c:>curl -H "Accept:application/json" http://localhost:8080/emberGF4/rest/customers/2
{"firstName":"F2","id":2,"lastName":"L2","orders":[]}
On single object the @JsonRootName("customer") is missing and @JsonIdentityReference(alwaysAsId = true) @JsonProperty("order_ids")
produced by:
@Path("/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Customer get(@PathParam("id") Long id) {
Customer c = manager.find(id);
return c;
}
Could you re-file at jackson-databind -- annotations package just contains annotations, databind actually uses them. So bugs here would be only for direct additions to annotation types.
jackson-annotations-2.2.3.jar =============== WORKING ===================== c:>curl -H "Accept:application/json" http://localhost:8080/emberGF4/rest/customers {"customers":[{"id":1,"first_name":"F1","last_name":"L1","order_ids":[1]},{"id":2,"first_name":"F2","last_name":"L2","order_ids":[]}]}
produced by: @GET @Produces(MediaType.APPLICATION_JSON) public Customers get(@QueryParam("ids[]") List ids) {
if (ids != null && !ids.isEmpty()) {
return new Customers(manager.findByIds(ids));
}
return new Customers(manager.findAll());
}
=============== NOT WORKING ===================== c:>curl -H "Accept:application/json" http://localhost:8080/emberGF4/rest/customers/2 {"firstName":"F2","id":2,"lastName":"L2","orders":[]}
On single object the @JsonRootName("customer") is missing and @JsonIdentityReference(alwaysAsId = true) @JsonProperty("order_ids")
produced by: @Path("/{id}") @GET @Produces(MediaType.APPLICATION_JSON) public Customer get(@PathParam("id") Long id) { Customer c = manager.find(id); return c; }
================= Customer ========================= package ember.sample.entity;
import java.util.Set; import java.util.TreeSet;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.annotation.ObjectIdGenerators;
@Entity @Table @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @JsonRootName("customer") public class Customer {
}