It is still spring, so deploying to an existing environment is also possible. Unfortunately, running a Spring Boot application on Jetty server requires some tweaks in its pom.xml. If you don’t feel like reading further and want to jump right into action, here’s an example application with simple setup (spring boot, hsql database, hibernate), ready to use as a base for your project.
The default embedded web server used by spring-boot is Tomcat. It is, however, fairly easy to switch to Jetty:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
|
We exclude the starter-tomcat dependency and add starter-jetty.
After following the steps from previously linked Spring blog post and deploying our webapp on jetty, this exception got thrown when starting the application:
1
|
java.lang.NoClassDefFoundError: Could not initialize class org.apache.tomcat.jdbc.pool.DataSource
|
Apparently, even though starter-tomcat was excluded, spring attempted to load Tomcat’s DataSource implementation.
Using a custom ConnectionPool can remedy the problem. There are several CP impementations available, like DBCP, Hikari, C3p0, BoneCP… We chose Hikari. The dependency added to pom.xml:
1
2
3
4
|
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
|
For it to work the tomcat-jdbc dependency had to be excluded from starter-jdbc:
1
2
3
4
5
6
7
8
9
10
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
|
Hikari requires differently named properties to configure a DataSource. Namely, instead of url, a jdbcUrl is required. A config could look like this:
1
2
3
4
5
|
dataSourceClassName=oracle.jdbc.pool.OracleDataSource
spring.datasource.jdbcUrl=jdbc:oracle:thin:@localhost:1521:xe
spring.dataSource.username=login
spring.dataSource.password=password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
|
Details about configuring HikariCP can be found in Hikari’s documentation. If you chose any other connection pool implementation, remember to check their documentation for details about configuring them.
Here’s an example application which should run flawlessly on jetty and through spring-boot:run. It’s a simple REST service with two methods: /set/{param}/{value} and get/{param} which uses an embedded HSQL database and Hibernate as ORM.