Skip to content

Commit fc38740

Browse files
committed
Converted to Spring Boot
1 parent cf7fb1b commit fc38740

17 files changed

Lines changed: 264 additions & 187 deletions

File tree

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
web: java -jar target/helloworld.jar
1+
web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/java-getting-started-1.0.jar

app.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "Start on Heroku: Java",
33
"description": "A barebones Java app, which can easily be deployed to Heroku.",
4-
"image": "heroku/java",
54
"addons": [ "heroku-postgresql" ]
65
}

pom.xml

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,116 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.example</groupId>
7-
<version>1.0-SNAPSHOT</version>
8-
<artifactId>helloworld</artifactId>
7+
<artifactId>java-getting-started</artifactId>
8+
<version>1.0</version>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.5.2.RELEASE</version>
14+
</parent>
15+
<name>spring-getting-started</name>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<webjars-bootstrap.version>3.3.6</webjars-bootstrap.version>
22+
<webjars-jquery-ui.version>1.11.4</webjars-jquery-ui.version>
23+
<webjars-jquery.version>2.2.4</webjars-jquery.version>
24+
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
25+
</properties>
26+
927
<dependencies>
28+
<!-- Spring and Spring Boot dependencies -->
1029
<dependency>
11-
<groupId>com.sparkjava</groupId>
12-
<artifactId>spark-core</artifactId>
13-
<version>2.2</version>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-actuator</artifactId>
1432
</dependency>
1533
<dependency>
16-
<groupId>com.sparkjava</groupId>
17-
<artifactId>spark-template-freemarker</artifactId>
18-
<version>2.0.0</version>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-cache</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-web</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
44+
<exclusions>
45+
<exclusion>
46+
<groupId>nz.net.ultraq.thymeleaf</groupId>
47+
<artifactId>thymeleaf-layout-dialect</artifactId>
48+
</exclusion>
49+
</exclusions>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-test</artifactId>
54+
<scope>test</scope>
1955
</dependency>
2056
<dependency>
2157
<groupId>org.postgresql</groupId>
2258
<artifactId>postgresql</artifactId>
23-
<version>9.4-1201-jdbc4</version>
59+
<version>9.4.1212</version>
2460
</dependency>
2561
<dependency>
2662
<groupId>com.zaxxer</groupId>
2763
<artifactId>HikariCP</artifactId>
2864
<version>2.6.0</version>
2965
</dependency>
66+
<dependency>
67+
<groupId>org.webjars</groupId>
68+
<artifactId>webjars-locator</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.webjars</groupId>
72+
<artifactId>jquery</artifactId>
73+
<version>${webjars-jquery.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.webjars</groupId>
77+
<artifactId>jquery-ui</artifactId>
78+
<version>${webjars-jquery-ui.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.webjars</groupId>
82+
<artifactId>bootstrap</artifactId>
83+
<version>${webjars-bootstrap.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.springframework.boot</groupId>
87+
<artifactId>spring-boot-devtools</artifactId>
88+
<scope>runtime</scope>
89+
</dependency>
3090
</dependencies>
91+
3192
<build>
3293
<plugins>
3394
<plugin>
34-
<groupId>org.apache.maven.plugins</groupId>
35-
<artifactId>maven-compiler-plugin</artifactId>
36-
<version>2.5.1</version>
37-
<configuration>
38-
<source>1.8</source>
39-
<target>1.8</target>
40-
<optimize>true</optimize>
41-
<debug>true</debug>
42-
</configuration>
43-
</plugin>
44-
<plugin>
45-
<artifactId>maven-assembly-plugin</artifactId>
46-
<version>2.3</version>
47-
<configuration>
48-
<descriptorRefs>
49-
<descriptorRef>jar-with-dependencies</descriptorRef>
50-
</descriptorRefs>
51-
<finalName>helloworld</finalName>
52-
<appendAssemblyId>false</appendAssemblyId>
53-
<archive>
54-
<manifest>
55-
<mainClass>Main</mainClass>
56-
</manifest>
57-
</archive>
58-
</configuration>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
5997
<executions>
6098
<execution>
61-
<id>build-jar-with-dependencies</id>
62-
<phase>package</phase>
6399
<goals>
64-
<goal>single</goal>
100+
<goal>build-info</goal>
65101
</goals>
102+
<configuration>
103+
<additionalProperties>
104+
<encoding.source>${project.build.sourceEncoding}</encoding.source>
105+
<encoding.reporting>${project.reporting.outputEncoding}</encoding.reporting>
106+
<java.source>${maven.compiler.source}</java.source>
107+
<java.target>${maven.compiler.target}</java.target>
108+
</additionalProperties>
109+
</configuration>
66110
</execution>
67111
</executions>
68112
</plugin>
69113
</plugins>
70114
</build>
115+
71116
</project>

src/main/java/Main.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example;
18+
19+
import com.zaxxer.hikari.HikariConfig;
20+
import com.zaxxer.hikari.HikariDataSource;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.stereotype.Controller;
27+
import org.springframework.web.bind.annotation.RequestMapping;
28+
29+
import javax.sql.DataSource;
30+
import java.sql.Connection;
31+
import java.sql.ResultSet;
32+
import java.sql.SQLException;
33+
import java.sql.Statement;
34+
import java.util.ArrayList;
35+
import java.util.Map;
36+
37+
@Controller
38+
@SpringBootApplication
39+
public class MainController {
40+
41+
@Value("${spring.datasource.url}")
42+
private String dbUrl;
43+
44+
@Autowired
45+
private DataSource dataSource;
46+
47+
public static void main(String[] args) throws Exception {
48+
SpringApplication.run(MainController.class, args);
49+
}
50+
51+
@RequestMapping("/")
52+
String index() {
53+
return "index";
54+
}
55+
56+
@RequestMapping("/db")
57+
String db(Map<String, Object> model) {
58+
try (Connection connection = dataSource.getConnection()) {
59+
Statement stmt = connection.createStatement();
60+
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
61+
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
62+
ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
63+
64+
ArrayList<String> output = new ArrayList<String>();
65+
while (rs.next()) {
66+
output.add("Read from DB: " + rs.getTimestamp("tick"));
67+
}
68+
69+
model.put("records", output);
70+
return "db";
71+
} catch (Exception e) {
72+
model.put("message", e.getMessage());
73+
return "error";
74+
}
75+
}
76+
77+
@Bean
78+
public DataSource dataSource() throws SQLException {
79+
if (dbUrl == null || dbUrl.isEmpty()) {
80+
return new HikariDataSource();
81+
} else {
82+
HikariConfig config = new HikariConfig();
83+
config.setJdbcUrl(dbUrl);
84+
return new HikariDataSource(config);
85+
}
86+
}
87+
88+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.datasource.url: ${JDBC_DATABASE_URL:}
2+
spring.datasource.hikari.connection-timeout=30000
3+
spring.datasource.hikari.maximum-pool-size=10
4+
5+
spring.thymeleaf.mode=HTML
6+
7+
logging.level.org.springframework=INFO
8+
9+
spring.profiles.active=production

src/main/resources/banner.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
_ _ _
2+
| | | | | |
3+
| |__| | ___ _ __ ___ | | ___ _
4+
| __ |/ _ \ '__/ _ \| |/ / | | |
5+
| | | | __/ | | (_) | <| |_| |
6+
|_| |_|\___|_| \___/|_|\_\\__,_|
7+
8+
:: Built with Spring Boot :: ${spring-boot.version}
1.4 KB
Loading

src/main/resources/spark/template/freemarker/db.ftl

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/resources/spark/template/freemarker/error.ftl

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)