0

I'm writing restful API with Spring. The API looks like below which lists all objects of its type.

http://192.168.1.100:8021/some/api/v3/someobjs

The DTO looks like below

public class SomeDTO {

  @NotBlank
  @Size(min = 1, max = 32, message = "Name size must be between 1 and 32.")
  private String name;

  @NotBlank
  @Size(min = 1, max = 4000, message = "Info size must be between 1 and 4000.")
  private String info;

  // Setter & Getter
}

The "info" value could be a JSON format string. So, what I got could be like this:

{
  "name": "wpdfw",
  "info": "{\n    \"indexName\": \"wpdfw\", \n    \"urls\": [\n        \"https://www.example.com/l2/api/v1\", \n        \"https://www.example.com/l3/api/v1\"\n    ], \n    \"regions\": [\n        \"wp.*.*\", \n        \"wf.*.*\"\n    ], \n    \"policy\": \"equal\"\n}"
}

However, I want the "info" to be in real JSON format instead of a string:

{
  "name": "wpdfw",
  "info": {
    "indexName": "wpdfw",
    "urls": ["https://www.example.com/l2/api/v1", "https://www.example.com/l3/api/v2"],
    "regions": ["wp.*.*", "wf.*.*"],
    "policy": "equal"
  }
}

Please note that the JSON format "info" value could be of any JSON hierarchical structure which is unknown. How can I do it?

yasi
  • 451
  • 7
  • 18

1 Answers1

1

Your question is similar to this one.

Check the @JsonRawValue which has @Target(value={ANNOTATION_TYPE,METHOD,FIELD}).

@JsonRawValue public String getInfo() { return info; }

Spring Rest uses Jackson library for serializing to JSON.

Depending on the library you have on your classpath you can have Jackson v1 or v2.

Also check this link that can help you with additional examples of using Jackson annotations.

Community
  • 1
  • 1
andreim
  • 3,365
  • 23
  • 21