如何编写与Azure REST API返回JSON对应的Java POJO/Entity类
Got it, let's build the POJO classes that map perfectly to your Azure REST API JSON response. We'll use Jackson (the go-to library for JSON processing in most Java apps, especially Spring-based ones) to handle serialization/deserialization. Here's how to do it step by step:
1. Add Jackson Dependency (if you haven't already)
If you're using Maven, add this to your pom.xml:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> <!-- Use the latest stable version --> </dependency>
2. Define the POJO Classes
We'll break down the JSON structure into nested classes matching each object:
a. Top-Level Response Class
This wraps the "value" array of storage accounts:
import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; public class StorageAccountsResponse { @JsonProperty("value") private List<StorageAccount> storageAccounts; // Getters and Setters public List<StorageAccount> getStorageAccounts() { return storageAccounts; } public void setStorageAccounts(List<StorageAccount> storageAccounts) { this.storageAccounts = storageAccounts; } }
b. StorageAccount Class
Represents each item in the "value" array:
import com.fasterxml.jackson.annotation.JsonProperty; import java.util.Map; public class StorageAccount { @JsonProperty("sku") private Sku sku; @JsonProperty("kind") private String kind; @JsonProperty("id") private String id; @JsonProperty("name") private String name; @JsonProperty("type") private String type; @JsonProperty("location") private String location; @JsonProperty("tags") private Map<String, String> tags; @JsonProperty("properties") private StorageAccountProperties properties; // Getters and Setters for all fields public Sku getSku() { return sku; } public void setSku(Sku sku) { this.sku = sku; } public String getKind() { return kind; } public void setKind(String kind) { this.kind = kind; } // ... Add remaining getters and setters (your IDE can auto-generate these!) }
c. Sku Class
Maps the "sku" object in each storage account:
import com.fasterxml.jackson.annotation.JsonProperty; public class Sku { @JsonProperty("name") private String name; @JsonProperty("tier") private String tier; // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTier() { return tier; } public void setTier(String tier) { this.tier = tier; } }
d. StorageAccountProperties Class
Handles the "properties" object, including the networkAcls (we'll define a placeholder class for it since you have ... in your JSON):
import com.fasterxml.jackson.annotation.JsonProperty; public class StorageAccountProperties { @JsonProperty("networkAcls") private NetworkAcls networkAcls; // Getters and Setters public NetworkAcls getNetworkAcls() { return networkAcls; } public void setNetworkAcls(NetworkAcls networkAcls) { this.networkAcls = networkAcls; } }
e. NetworkAcls Class (Placeholder)
Expand this with the actual fields from your full networkAcls JSON once you have it:
// Add fields that match your actual networkAcls structure here public class NetworkAcls { // Example fields (adjust based on your full JSON): // private String defaultAction; // private List<IpRule> ipRules; // Getters and Setters for your specific fields }
3. Deserialize the JSON Response
Once you have the POJOs, use Jackson's ObjectMapper to convert the API response string into your Java objects:
import com.fasterxml.jackson.databind.ObjectMapper; public class AzureApiClient { public static void main(String[] args) throws Exception { String jsonResponse = "{ \"value\": [ { \"sku\": { \"name\": \"Standard_LRS\", \"tier\": \"Standard\" }, \"kind\": \"Storage\", \"id\": \"/subscriptions/cc60365e-3193-xxxx-b0f6-xxxxxxx/resourceGroups/WebServer-ubuntu/providers/Microsoft.Storage/storageAccounts/webserverubuntudiagxxx\", \"name\": \"webserverubuntudiag4xx\", \"type\": \"Microsoft.Storage/storageAccounts\", \"location\": \"eastus\", \"tags\": {}, \"properties\": { \"networkAcls\": {} } } ] }"; ObjectMapper objectMapper = new ObjectMapper(); StorageAccountsResponse response = objectMapper.readValue(jsonResponse, StorageAccountsResponse.class); // Access the first storage account StorageAccount firstAccount = response.getStorageAccounts().get(0); System.out.println("Storage Account Name: " + firstAccount.getName()); System.out.println("SKU Name: " + firstAccount.getSku().getName()); } }
Quick Notes
- If you prefer Gson over Jackson, replace
@JsonPropertywith@SerializedNamefrom the Gson library. - Don't forget to generate all missing getters/setters—most IDEs (IntelliJ, Eclipse) can do this in one click.
- Update the
NetworkAclsclass with real fields once you have the completenetworkAclsJSON structure from the API.
内容的提问来源于stack exchange,提问作者Deepak S

