0

In the latest Jackson 2.14 you can't use WRITE_NUMBERS_AS_STRINGS which is deprecated:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true); // DEPRECATED!

Also you can't use @JsonSerialize(using = ToStringSerializer.class):

enter image description here

These were the suggestions offered in this thread. So how do we now cast fields to Strings in the latest Jackson? Some examples would be appreciated, because I couldn't find any.

gene b.
  • 10,512
  • 21
  • 115
  • 227

2 Answers2

1

I found the new convention. It's now called JsonWriteFeature.WRITE_NUMBERS_AS_STRING) and also .configure() needs to be put after JsonMapper.builder().

    ObjectMapper mapper = JsonMapper.builder()
                                    .configure(JsonWriteFeature.WRITE_NUMBERS_AS_STRINGS, true) 
                                    .build();
gene b.
  • 10,512
  • 21
  • 115
  • 227
0

You can try setting a method as a setter for your value. This method then converts your integer to a string.

public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();

    String json = "{\"number\": 1}";
    Model model = mapper.readValue(json, Model.class);
    System.out.println(model.getNumber());
}


public class Model {
  private String number;

  public void setNumber(@JsonProperty("number") Integer number) {
    this.number = Integer.toString(number);
  }

  public String getNumber() {
    return number;
  }
}