Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
velocity
System.out.println("parsing JSON file ");
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("bikes.json").getFile());
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(file);
System.out.println(jsonNode);
Properties props = new Properties();
props.setProperty("resource.loader", "file");
//props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
VelocityEngine velocityEngine = new VelocityEngine();
// @DOC: http://velocity.apache.org/engine/1.7/developer-guide.html#configuring-resource-loaders
String path = "C:\\pierre\\workspacepv\\velocity\\src\\main\\resources\\templates\\";
props.put("file.resource.loader.path", path);
//props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
velocityEngine.init(props);
// do it with JSON only
Template t = velocityEngine.getTemplate("index.vm");
VelocityContext context = new VelocityContext();
context.put("payload", jsonNode);
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println("Velocity with JSON");
System.out.println(writer.toString());
// now do it with Java
ObjectMapper objectMapperJava = new ObjectMapper();
objectMapperJava.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
Example example = objectMapper.readValue(file, Example.class);
System.out.println("Java payload");
System.out.println(example);
context.put("payload", example);
StringWriter writerJava = new StringWriter();
t.merge(context, writerJava);
System.out.println("Velocity with Java");
System.out.println(writerJava.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment