引入 Gson 到 pom.xml
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
示例代码
JsonObject rest_data = new JsonObject();
rest_data.addProperty("id", 123);
rest_data.addProperty("name", "shanghai");
Gson gson = new Gson();
String payload = gson.toJson(rest_data);