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?

1
  • supoose this post may help you get started with Spring Data Rest. Commented Apr 14, 2017 at 5:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.