Failure in putting template mapping using Java Api

Hi,
I'm trying to put a template mapping into elasticsearch using the Java api. This is the code I'm using:

Client client = TransportClient.builder()
    .build()
    .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(ES_ADDRESS, 9300)));
String indexSettings;
InputStream jsonSettingsIS = Thread.currentThread().getContextClassLoader().getResourceAsStream("" + index +     ".json");
indexSettings = new Scanner(jsonSettingsIS).useDelimiter("\\A").next();
try
{
   client.admin()
        .indices()
        .preparePutTemplate(index)
        .setTemplate(indexSettings)
        .get();
}
catch (IndexTemplateAlreadyExistsException e)
{
   LOGGER.info("Template index [" + index + "] already exists.");
}

When executing that snipped of code I get this error:

Exception in thread "main" InvalidIndexTemplateException[index_template [template-*] invalid, cause [Validation    Failed: 1: template must not contain a space;2: template must not contain a ',';3: template must not container the following characters [\, /, *, ?, ", <, >, |,  , ,];]]
at org.elasticsearch.cluster.metadata.MetaDataIndexTemplateService.validate(MetaDataIndexTemplateService.java:218)

The template I'm trying to put is

{
    "template": "template-*",
    "settings": {
       "index": {
          "analysis": {
             "analyzer": {
                "whitespace": {
                   "type": "whitespace"
                   }
               }
            }
         }
      }
}

Please format better your code. This is ugly to read IMO. </> icon is your friend! :slight_smile:

You need to use setSource instead of setTemplate. setTemplate is used to set the template name only.

Hi, thanks for your answers. Using setSource actually works.
Thanks again!