Spring Boot不能正常工作,出现了一些错误?

huangapple go评论75阅读模式
英文:

Spring Boot not working getting some errors?

问题

以下是您提供的内容的翻译:

请帮助我,因为我无法找出我的Spring Boot应用程序中出现了什么问题。我正在使用以下的pom.xml文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
</parent>
<properties>
    <java.version>1.8</java.version>
    <spring-boot-starter.version>2.22.0</spring-boot-starter.version>
    <tomcat.version>9.0.37</tomcat.version>
</properties>
<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.2.2.RELEASE</version>
    </dependency>
</dependencies>

当我使用Tomcat 9时,会出现以下错误:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171)
...

而当我使用Tomcat 8时:

<tomcat.version>8.5.50</tomcat.version>

会出现以下错误:

描述:

尝试调用一个不存在的方法。尝试调用的位置为:

org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:175)

以下方法不存在:

org.apache.tomcat.util.modeler.Registry.disableRegistry()V

该方法的类,org.apache.tomcat.util.modeler.Registry,可以从以下位置访问:

jar:file:/tomcat/embed/tomcat-embed-core/8.5.50/tomcat-embed-core-8.5.50.jar!/org/apache/tomcat/util/modeler/Registry.class

它是从以下位置加载的:

file:/tomcat/embed/tomcat-embed-core/8.5.50/tomcat-embed-core-8.5.50.jar

请更正应用程序的类路径,以便它包含一个与org.apache.tomcat.util.modeler.Registry兼容的单个版本。

如果有人知道如何解决,请帮助一下。

英文:

Please help me as I am not able to find out what is the issue in my springboot application. I am using following pom.xml

&lt;parent&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
	&lt;version&gt;2.2.5.RELEASE&lt;/version&gt;
	&lt;relativePath/&gt; 
&lt;/parent&gt;
&lt;properties&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;spring-boot-starter.version&gt;2.22.0&lt;/spring-boot-starter.version&gt;
	&lt;tomcat.version&gt;9.0.37&lt;/tomcat.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
    &lt;dependency&gt;  
        &lt;groupId&gt;javax.servlet&lt;/groupId&gt;  
        &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;  
        &lt;version&gt;3.1.0&lt;/version&gt;  
        &lt;scope&gt;provided&lt;/scope&gt;
    &lt;/dependency&gt; 
    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
	&lt;/dependency&gt;
    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
	    &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
	    &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
	    &lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;
    &lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;
	
	&lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
        &lt;version&gt;5.2.2.RELEASE&lt;/version&gt;
    &lt;/dependency&gt;
 &lt;/dependencies&gt;

When I am using tomcat 9 it gives me below error:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping;
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
com.b.AuthenticationFilter.doFilter(AuthenticationFilter.java:665)
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)

And when I am using tomcat 8 :

&lt;tomcat.version&gt;8.5.50&lt;/tomcat.version&gt;

it gives me below error:

Description:

An attempt was made to call a method that does not exist. The attempt was
made from the following location:

    org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:175)

The following method did not exist:

    org.apache.tomcat.util.modeler.Registry.disableRegistry()V

The method&#39;s class, org.apache.tomcat.util.modeler.Registry, is available from the following locations:

    jar:file:/tomcat/embed/tomcat-embed-core/8.5.50/tomcat-embed-core-8.5.50.jar!/org/apache/tomcat/util/modeler/Registry.class

It was loaded from the following location:

    file:/tomcat/embed/tomcat-embed-core/8.5.50/tomcat-embed-core-8.5.50.jar

Correct the classpath of your application so that it contains a single, compatible version of org.apache.tomcat.util.modeler.Registry

If anyone can knows how to solve it please help.

答案1

得分: 4

你的POM文件中包含一些依赖项,这些依赖项之间存在版本冲突。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter-web 的版本 2.3.3.RELEASE 已经内置了 Tomcat 9.0.37

Tomcat 9 及以上版本使用 servlet 4.x。

从你的 pom.xml 中移除以下依赖项可以解决依赖问题。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

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

因为这些是传递性依赖项,已经被你项目中的其他依赖项提供。

你可以使用 mvn dependency:tree -Dverbose 命令来检查依赖树,以观察潜在的依赖冲突。

英文:

Your POM contains some dependencies that has version conflicts with each other.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;

The 2.3.3.RELEASE version of spring-boot-starter-web is already built with Tomcat 9.0.37

Tomcat 9 and above is on servlet 4.x

Removing these the following dependencies from your pom.xml should alleviate your dependency problems.

&lt;dependency&gt;
    &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
    &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

and

&lt;dependency&gt;  
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;  
    &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;  
    &lt;version&gt;3.1.0&lt;/version&gt;  
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt; 

since these are transitive dependencies that are already provided by dependencies in your project.

You can use mvn dependency:tree -Dverbose to inspect your dependency tree and observe any potential dependency conficts.

答案2

得分: 1

我曾经遇到过类似的问题,原因是 jsp-api 与相应的 servlet-api 版本不匹配。正如 Sven Döring 已经提到的,Spring Boot 2.1.x 使用的是 Tomcat 9,该版本使用/提供 Servlet API v4。因此,您需要 jsp-api 2.3.3,否则可能会出现 NoSuchMethodError。

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.3</version>
    <scope>provided</scope>
</dependency>

有关版本匹配的更多信息可以在这里找到:
[![enter image description here][1]][1]
不幸的是,Tomcat 9 和 Servlet-API 4 在此列表中找不到。

  [1]: https://i.stack.imgur.com/SKmRW.png
英文:

I had a similar problem and the cause was the jsp-api didn't fit to the corresponding servlet-api version. As Sven Döring already mentioned Spring Boot 2.1.x uses Tomcat 9 which uses/provides Servlet API v4. For this you need jsp-api 2.3.3 otherwise you can get the NoSuchMethodError.

&lt;dependency&gt;
    &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
    &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
    &lt;version&gt;2.3.3&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

More info about the version matching can be found here:
Spring Boot不能正常工作,出现了一些错误?
Unfortunately Tomcat 9 and Servlet-API 4 is missing in this list.

答案3

得分: 1

缺失的方法 HttpServletRequest.getHttpServletMapping() 是在 Servlet API 版本 4 中添加的,因此问题是由于您依赖于 Servlet 3.1 API 引起的:

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

不应该需要声明对 Servlet API 的依赖,因为它已经包含在 spring-boot-starter-web 中。从您的 pom.xml 中移除上述依赖应该可以解决您的问题。

英文:

The method that is missing, HttpServletRequest.getHttpServletMapping(), was added to the Servlet API in version 4 so the problem is caused by your dependency on the Servlet 3.1 API:

&lt;dependency&gt;  
    &lt;groupId&gt;javax.servlet&lt;/groupId&gt;  
    &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;  
    &lt;version&gt;3.1.0&lt;/version&gt;  
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt; 

There should be no need to declare a dependency on the Servlet API as it is already included with spring-boot-starter-web. Removing the above from your pom.xml should fix your problem.

huangapple
  • 本文由 发表于 2020年9月2日 20:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63705919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定