spring custom response @JsonSerialize

POJO for response

JsonPropertyOrder({ "name", ... })
public class Student {
    @JsonSerialize(using = EmptyStringSerializer.class)
    @JsonProperty("name")
    private String name;
   
    ...
}

Custom Serializer Class , You can modify this class

public class EmptyStringSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        /*if (s == null) {
            // don't write this node
            return;
        }*/

       
         jsonGenerator.writeString(s);
       
    }
}

Related posts:

This entry was posted in spring boot. Bookmark the permalink.