I have this weird problem that when the server receives a JSON via REST, Jackson tries to convert a String into an Integer:
BlockquoSchwerwiegend: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of int from String value 'before': not a valid Integer value at [Source: org.apache.catalina.connector.CoyoteInputStream@3916f0; line: 1, column: 182] (through reference chain: com.entities.SectionRelation["listLinkLabel"]->java.util.HashSet[0]->com.entities.LinkLabel["linkLabel"]) at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55) at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:883) at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseInteger(StdDeserializer.java:411) at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:289) at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:271) at com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeSetAndReturn(ObjectIdValueProperty.java:85) at com.fasterxml.jackson.databind.deser.impl.ObjectIdValueProperty.deserializeAndSet(ObjectIdValueProperty.java:77) at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:335) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithObjectId(BeanDeserializerBase.java:1045) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:240) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:212) at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523) ...
This is the entity where the error is supposed to be:
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
/**
* The label name is unique and is therefore the
* primary key.
*/
@Entity
@JsonIdentityInfo(
generator = ObjectIdGenerators.IntSequenceGenerator.class,
property = "linkLabel")
public class LinkLabel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@JsonFormat(shape = JsonFormat.Shape.STRING)
@Column(name = "LinkLabel")
@GeneratedValue(strategy = GenerationType.AUTO)
private String linkLabel;
@JsonIgnore
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(
name="LINKLABEL_LINKSET",
joinColumns={@JoinColumn(name="LINKLABEL_ID", referencedColumnName="LinkLabel")},
inverseJoinColumns={@JoinColumn(name="LINK_LABEL_SET_ID", referencedColumnName="id")})
private Set<LinkSet> linkSet = new HashSet();
public String getLinkLabel() {
return linkLabel;
}
public void setLinkLabel(String linkLabel) {
this.linkLabel = linkLabel;
}
public Set<LinkSet> getLinkSet() {
return linkSet;
}
public void addLinkSet(LinkSet linkSet) {
this.linkSet.add(linkSet);
}
}
This is an example JSON which was sent by the server:
{
"links": [{
"id": 2,
"section1": {
...
},
"section2": {
...
},
"listLinkLabel": [{
"linkLabel": 1,
"linkLabel": "after"
}]
}, {
"id": 5,
"section1": {
...
},
"section2": {
...
},
"listLinkLabel": [{
"linkLabel": 2,
"linkLabel": "before"
}, {
"linkLabel": 3,
"linkLabel": "overlap"
}, 1]
}, {
"id": 3,
"section1": {
...
},
"section2": {
...
},
"listLinkLabel": [3]
}
}
This is the responsible snippet of the frontend:
this.addLink = function(source, target) {
var JSONTemplate = {
"id":null,
"section1":{
...
},
"section2":{
...
},
"listLinkLabel":[{
// "linkLabel": 1
// ,
"linkLabel": "before"
}]
};
$http.post('service/sectionrelation', JSON.stringify(JSONTemplate));
}
I don't see why Jackson tries to convert "linkLabel" "before" to an Integer, when the type is definetely a String, even @JsonFormat doesn't change anything. Only ""linkLabel": 1" evokes no errors , but there must be a possibility to send only ""linkLabel": "before"". This seems pretty basic and simple to me, because this is the normal representation of the entity.
In the pom.xml Jackson is used 2.6.3 and GlassFish 4.1 is the application server.