简体   繁体   English

Spring 5 - HTTP 状态 500 – 内部服务器错误

[英]Spring 5 - HTTP Status 500 – Internal Server Error

I'm doing Website from tutorial and I'm using Spring. Sadly code from video is not working in my IDE.我正在做教程中的网站,我正在使用 Spring。遗憾的是,视频中的代码在我的 IDE 中不起作用。

There is pom.xml.有 pom.xml。

<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>
  <groupId>org.example</groupId>
  <artifactId>Travel</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Travel</name>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>17</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
  
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.0.3</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
  </dependencies>

  
  
</project>

And WebConfing.和网络会议。

package main.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan("main")
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private WebApplicationContext context;
    
    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(context.getServletContext());
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        return resolver;
    }
    
    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }
    
    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }
    
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

When I try to put it in localhost i can see error 500 in browser and then this is from IDE:当我尝试将其放入本地主机时,我可以在浏览器中看到错误 500,然后这是来自 IDE:

sty 07, 2023 7:36:20 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet [dispatcher]
java.lang.Error: Unresolved compilation problems: 
ServletContextTemplateResolver cannot be resolved to a type
ServletContextTemplateResolver cannot be resolved to a type
ServletContextTemplateResolver cannot be resolved to a type
The method getServletContext() from the type WebApplicationContext refers to the missing type ServletContext

at main.config.WebConfig.templateResolver(WebConfig.java:31)
at main.config.WebConfig$$SpringCGLIB$$0.CGLIB$templateResolver$1(<generated>)
at main.config.WebConfig$$SpringCGLIB$$2.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at main.config.WebConfig$$SpringCGLIB$$0.templateResolver(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:491)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1324)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1161)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:561)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:961)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:915)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:706)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:583)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:537)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
at jakarta.servlet.GenericServlet.init(GenericServlet.java:158)
at jakarta.servlet.http.HttpServlet.init(HttpServlet.java:140)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:995)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:696)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1739)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)

So for sure this lines are wrong:所以可以肯定这一行是错误的:

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(context.getServletContext());
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        return resolver;
    }

I tried to change it in this way:我试图以这种方式改变它:

    @Bean
    public ServletContextTemplateResolver templateResolver() {
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver((ServletContext) context.getServletContext());
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        return resolver;
    }

Still same, 500 error.还是一样,500错误。

As you can see in the changelogs , Spring migrated to Jakarta EE in the 6.0 release.正如您在变更日志中看到的那样,Spring 在 6.0 版本中迁移到了 Jakarta EE。

In your pom.xml , you are using version 6.0.3 of spring-webmvc (which requires Jakarta EE) but also javax.servlet-api which is part of Java EE.在您的pom.xml中,您使用的是6.0.3版的spring-webmvc (需要 Jakarta EE)以及javax.servlet-api ,它是 Java EE 的一部分。

You basically have two options:你基本上有两个选择:

Migrate to Jakarta EE迁移到雅加达 EE

Replace代替

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

with

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.0.0</version>
    <scope>provided</scope>
</dependency>

This will then use version 6.0.0 of the Jakarta Servlet API instead of version 4.0.1 of the Java EE servlet API.然后,这将使用 Jakarta Servlet API 的6.0.0版,而不是 Java EE servlet API 的 4.0.1 版。

Alternatively, you can use an older version like 5.0.0 .或者,您可以使用旧版本,如5.0.0

Similarly, you would need toupgrade thymeleaf to 3.1.x as this version supports Spring 6 and Jakarta EE .同样,您需要将 thymeleaf 升级到 3.1.x,因为此版本支持 Spring 6 和 Jakarta EE For doing so, replace为此,更换

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>

with

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring6</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>

so that Thymeleaf is compatible with your Spring version as well.这样 Thymeleaf 也与您的 Spring 版本兼容。

Aside from updating dependencies, it is also important to replace the use of ServletContextTemplateResolver with something else asthese were changed in Spring 3.1 .除了更新依赖项之外,将ServletContextTemplateResolver的使用替换为其他内容也很重要,因为这些在 Spring 3.1 中已更改

Since it is a Spring application, you might be able to use SpringResourceTemplateResolver as described in this Tutorial for Thymeleaf 3.1 :由于它是一个 Spring 应用程序,您可以按照Thymeleaf 3.1 教程中所述使用SpringResourceTemplateResolver

@Bean
public SpringResourceTemplateResolver templateResolver(){
    // SpringResourceTemplateResolver automatically integrates with Spring's own
    // resource resolution infrastructure, which is highly recommended.
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    // HTML is the default value, added here for the sake of clarity.
    templateResolver.setTemplateMode(TemplateMode.HTML);
    // Template cache is true by default. Set to false if you want
    // templates to be automatically updated when modified.
    templateResolver.setCacheable(true);
    return templateResolver;
}

Alternatively, you might be able to use WebApplicationTemplateResolver which can work with both Java EE (using JavaxServletWebApplication ) and Jakarta EE (using JakartaServletWebApplication ):或者,您可以使用WebApplicationTemplateResolver ,它可以与 Java EE(使用JavaxServletWebApplication )和 Jakarta EE(使用JakartaServletWebApplication )一起使用:


    @Bean
    public WebApplicationTemplateResolver templateResolver() {
        WebApplicationTemplateResolver resolver = new WebApplicationTemplateResolver(JakartaServletWebApplication.buildApplication(context.getServletContext()));
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".html");
        return resolver;
    }

Downgrade Spring降级 Spring

Another possibility is to use a version of Spring Webmvc before 6.x .另一种可能性是使用6.x之前的 Spring Webmvc 版本。 For doing that, replace为此,更换

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>6.0.3</version>
    </dependency>

with

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.24</version>
    </dependency>

However, when using 5.x versions of Spring, you would miss the other features of Spring 6.x (including possible future features).但是,当使用 Spring 的 5.x 版本时,您会错过 Spring 6.x 的其他功能(包括未来可能的功能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTTP 状态 500 – Eclipse 中的内部服务器错误 - HTTP Status 500 – Internal Server Error in Eclipse 休眠状态:HTTP状态500-内部服务器错误 - Hibernate :HTTP Status 500 - Internal Server Error HTTP状态500-Glassfish上的内部服务器错误 - HTTP Status 500 - Internal Server Error on Glassfish GlassFish Http状态500-内部服务器错误 - GlassFish Http Status 500 - Internal Server Error HTTP状态500-内部服务器错误蚀 - HTTP Status 500 - Internal Server Error eclipse Spring MVC HTTP状态500 –内部服务器错误,Servlet [dispatcher]的Servlet.init()抛出异常 - Spring MVC HTTP Status 500 – Internal Server Error, Servlet.init() for servlet [dispatcher] threw exception HTTP 状态 500 – 内部服务器错误 | 这么多错误! | 智能| Hibernate & Spring - HTTP Status 500 – Internal Server Error | So many errors! | IntelliJ | Hibernate & Spring HTTP状态500-内部服务器错误-JSP-Glass Fish服务器 - HTTP Status 500 - Internal Server Error - JSP - Glass Fish server “HTTP状态500 - 内部服务器错误”通过服务器上传文件 - “HTTP status 500 - Internal server error” as upload a file over the server HTTP 状态 500 – 内部服务器错误-JSP-Tomcat 服务器 - HTTP Status 500 – Internal Server Error-JSP-Tomcat server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM