简体   繁体   中英

Convert AWS APIGateway's Model to JSON in Java

I am using Amazon's APIGateway service client side. When you make a request the data returned is stored in a Model data type that the schema is set up beforehand. the calls look like this:

MyModel myModel = client.settingsPost();
String volume = myModel.getVolume();

the schema for this simple object would look like this:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "MyModel",
  "type": "object",
  "properties" : {
      "volume" : { "type" : "string" }
  }
}

I would like to convert the Model returned directly to JSON instead of having to go manually reconstruct a new JSONObject from each value of this Model. The Models seem to be very simple and I cant even iterate through them. But I wonder if there is a way to convert them using the GSON library somehow?

EDIT: I am using the APIGateway SDK generated to Java.

Using Jackson:

ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(myModel);

Using Gson:

Gson gson = new Gson();
String json = gson.toJson(myModel);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM