How do I deploy an application/deployment via the HTTP-JSON API in JBoss EAP 6?

Solution Unverified - Updated -

Environment

  • JBoss Enterprise Application Platform (EAP)
    • 6.x

Issue

  • I know I can deploy an application via the JBoss CLI, but I want to be able to do the same thing via the HTTP-JSON API.

Resolution

Use the echo-dmr command in the JBoss CLI to get the JSON request format, for example:

[standalone@localhost:9990 /] echo-dmr deploy test.war --unmanaged
{
    "operation" => "composite",
    "address" => [],
    "steps" => [
        {
            "operation" => "add",
            "address" => {"deployment" => "test.war"},
            "content" => [{
                "path" => "/Users/jboss/applications/servers/jboss-eap-7.0/test.war",
                "archive" => true
            }],
            "enabled" => true
        },
        {
            "operation" => "deploy",
            "address" => {"deployment" => "test.war"}
        }
    ]
}

Another alternative:

  • Use this class (DeployDmrToJson.java) to generate request JSON to deploy your file:
import org.jboss.dmr.ModelNode;
import java.net.URL;

public class DeployDmrToJson
{
  public static void main(String[] args) throws Exception
  {
    if(args.length < 1)
      throw new IllegalArgumentException("The first argument must be a URL");

    URL url = new URL(args[0]);
    String[] pathElements = url.getFile().split("/");
    String name = pathElements[pathElements.length-1];

    ModelNode deploy = getDeploy(url.toExternalForm(), name);
    ModelNode undeploy = getUndeploy(name);

    System.out.println("Deploy\n------------------------------\n");
    System.out.println("Formatted:\n" + deploy.toJSONString(false));
    System.out.println("Unformatted:\n" + deploy.toJSONString(true));
    System.out.println("\nUneploy\n------------------------------\n");
    System.out.println("Formatted:\n" + undeploy.toJSONString(false));
    System.out.println("Unformatted:\n" + undeploy.toJSONString(true));
  }

  public static ModelNode getUndeploy(String name)
  {
    ModelNode undeployRequest = new ModelNode();
    undeployRequest.get("operation").set("undeploy");
    undeployRequest.get("address", "deployment").set(name);

    ModelNode removeRequest = new ModelNode();
    removeRequest.get("operation").set("remove");
    removeRequest.get("address", "deployment").set(name);

    ModelNode composite = new ModelNode();
    composite.get("operation").set("composite");
    composite.get("address").setEmptyList();
    final ModelNode steps = composite.get("steps");
    steps.add(undeployRequest);
    steps.add(removeRequest);
    return composite;
  }

  public static ModelNode getDeploy(String url, String name)
  {
    ModelNode deployRequest = new ModelNode();
    deployRequest.get("operation").set("deploy");
    deployRequest.get("address", "deployment").set(name);

    ModelNode addRequest = new ModelNode();
    addRequest.get("operation").set("add");
    addRequest.get("address", "deployment").set(name);
    addRequest.get("content").get(0).get("url").set(url);

    ModelNode composite = new ModelNode();
    composite.get("operation").set("composite");
    composite.get("address").setEmptyList();
    final ModelNode steps = composite.get("steps");
    steps.add(addRequest);
    steps.add(deployRequest);
    return composite;
  }
}

Run the class like this:

java -cp .:$JBOSS_HOME/modules/org/jboss/dmr/main/jboss-dmr-1.1.1.Final-redhat-1.jar DeployDmrToJson \
file:///Users/klape/support/helloWorld.war/dist/helloWorld.war

For the example URL given, it will print output like this:

Deploy
------------------------------

Formatted:
{
    "operation" : "composite",
    "address" : [],
    "steps" : [
        {
            "operation" : "add",
            "address" : {"deployment" : "helloWorld.war"},
            "content" : [{"url" : "file:/Users/klape/support/helloWorld.war/dist/helloWorld.war"}]
        },
        {
            "operation" : "deploy",
            "address" : {"deployment" : "helloWorld.war"}
        }
    ]
}
Unformatted:
{"operation" : "composite", "address" : [], "steps" : [{"operation" : "add", "address" : {"deployment" : "helloWorld.war"}, "content" : [{"url" : "file:/Users/klape/support/helloWorld.war/dist/helloWorld.war"}]},{"operation" : "deploy", "address" : {"deployment" : "helloWorld.war"}}]}

Uneploy
------------------------------

Formatted:
{
    "operation" : "composite",
    "address" : [],
    "steps" : [
        {
            "operation" : "undeploy",
            "address" : {"deployment" : "helloWorld.war"}
        },
        {
            "operation" : "remove",
            "address" : {"deployment" : "helloWorld.war"}
        }
    ]
}
Unformatted:
{"operation" : "composite", "address" : [], "steps" : [{"operation" : "undeploy", "address" : {"deployment" : "helloWorld.war"}},{"operation" : "remove", "address" : {"deployment" : "helloWorld.war"}}]}
  • This is the JSON (unformatted and formatted) that can be used to deploy and undeploy applications. You can then use this to make a request to the HTTP management interface:
    curl -f --digest -u "<user>:<pass>" -H Content-Type:\ application/json -d '<json request>' "http://localhost:9990/management"

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments