您的位置:首页 > 编程语言 > Java开发

Spring Cloud (4) | NoSuchMethodError:javax.servlet.ServletContext.getVirtualServerName()

2017-11-27 12:05 423 查看
the method
getVirtualServerName
has been added in ServletContext in Servlet 3.1. Find the java doc’s method getVirtualServerName

this problem can have at least 3 causes:

your servlet version is older that 3.1.

other jar has the servlet older version than 3.1.

your tomcat version is older than 8

to solve it, you can try the below way.

I. to check your pom.xml whether there are the below code.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>


if your pom.xml has the above code, it would still has that problem. you can do the second way.

II. to check your other jar has refer to the
javax.servlet-api
jar. for example, the
org.apache.santuario
has refer to the
javax.servlet-api
jar. the pom.xml:

<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
</dependency>


but when you look at the maven dependencies, it refer to the
javax.servlet-api
jar whose version is 2.3 older than 3.1.





so you should exclude the 2.3 version. pom.xml:

<!-- exclude servlet-api 2.3 jar-->
<dependency>
<groupId>org.apache.santuario</groupId>
<artifactId>xmlsec</artifactId>
<version>1.4.3</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- servlet-api 3.1 version has getVirtualServerName() -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>


III. spring boot run the default tomcat 7. so define your tomcat version 8 instead of tomcat 7. To add the code your pom.xml:

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


原文链接:https://stackoverflow.com/questions/34950164/getting-nosuchmethoderrorjavax-servlet-servletcontext-getvirtualservername/47503564#47503564

更多系列文章推荐:

Spring Cloud (20) | Spring Cloud Bus 使用kafka消息总线、gitlab添加webhooks实现自动刷新配置

Spring Cloud (19) | Eureka Server 高可用服务注册中心

Spring Cloud (18) | 给Eureka Server加上安全验证

Spring Cloud (15) | Spring Boot、HikariCP、Mybatis和MySQL 配置HikariCP数据库连接池

Spring Cloud (14) | 微服务不能从git/github/gitlab中获取数据库信息 can’t load properties from git/github/gitlab

Spring Cloud (12) | Spring Cloud Zuul网关调用微服务,request请求参数是application/json

Spring Cloud (11) | healthcheck开启健康检查

Spring Cloud (10) | Eureka 各项参数详解

Spring Cloud (8) | 把Spring Boot项目改造成tomcat容器启动

Spring Cloud (7) | Mongodb 微服务

Spring Cloud (6) | spring cloud zuul 跨域问题No ‘Access-Control-Allow-Origin’ header

Spring Cloud (5) | 配置中心 Config Server 配置git or github or gitlab

Spring Cloud (3) | spring cloud bus 消息总线kafka应用

Spring Cloud (1) | java.net.UnknownHostException: eureka-server
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring-cloud servlet
相关文章推荐