Skip to content
Advertisement

An attempt was made to call a method that does not exist. STS

When i run the STS(SpringBoot) application i get the below error:

The attempt was made from the following location:      org.apache.catalina.authenticator.AuthenticatorBase.startInternal(AuthenticatorBase.java:1321)

The following method did not exist:

javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

The method’s class, javax.servlet.ServletContext, is available from the following locations:

jar:file:/home/talha/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
    jar:file:/home/talha/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.33/tomcat-embed-core-9.0.33.jar!/javax/servlet/ServletContext.class

It was loaded from the following location:

file:/home/talha/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar

The suggestion in the ide is: Action:

Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext

I guess there is something wrong with my pom.xml the code is as below:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.uni</groupId>
    <artifactId>authorize</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>authorize</name>
    <description>API for user registration and login with validation</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
        <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
         <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.googlecode.jsontoken</groupId>
            <artifactId>jsontoken</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- Thanks for using https://jar-download.com  -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>authrize</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

The main dependencies causing the error are:

<dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.googlecode.jsontoken</groupId>
            <artifactId>jsontoken</artifactId>
            <version>1.0</version>
        </dependency>

I get the error only after adding the above dependencies, the need to add the dependencies is the below class:

package com.uni.authorize.service;

import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.uni.authorize.model.TokenKeys;
import com.uni.authorize.pojo.TokenObject;
import com.uni.authorize.repository.TokenKeysRepository;
import com.uni.authorize.service.CreateToken;

import net.oauth.jsontoken.JsonToken;
import net.oauth.jsontoken.crypto.HmacSHA256Signer;

@Configuration
public class CreateToken {

    private static final Logger LOGGER = LoggerFactory.getLogger(CreateToken.class);

    private static final String ISSUER = "UnI United Tech";

    @Autowired
    TokenKeysRepository tokenKeyRepository;

    public TokenObject createToken(String userId) {

        List<TokenKeys> tokenList = tokenKeyRepository.findAll();
        TokenKeys tokenKeys = tokenList.get(0);

        long accessTokenValidity = tokenKeys.getAccessTokenValidity();
        long refreshTokenValidiry = tokenKeys.getRefreshTokenValidity();

        String accessTokenKey = tokenKeys.getAccessKey();

        String refreshTokenKey = tokenKeys.getRefreshKey();

        String accessToken = generateAccessToken(userId, accessTokenKey, accessTokenValidity);
        String refreshToken = generateRefreshToken(userId, refreshTokenKey, refreshTokenValidiry);

        TokenObject tokenObject = new TokenObject();

        tokenObject.setAccess_token(accessToken);
        tokenObject.setRefresh_token(refreshToken);
        tokenObject.setToken_type("bearer");
        tokenObject.setExpires_in(accessTokenValidity);
        tokenObject.setScope("read write trust");
        return tokenObject;
    }

    private String generateAccessToken(String userId, String accessTokenKey, long accessTokenValidity) {

        HmacSHA256Signer signer;

        try {
            signer = new HmacSHA256Signer(ISSUER, null, accessTokenKey.getBytes());
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

        // Configure JSON token
        JsonToken token = new net.oauth.jsontoken.JsonToken(signer);
        // token.setAudience(AUDIENCE);

        DateTime dateTime = new DateTime();

        long dateTimeMillis = dateTime.getMillis();

//      DateTime currentTimeDateTime = new DateTime(dateTimeMillis);
//      DateTime expiryTimeDateTime = new DateTime(dateTimeMillis + accessTokenValidity);

        Instant currentTimeInstant = new org.joda.time.Instant(dateTimeMillis);
        Instant expirationTimeInstant = new org.joda.time.Instant(dateTimeMillis + accessTokenValidity);

        LOGGER.debug("Current Time Instant" + currentTimeInstant);
        LOGGER.debug("Expiration Tine Instant" + expirationTimeInstant);

        token.setIssuedAt(currentTimeInstant);
        token.setExpiration(expirationTimeInstant);

        // Configure request object, which provides information of the item
        JsonObject request = new JsonObject();
        request.addProperty("userId", userId);

        JsonObject payload = token.getPayloadAsJsonObject();
        payload.add("info", request);

        try {
            return token.serializeAndSign();
        } catch (SignatureException e) {
            throw new RuntimeException(e);
        }
    }

    private String generateRefreshToken(String userId, String refreshTokenKey, long refreshTokenValidiry) {
        HmacSHA256Signer signer;

        try {
            signer = new HmacSHA256Signer(ISSUER, null, refreshTokenKey.getBytes());
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }

        // Configure JSON token
        JsonToken token = new net.oauth.jsontoken.JsonToken(signer);
        // token.setAudience(AUDIENCE);

        DateTime dateTime = new DateTime();

        long dateTimeMillis = dateTime.getMillis();

//      DateTime currentTimeDateTime = new DateTime(dateTimeMillis);
//      DateTime expiryTimeDateTime = new DateTime(dateTimeMillis + refreshTokenValidiry);

        Instant currentTimeInstant = new org.joda.time.Instant(dateTimeMillis);
        Instant expirationTimeInstant = new org.joda.time.Instant(dateTimeMillis + refreshTokenValidiry);

        LOGGER.debug("Current Time Instant" + currentTimeInstant);
        LOGGER.debug("Expiration Tine Instant" + expirationTimeInstant);

        token.setIssuedAt(currentTimeInstant);
        token.setExpiration(expirationTimeInstant);

        // Configure request object, which provides information of the item
        JsonObject request = new JsonObject();
        request.addProperty("userId", userId);

        JsonObject payload = token.getPayloadAsJsonObject();
        payload.add("info", request);

        try {
            return token.serializeAndSign();
        } catch (SignatureException e) {
            throw new RuntimeException(e);
        }
    }

}

Please help me resolve this issue. Thanks in advance

Answer

getVirtualServerName() was added in Servlet 3.1, but you included servlet-api-2.5.jar is your application.

Options:

  • Change your dependencies to include servlet-api-3.1.jar (or later)

  • Remove the servlet-api-2.5.jar dependency, since the correct version is included in the Embedded Tomcat file (tomcat-embed-core-9.0.33.jar).

Actually, you should never ship servlet-api.jar with your application, since it will be provided by the Servlet Container. Seems you’re missing <scope>provided</scope> in your dependency tag for the servlet-api file.

Advertisement