Search code examples
spring-boottomcat8centos-web-panel

How to resolve the requested resource is not available error in spring boot which runs on tomcat server?


I had successfully run my spring boot application in localhost.

enter image description here

But when I had run it from the CWP panel tomcat server, I am getting the error given below. How to resolve this error? Do I need anything else to run on a CWP panel tomcat server?

enter image description here

Steps which I had followed

  1. Extends the SpringBootServletInitializer class in the main class.

    @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. Update packaging JAR to WAR

  3. Added Tomcat starter dependency with scope as provided

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-tomcat</artifactId>
         <scope>provided</scope>
     </dependency>
    
  4. Application properties

     server.servlet.context-path=/GDriveAppJava
    
  5. Generated war file(GDriveAppJava) and copied to webapps folder in the tomcat as shown below.

enter image description here

  1. Controller file

    @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);
       }
    

    }

Application properties

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







        

Solution

  • Step 1: You need to generate a war file and deploy it to the tomcat server. Referring to https://codezup.com/deploy-spring-boot-war-to-external-tomcat/ for generating war file.

    Step 2: Search the war file to target folder. Then rename the war file. for example: gdapp.war. Then depoloy the .war file to the tomcat server.

    Step 3: Access the application using the file name in the URL. example: gdapp/GDriveAppJava/...

    Note: Make sure you have configured your database properly.