Created
October 30, 2018 07:07
-
-
Save michael-msx/c9cf803c4c7fa35b82b3d243ac8ea4ff to your computer and use it in GitHub Desktop.
Java Maven for AWS Lambda
This file contains hidden or 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
| import com.amazonaws.services.lambda.runtime.Context; | |
| import com.amazonaws.services.lambda.runtime.RequestHandler; | |
| import com.fasterxml.jackson.core.JsonProcessingException; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| import com.fasterxml.jackson.databind.SerializationFeature; | |
| import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | |
| import org.springframework.jdbc.core.JdbcTemplate; | |
| import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
| import org.springframework.jdbc.support.rowset.SqlRowSet; | |
| import javax.sql.DataSource; | |
| import java.time.LocalDateTime; | |
| import java.time.format.DateTimeFormatter; | |
| public class Demo implements RequestHandler<Integer, String> { | |
| public String handleRequest(Integer integer, Context context) { | |
| JdbcTemplate template = new JdbcTemplate(getDataSource()); | |
| String sql = " select sysdate from dual"; | |
| SqlRowSet rowSet = template.queryForRowSet(sql); | |
| Resp resp = new Resp(); | |
| while (rowSet.next()){ | |
| resp.setNow(rowSet.getDate(1).toLocalDate()); | |
| } | |
| String s = ""; | |
| try { | |
| s = getObjectMapper().writeValueAsString(resp).replace("\"","'"); | |
| } catch (JsonProcessingException e) { | |
| e.printStackTrace(); | |
| } | |
| return s; | |
| } | |
| public ObjectMapper getObjectMapper() { | |
| ObjectMapper mapper = new ObjectMapper(); | |
| JavaTimeModule javaTimeModule=new JavaTimeModule(); | |
| // Hack time module to allow 'Z' at the end of string (i.e. javascript json's) | |
| javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME)); | |
| mapper.registerModule(javaTimeModule); | |
| mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); | |
| return mapper; | |
| } | |
| public static DataSource getDataSource() { | |
| DriverManagerDataSource dataSource = new DriverManagerDataSource(); | |
| dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); | |
| dataSource.setUrl(dbURL); | |
| dataSource.setUsername(dbUser); | |
| dataSource.setPassword(dbPWD); | |
| return dataSource; | |
| } | |
| } |
This file contains hidden or 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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.demo</groupId> | |
| <artifactId>demo-api</artifactId> | |
| <version>1.0</version> | |
| <properties> | |
| <java.version>1.8</java.version> | |
| </properties> | |
| <dependencies> | |
| <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> | |
| <dependency> | |
| <groupId>org.springframework</groupId> | |
| <artifactId>spring-jdbc</artifactId> | |
| <version>5.1.2.RELEASE</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.projectlombok</groupId> | |
| <artifactId>lombok</artifactId> | |
| <version>1.18.2</version> | |
| <scope>provided</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.oracle</groupId> | |
| <artifactId>ojdbc6</artifactId> | |
| <version>11.2.0</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core --> | |
| <dependency> | |
| <groupId>com.amazonaws</groupId> | |
| <artifactId>aws-lambda-java-core</artifactId> | |
| <version>1.2.0</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-core</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-annotations</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.module</groupId> | |
| <artifactId>jackson-module-parameter-names</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.datatype</groupId> | |
| <artifactId>jackson-datatype-jdk8</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.datatype</groupId> | |
| <artifactId>jackson-datatype-jsr310</artifactId> | |
| <version>2.9.7</version> | |
| </dependency> | |
| </dependencies> | |
| <build> | |
| <plugins> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-shade-plugin</artifactId> | |
| <version>3.2.0</version> | |
| <configuration> | |
| <createDependencyReducedPom>false</createDependencyReducedPom> | |
| </configuration> | |
| <executions> | |
| <execution> | |
| <phase>package</phase> | |
| <goals> | |
| <goal>shade</goal> | |
| </goals> | |
| </execution> | |
| </executions> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment