Roman
Roman

Reputation: 3149

AWS API Gateway - Integration Response body mapping

In AWS API Gateway Integration Response body mapping I have following code:

#set($inputRoot = $input.path('$.Item'))
[
#foreach($elem in $inputRoot.Events)
 {
  "id": $elem.id,
  "from" : $elem.from,
  "to" : $elem.to,
  "spent" : $elem.spent,
  #if("$!elem.comment" != "")
    "comment": $elem.comment,    
  #end
  "project" : {
    "id" : $elem.project.id,
    "number" : $elem.project.number,
    "name" : $elem.project.name
  }
  }
#if($foreach.hasNext),#end
#end
]

The data comes from a lambda functions which queries a DynamoDB Table

API gateway returns the data like this:

[
 {
  "id": 123443214,
  "from" : 19:34,
  "to" : 22:30,
  "spent" : 02:56,
    "project" : {
    "id" : 4321,
    "number" : CIB,
    "name" : Backend
  }
  }
, {
  "id": 12341234,
  "from" : 19:34,
  "to" : 22:30,
  "spent" : 02:56,
    "project" : {
    "id" : 12341234,
    "number" : CIB,
    "name" : Backend
  }
  }
]

So it it's already formatted. How do I get APi Gateway to return the response unformatted? So that it's just pure json, without break lines, indentations etc.?

Thanks in advance!

Upvotes: 0

Views: 1563

Answers (1)

Claude Brisson
Claude Brisson

Reputation: 4130

(Small preliminary remark: you are missing some quotes around JSON string values).

It's possible to remove line breaks using ## and indentation using #**#, as follow, but the template will look a bit ugly:

#set($inputRoot = $input.path('$.Item'))##
[##
#foreach($elem in $inputRoot.Events)##
{##
#**#"id":$elem.id,##
#**#"from": $elem.from,##
#**#"to":$elem.to,##
#**#"spent":$elem.spent,##
#if("$!elem.comment" != "")##
#*  *#"comment":$elem.comment,##
#end##
#**#"project":{##
#**#"id":$elem.project.id,##
#**#"number":"$elem.project.number",##
#**#"name":"$elem.project.name"##
}##
}##
#if($foreach.hasNext),#end##
#end##
]##

Since the only reason the indentation is in here at first is readability of the template, I would go another direction.

For instance, you can add a post-processing tidy formatter in your View servlet) using org.json:

import org.json.JSONObject;
  ....
Writer writer = new StringWriter();
getVelocityView().merge(template, context, writer);
String compactJSON = new JSONObject(writer.toString()).toString();
response.getWriter().write(compactJSON);

But this will only work for small JSON files since the response is buffered into memory, so let's keep on searching for a more elegant solution.

The way to go is to pre-process your template, using a custom ResouceLoader.

CompactJSONResourceLoader.java

package my.custom.loader;

import java.io.InputStream;
import java.io.IOException;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.loader.ResourceLoaderFactory;

public class CompactJSONResourceLoader extends ResourceLoader
{
    protected ResourceLoader innerLoader = null;

    @Override
    public void init(ExtendedProperties configuration)
    {
        try
        {
            String innerLoaderID = configuration.getString("innerLoader") + ".resource.loader";
            String innerLoaderClass = rsvc.getConfiguration().getString(innerLoaderID + ".class");
            innerLoader = ResourceLoaderFactory.getLoader(rsvc, innerLoaderClass);
            ExtendedProperties innerConfiguration = rsvc.getConfiguration().subset(innerLoaderID);
            innerLoader.commonInit(rsvc, innerConfiguration);
            innerLoader.init(innerConfiguration);
        }
        catch (Exception e)
        {
            log.error("could not initialize CompactJSONResourceLoader inner loader", e);
        }
    }

    protected class CompactJSONInputStream extends InputStream
    {
        InputStream innerStream = null;
        boolean insideQuotes = false;

        public CompactJSONInputStream(InputStream innerStream)
        {
            this.innerStream = innerStream;
        }

        @Override
        public int read() throws IOException
        {
            int ch;
            do
            {
                ch = innerStream.read();
                if (insideQuotes)
                {
                    if (ch == '"') insideQuotes = false;
                    break;
                }
                else if (!Character.isWhitespace(ch))
                {
                    if (ch == '"') insideQuotes = true;
                    break;
                }
            }
            while (ch != -1);
            return ch;
        }
    }

    @Override
    public InputStream getResourceStream(String source) throws ResourceNotFoundException
    {
        return new CompactJSONInputStream(innerLoader.getResourceStream(source));
    }

    @Override
    public boolean isSourceModified(Resource resource)
    {
        return innerLoader.isSourceModified(resource);
    }

    @Override
    public long getLastModified(Resource resource)
    {
        return innerLoader.getLastModified(resource);
    }
}

And you would then need to configure Velocity with the following properties:

resource.loader = compact
compact.resource.loader.class = my.custom.loader.CompactJSONResourceLoader

compact.resource.loader.innerLoader = file

(you would replace file with the resource loader you are currently using, of course).

Upvotes: 0

Related Questions