0

我已经在 localhost 中成功运行了我的 Spring Boot 应用程序。

在此处输入图像描述

但是当我从 CWP 面板 tomcat 服务器运行它时,我得到了下面给出的错误。如何解决此错误?我是否需要在 CWP 面板 tomcat 服务器上运行其他任何东西?

在此处输入图像描述

我遵循的步骤

  1. 扩展主类中的 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);
      }
    
    }
    
  2. 将打包 JAR 更新为 WAR

  3. 添加了具有所提供范围的 Tomcat 启动器依赖项

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
     </dependency>
    
  4. 应用程序属性

     server.servlet.context-path=/GDriveAppJava
    
  5. 生成war文件(GDriveAppJava)并复制到tomcat中的webapps文件夹,如下图所示。

在此处输入图像描述

  1. 控制器文件

    @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







        
4

1 回答 1

1

第一步:你需要生成一个war文件并部署到tomcat服务器上。参考https://codezup.com/deploy-spring-boot-war-to-external-tomcat/生成war文件。

第2步:搜索war文件到目标文件夹。然后重命名war文件。例如:gdapp.war。然后将 .war 文件部署到 tomcat 服务器。

第 3 步:使用 URL 中的文件名访问应用程序。例如:gdapp/GDriveAppJava/...

注意:确保您已正确配置数据库。

于 2021-11-20T14:54:28.880 回答