[英]Tomcat requires long time to recognizes the start of spring-boot application
[英]Spring-boot logo not loading on console, how to start application on tomcat server?
這是一個使用 spring-boot 構建的簡單微服務。 當我作為 Java 應用程序執行時,應用程序工作正常。 但是當我在服務器上運行時,即 Tomcat Server v8.5 服務器顯示 Started & Syncnorized 但在控制台中,spring-boot 標志沒有出現,應用程序沒有啟動。 我的 IDE 上的其他項目工作正常。
我嘗試了以下但沒有奏效:
1)刪除服務器並重新添加。 2)使用Maven清理項目。 3) 更改 BuildPath 設置(JDK 版本、Facets 等)。
截圖參考 1) https://i.imgur.com/fVFLhNK.png 2) https://i.imgur.com/nTSWEGd.png
主應用程序
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);
}
}
控制器
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 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>
應用程序屬性
#Local Database
spring.datasource.url = jdbc:mysql://localhost:3306/site_hawk
spring.datasource.username = root
spring.datasource.password = admin123
server.port=8089
請幫助我如何運行我的 spring-boot 應用程序。我想在控制台上看到 Spring boot 運行。 但是控制台上沒有出現Spring Boot的Big Logo。
應用程序容器(包括 Tomcat)的正常部署格式是.war,但是 Spring Boot 默認打包一個可運行的 jar 文件。
您可以在pom.xml文件中更改打包格式。
<packaging>war</packaging>
您可能還需要為 Tomcat 特定類添加額外的依賴項。
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
這樣的設置應該為$project_dir/target/classes 生成一個.war文件,大多數 servlet 容器都可以成功運行該文件。
服務器不會簡單地打印控制台中的所有內容,因為會有很多應用程序同時運行,控制台會被這么多的“System.out”搞得一團糟,所以解決方案到服務器被放在每個應用程序的日志中。 嘗試找到您的應用程序日志,也許春天的標志會在那里。
還要確保您正在上傳.war
文件。 jar 包用於運行 is you pc。 對於 Tomcat,您需要發送.war
文件。 您可以簡單地更改pom.xml
的打包類型。用法 - Apache Maven WAR 插件
要在 tomcat 服務器中運行您的應用程序,您應該通過以下方式排除嵌入式應用程序:
<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>
正如 käyrätorvi 所說,將您的應用程序打包為戰爭
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.