FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

Annotations not working on single object but working fine on list<> of objects (on Glassfish4) #21

Closed ChristBE closed 11 years ago

ChristBE commented 11 years ago

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; }

================= 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 {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(length = 24)
@Size(max = 24)
@NotEmpty
@JsonProperty("first_name")
private String firstName;

@Column(length = 24)
@Size(max = 24)
@NotEmpty
@JsonProperty("last_name")
private String lastName;

@OneToMany(mappedBy = "customer")
@JsonIdentityReference(alwaysAsId = true)
@JsonProperty("order_ids")
private Set<CustomerOrder> orders = new TreeSet<>();

public Customer() {
}

public Customer(Long id) {
    super();
    this.id = id;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

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

public String getLastName() {
    return lastName;
}

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

public Set<CustomerOrder> getOrders() {
    return orders;
}

public void setOrders(Set<CustomerOrder> orders) {
    this.orders = orders;
}

}

cowtowncoder commented 11 years ago

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.

cowtowncoder commented 11 years ago

Moved to https://github.com/FasterXML/jackson-databind/issues/309