【问题标题】:How to resolve the requested resource is not available error in spring boot which runs on tomcat server?如何解决在tomcat服务器上运行的spring boot中请求的资源不可用错误?
【发布时间】:2021-12-30 19:28:41
【问题描述】:

我已经在 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







        

【问题讨论】:

    标签: spring-boot tomcat8 centos-web-panel


    【解决方案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/...

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

    【讨论】:

    • 这里面的GDriveAppJava是什么?
    • 查看 uri。您将项目文件夹命名为 GDriveAppJava
    • 医学博士。 Skhawath Hossain 但是您提到 gdapp 也是正确的,并且 URL 应该像 projectFolderName/getMethodname 对吗?
    • 当您在 webapp 中上传 .war 文件时,请确保重命名该文件。然后在 URI 的开头添加文件名。我的项目在外部 tomcat 服务器中以这种方式工作。
    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2012-09-12
    • 1970-01-01
    • 2018-11-02
    • 2017-11-14
    相关资源
    最近更新 更多