Created
April 6, 2016 14:49
-
-
Save avantgardnerio/673031c72f31f4fbc30c38aceab81089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.squarelabs.spark; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.introspect.AnnotatedMember; | |
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; | |
import org.jooq.DSLContext; | |
import org.jooq.Record; | |
import org.jooq.Result; | |
import org.jooq.SQLDialect; | |
import org.jooq.impl.DSL; | |
import org.jooq.impl.TableRecordImpl; | |
import org.jooq.util.maven.example.tables.People; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import static spark.Spark.get; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
Class.forName("org.postgresql.Driver"); | |
String userName = System.getProperty("username"); | |
String password = System.getProperty("password"); | |
String url = System.getProperty("url"); | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.setAnnotationIntrospector(new IgnoreInheritedIntrospector()); | |
get("/people", (req, res) -> { | |
try (Connection conn = DriverManager.getConnection(url, userName, password)) { | |
DSLContext create = DSL.using(conn, SQLDialect.POSTGRES); | |
Result<Record> result = create.select().from(People.PEOPLE).fetch(); | |
return result; | |
} | |
}, mapper::writeValueAsString); | |
get("/hello", (req, res) -> "Hello World"); | |
} | |
// http://stackoverflow.com/questions/26945580/jackson-serialization-how-to-ignore-superclass-properties | |
private static class IgnoreInheritedIntrospector extends JacksonAnnotationIntrospector { | |
@Override | |
public boolean hasIgnoreMarker(final AnnotatedMember m) { | |
return m.getDeclaringClass() == TableRecordImpl.class || super.hasIgnoreMarker(m); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment