2

I've been trying to convert an existing Springboot App to make it deployable to Tomcat 10. It deploys fine to Tocat but when I try to access here like this http://localhost:8585/kantha/kantha/api/v1/items I get 404 not found error.

If I run it as a spring boot app it works as expected at this http://localhost:8787/kantha/api/v1/items

I do not get any errors in developer tools nor in the catalina log.

Here is my Application File


package com.entrustment.entrustment.sites; 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@SuppressWarnings({"unchecked", "deprecated"})
@SpringBootApplication
@EnableJpaAuditing
@EntityScan("com.entrustment.entrustment.sites.domain")
//@ComponentScan(basePackages= {"mik.miksubishi.domain"})
public class EntrustmentApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(EntrustmentApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(EntrustmentApplication.class, args);  
    }

   @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE); // enables wrapping for root elements
        return builder;
    }
 
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*");
            }
        };
    }
}

Here is my 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>

    <groupId>com.companyname</groupId>
    <artifactId>kantha</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>kantha</name>
    <description>Kantha</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <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>
        </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>
        
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>   
        
        <dependency> 
          <groupId>com.google.guava</groupId> 
          <artifactId>guava</artifactId> 
          <version>22.0</version> 
        </dependency>           
    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
                <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<mainClass>org.apache.openjpa.jdbc.meta.ReverseMappingTool</mainClass>
<commandlineArgs>
    -directory src/main/java -accessType fields
    -useGenericCollections true -package org.yourproject.model
    -metadata none -annotations true
    -innerIdentityClasses false -useBuiltinIdentityClass false
    -primaryKeyOnJoin false
    </commandlineArgs>
<includePluginDependencies>true</includePluginDependencies>
</configuration>
<dependencies>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.CR3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-all</artifactId>
        <version>2.0.1</version>
    </dependency>
</dependencies>
    </plugin>
            
        </plugins>
    </build>
</project>

My Web Initializer

package com.entrustment.entrustment.sites.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class EntrustmentAppWebAppInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {


      @Override
      protected String[] getServletMappings() {
        return new String[] { "/" };  // Standard: Map dispatcher to URLs with /content
      }

      @Override
        protected Class<?>[] getServletConfigClasses() {
      return new Class<?>[] { WebConfig.class };
      }

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }
}

Mik
  • 21
  • 1
  • My applications.properties file has this server.servlet.context-path=/kantha/api/v1 – Mik Feb 21 '21 at 21:17
  • Remove your `EntrustmentAppWebAppInitializer`. That and your app is available add `http://localhost:8585//kantha/api/v1/items `. So unless your war is named `kantha.war` the URL is not correct. – M. Deinum Feb 22 '21 at 07:14
  • I'm serious with my duplication suggestion. It might not be immediately obvious, but if I get the version numbers correct, you'd need Spring Boot 3 to be jakarta EE compatible. And as you're using Tomcat 10, that's what you need. But you use Spring Boot 2. – Olaf Kock Dec 23 '21 at 13:45

1 Answers1

0

Because may be you are passing context path kantha twice by mistake?

http://localhost:8585/kantha/kantha/api/v1/items

Try accessing by below.

http://localhost:8585/kantha/api/v1/items
Alien
  • 15,141
  • 6
  • 37
  • 57
  • I should have mentioned that I tried both http://localhost:8585/kantha/api/v1/items and http://localhost:8585/kantha/kantha/api/v1/items with no luck. – Mik Feb 21 '21 at 21:12