我已经在 localhost 中成功运行了我的 Spring Boot 应用程序。
但是当我从 CWP 面板 tomcat 服务器运行它时,我得到了下面给出的错误。如何解决此错误?我是否需要在 CWP 面板 tomcat 服务器上运行其他任何东西?
我遵循的步骤
扩展主类中的 SpringBootServletInitializer 类。
@SpringBootApplication public class GDriveAppJavaApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder builder) { return builder.sources(GDriveAppJavaApplication.class); } public static void main(String[] args) { SpringApplication.run(GDriveAppJavaApplication.class, args); } }
将打包 JAR 更新为 WAR
添加了具有所提供范围的 Tomcat 启动器依赖项
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
应用程序属性
server.servlet.context-path=/GDriveAppJava
生成war文件(GDriveAppJava)并复制到tomcat中的webapps文件夹,如下图所示。
控制器文件
@RestController @AllArgsConstructor public class FileController { @Autowired private FileManager fileManager; @Autowired private FileRepository fileRepository; @PostMapping(value = "/upload/{user_id}") public ResponseEntity<FileEntity> uploadFIle(@RequestParam("file") MultipartFile file,@PathVariable String user_id){ FileEntity updated = fileManager.uploadFile(file, user_id); return new ResponseEntity<FileEntity>(updated, HttpStatus.OK); } @GetMapping(path="/viewByUserId/{user_id}") public List<FileEntity> viewByUserid(@PathVariable String user_id) { return fileRepository.findByUserId(user_id); } @GetMapping("/downloadFile/{file_id}") public void download(@PathVariable String file_id, HttpServletResponse response) throws IOException, GeneralSecurityException { fileManager.downloadFile(file_id, response.getOutputStream()); } @GetMapping(path="/viewByFileId/{file_id}") public List<FileEntity> viewByFileId(@PathVariable String file_id) { return fileRepository.findByFileId(file_id); }
}
应用程序属性
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:jdbc:mysql://${MYSQL_HOST:localhost}:3306/user123_gdriveapp
spring.datasource.username=user123//my server username
spring.datasource.password=password123//my server password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql: true
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
server.servlet.context-path=/GDriveAppJava