Skip to content

[Management] Sending Raw Json

Rujun Chen edited this page Aug 20, 2021 · 1 revision

Be cautious whenever you are about to pass a JSON string as a parameter into a method that accepts that string as a Java Object. Azure SDK expects a parsed JSON object to be passed in instead.

For example, the following method

// In DeploymentsOperations.java
ServiceResponse<DeploymentExtended> createOrUpdate(
        String resourceGroupName,
        String deploymentName,
        Deployment parameters)
    throws CloudException, IOException, IllegalArgumentException, InterruptedException;

asks for a Deployment object, which contains DeploymentProperties. The definition of DeploymentProperties is:

public class DeploymentProperties {
    /**
     * Gets or sets the template content. Use only one of Template or
     * TemplateLink.
     */
    private Object template;

    /**
     * Gets or sets the URI referencing the template. Use only one of Template
     * or TemplateLink.
     */
    private TemplateLink templateLink;

    // etc
}

If you do

DeploymentProperties properties = new DeploymentProperties();
properties.setTemplate(readTemplateJson());
Deployment deployment = new Deployment();
deployment.setProperties(properties);

client.getDeploymentsOperations().createOrUpdate(rgName, deploymentName, deployment);

Azure SDK for Java will send a JSON string, which is wrapped in escaped quotes. The server will not be able to handle it.

The correct way of creating a deployment:

DeploymentProperties properties = new DeploymentProperties()
properties.setTemplate(client.getMapperAdapter().getObjectMapper().readTree(readTemplateJson()));
Deployment deployment = new Deployment();
deployment.setProperties(properties);

client.getDeploymentsOperations().createOrUpdate(rgName, deploymentName, deployment);
Clone this wiki locally