I have the following json i want to deserialize to a Java Object :
{
"firstname": "John",
"lastname": "Do",
"sex": "",
}
In the other part i have a Java Rest Webservice :
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response findPerson(@Valid PersonneRequest personne) {
if(personne!= null){
Personne result = null;
// do some stuff
return Response.ok(result).build();
}
// Bad request
return Response.status(Response.Status.BAD_REQUEST).build();
}
Here is the Personne class :
public class PersonneRequest implements Serializable{
private String firstname;
private String lastname;
private Sex sex;
// getters and setters
}
Here is my Sexe class :
public enum Sex{
MAN("man"),
WOMAN("woman");
private String type;
private Sex(String type){
this.type = type;
}
public String getType(){
if(type!=null && !StringUtils.isEmpty(type)){
return type;
}
return MAN.getType();
}
}
My problem is when i make a post call on my webervice. Json data are not deserialize when sex is empty. I am getting the following exception. How to set a value for empty value of enum ? Here is my java exception :
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of Sex from String value '': value not one of declared Enum instance names
at [Source: org.glassfish.jersey.message.internal.EntityInputStream@5ed82ec0; org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.StdDeserializationContext.weirdStringException(StdDeserializationContext.java:243) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:74) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:23) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) ~[jackson-jaxrs-1.9.13.jar:1.9.13]
Thanks