1

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

Pracede
  • 4,226
  • 16
  • 65
  • 110
  • Make sure that you client sends `null` rather than the empty string when it doesn’t know a vale for the “sex” attribute. – Erwin Bolwidt Dec 27 '17 at 00:53

2 Answers2

0

Use @JsonInclude(JsonInclude.Include.NON_EMPTY) with property Sex. It will handle empty string value.

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Sex sex;

I hope this may help you.

Nishesh Pratap Singh
  • 2,131
  • 2
  • 13
  • 20
0

I thinks that there are two problem in your code: Firstly, your getType() method in class enum Sex is wrong. It is return MAN.getType() that mean call itself so that cause infinitive loop. May be you want to catch the case that null is assigned for enum but its not the right way.

Secondly, the reason that directly related to your problem in log file is can not assign null or empty value for enum type. I think use @JsonIgnoreProperties and DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL is the best way to solve your problem Refer the post here: How to ignore enum fields in Jackson JSON-to-Object mapping?

hoapham
  • 180
  • 5
  • 10