Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename JSON fields used by MappingJacksonJsonView in Spring

I'm using MappingJacksonJsonView to serialize to JSON a class, however, I'd like to be able to rename some of the fields from the default name based on the getter name.

This is because I've to output field names like "delete_url" and "delete_type" for jQuery file upload. I'm using @Jsonserialize annotation to hand pick the fields to serialize.

@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {

    @JsonSerialize
    String getName();

    @JsonSerialize
    String getDelete_url();

    ...

For instance, I'm forced to call a method getDelete_url(), while I'd like to call it getDeleteUrl(), but still output the key "delete_url" when serializing to JSON.

like image 642
stivlo Avatar asked Oct 25 '11 17:10

stivlo


1 Answers

You should be able to qualify using @JsonProperty.

@JsonAutoDetect(getterVisibility = Visibility.NONE)
public interface Picture {

  @JsonSerialize
  @JsonProperty("name")
  String getName();

  @JsonSerialize
  @JsonProperty("delete_url")
  String getDeleteUrl();

  //...
like image 171
nicholas.hauschild Avatar answered Oct 12 '22 14:10

nicholas.hauschild