jackson-annotations provides @JsonFormat
which can handle a lot of customizations without the need to write the custom serializer.
For example, requesting a STRING
shape for a field with numeric type will output the numeric value as string
public class Person {
public String name;
public int age;
@JsonFormat(shape = JsonFormat.Shape.STRING)
public int favoriteNumber;
}
will result in the desired output
{"name":"Joe","age":25,"favoriteNumber":"123"}