Issue
This is a Simple Microservice built using spring-boot. The application is working fine when I execute as Java Application. But when I run on Server i.e. Tomcat Server v8.5 the Server shows Started & Syncnorized but in console, the spring-boot logo doesn't appear and the application doesn't start. Other Projects on my IDE are working fine.
I tried following but didn't work:
1)Delete Server and add again. 2)Clean the Project using Maven. 3)Changed BuildPath Settings(JDK Version, Facets, etc).
Screenshot to refer 1) https://i.imgur.com/fVFLhNK.png 2)https://i.imgur.com/nTSWEGd.png
Main App
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class ShUsersApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ShUsersApplication.class, args);
}
}
Controller
package com.logituit.sitehawk.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import com.logituit.sitehawk.Model.Contract;
import com.logituit.sitehawk.Service.UserService;
@RestController
@RequestMapping("/sitehawk")
public class UserController {
private UserService userService;
private Gson gson;
@Autowired
public UserController(@NonNull final UserService userService, @NonNull Gson gson) {
this.userService = userService;
this.gson = gson;
}
@PostMapping("/save")
public String saveDetails(HttpEntity<String> httpEntity) {
final Contract contract = gson.fromJson(httpEntity.getBody(), Contract.class);
if (contract != null) {
userService.createTicketBySite(contract);
return "Success";
} else
return "Failed";
}
@GetMapping("/ticketsfromsite/{siteId}")
public List<Contract> getAllTicketsBySite(HttpEntity<String> httpEntity, @PathVariable("siteId") final int siteId) {
return userService.getTicketsBySite(siteId);
}
}
POM.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.logituit.sitehawk</groupId>
<artifactId>SH_Users</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SH_Users</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
#Local Database
spring.datasource.url = jdbc:mysql://localhost:3306/site_hawk
spring.datasource.username = root
spring.datasource.password = admin123
server.port=8089
Please help me how can I run my spring-boot App.I'm want to see Spring boot running on the console. But the Big Logo of Spring Boot doesn't appear on the console.
Solution
The normal deployment format for application containers (Tomcat included) is .war however Spring Boot by default packages a runnable jar file.
You can change the packaging format in pom.xml file.
<packaging>war</packaging>
You may also need to add an additional dependency for Tomcat specific classes.
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Such setup should yield a .war file to $project_dir/target/classes which most servlet containers can successfully run.
Answered By - käyrätorvi