Tomcat错误“请求的资源.不可用”与spring的GetMapping()

yeotifhr  于 2024-01-08  发布在  Spring
关注(0)|答案(1)|浏览(314)

我正在使用Tomcat 10.1,Maven和Eclipse IDE制作Spring应用程序。我运行我的应用程序并查看index.jsp,但如果我输入路径“/refer”,(从GetMapping(“/refer”)),我想得到 hello_world.html,但得到错误消息:“请求的资源[/lab 57/refer]不可用”,描述:源服务器没有找到目标资源的当前表示,或者不愿意透露存在的表示。
我重新安装了Tomcat,问题没有解决,所以我真的不明白是什么问题?我的代码和项目结构:
x1c 0d1x的数据

HelloController.java

  1. package ru.autoportal.lab;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller
  6. public class HelloController {
  7. @GetMapping("/refer")
  8. public String sayHello() {
  9. return "hello_world";
  10. }
  11. }

字符串

DispatcherServleteInitializer.java

  1. package ru.autoportal.lab.config;
  2. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  3. public class DispatcherServleteInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  4. @Override
  5. protected Class<?>[] getRootConfigClasses() {
  6. return null;
  7. }
  8. @Override
  9. protected Class<?>[] getServletConfigClasses() {
  10. return new Class[]{SpringConfig.class};
  11. }
  12. @Override
  13. protected String[] getServletMappings() {
  14. return new String[]{"/"};
  15. }
  16. }

SpringConfig.java

  1. package ru.autoportal.lab.config;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.ComponentScan;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  8. import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  10. import org.thymeleaf.spring5.SpringTemplateEngine;
  11. import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
  12. import org.thymeleaf.spring5.view.ThymeleafViewResolver;
  13. @Configuration
  14. @ComponentScan("ru.autoportal.lab")
  15. @EnableWebMvc
  16. public class SpringConfig implements WebMvcConfigurer {
  17. private final ApplicationContext applicationContext;
  18. @Autowired
  19. public SpringConfig(ApplicationContext applicationContext) {
  20. this.applicationContext = applicationContext;
  21. }
  22. @Bean
  23. public SpringResourceTemplateResolver templateResolver() {
  24. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  25. templateResolver.setApplicationContext(applicationContext);
  26. templateResolver.setPrefix("/WEB-INF/views/");
  27. templateResolver.setSuffix(".html");
  28. return templateResolver;
  29. }
  30. @Bean
  31. public SpringTemplateEngine templateEngine() {
  32. SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  33. templateEngine.setTemplateResolver(templateResolver());
  34. templateEngine.setEnableSpringELCompiler(true);
  35. return templateEngine;
  36. }
  37. @Override
  38. public void configureViewResolvers(ViewResolverRegistry registry) {
  39. ThymeleafViewResolver resolver = new ThymeleafViewResolver();
  40. resolver.setTemplateEngine(templateEngine());
  41. registry.viewResolver(resolver);
  42. }
  43. }

hello_world.html

  1. <html xmlns:th="http://www.thymeleaf.org" lang="en">
  2. <head>
  3. <meta charset="ISO-8859-1">
  4. <title>My app</title>
  5. </head>
  6. <body>
  7. <p>Hello!</p>
  8. </body>
  9. </html>

pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>ru.portal.autos</groupId>
  6. <artifactId>lab57</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>war</packaging>
  9. <name>lab57 Maven Webapp</name>
  10. <!-- FIXME change it to the project's website -->
  11. <url>http://www.example.com</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <maven.compiler.source>1.7</maven.compiler.source>
  15. <maven.compiler.target>1.7</maven.compiler.target>
  16. <spring.version>5.2.1.RELEASE</spring.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>junit</groupId>
  21. <artifactId>junit</artifactId>
  22. <version>4.11</version>
  23. <scope>test</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-core</artifactId>
  28. <version>${spring.version}</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-context</artifactId>
  33. <version>${spring.version}</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework</groupId>
  37. <artifactId>spring-web</artifactId>
  38. <version>${spring.version}</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-webmvc</artifactId>
  43. <version>${spring.version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.thymeleaf</groupId>
  47. <artifactId>thymeleaf-spring5</artifactId>
  48. <version>3.0.11.RELEASE</version>
  49. </dependency>
  50. <dependency>
  51. <groupId>javax.servlet</groupId>
  52. <artifactId>javax.servlet-api</artifactId>
  53. <version>4.0.1</version>
  54. <scope>provided</scope>
  55. </dependency>
  56. </dependencies>
  57. <build>
  58. <finalName>spring-mvc-app1</finalName>
  59. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  60. <plugins>
  61. <plugin>
  62. <artifactId>maven-clean-plugin</artifactId>
  63. <version>3.1.0</version>
  64. </plugin>
  65. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  66. <plugin>
  67. <artifactId>maven-resources-plugin</artifactId>
  68. <version>3.0.2</version>
  69. </plugin>
  70. <plugin>
  71. <artifactId>maven-compiler-plugin</artifactId>
  72. <version>3.8.0</version>
  73. </plugin>
  74. <plugin>
  75. <artifactId>maven-surefire-plugin</artifactId>
  76. <version>2.22.1</version>
  77. </plugin>
  78. <plugin>
  79. <artifactId>maven-war-plugin</artifactId>
  80. <version>3.2.2</version>
  81. </plugin>
  82. <plugin>
  83. <artifactId>maven-install-plugin</artifactId>
  84. <version>2.5.2</version>
  85. </plugin>
  86. <plugin>
  87. <artifactId>maven-deploy-plugin</artifactId>
  88. <version>2.8.2</version>
  89. </plugin>
  90. </plugins>
  91. </pluginManagement>
  92. </build>
  93. </project>

lrpiutwd

lrpiutwd1#

Spring 5不能在Tomcat 10上工作,但Spring 6可以,因为Tomcat 10和Spring 6发生了名称更改,将javax包重命名为jakarta
如果你想使用Spring 5运行你的应用程序,你需要将你的Tomcat降级到版本9。
如果你想使用Spring 6运行你的应用程序,你需要Tomcat 10和至少JDK 17。
由于软件包重命名时发生的破坏性更改,这两者之间没有中间地带。
另外,我注意到您在pom.xml中使用了JDK 7,(1.7),我还建议你至少使用JDK 8,因为这是一个常见的JDK,用于Spring 5项目旁边的JDK 11,这两者之间的唯一区别是,如果你使用JDK 11,你将需要添加一些额外的jar时,使用Spring和Hibernate,但是如果你使用的是JDK 8(1.8),你就不必这样做了。
下面是Hibernate与JDK 11一起运行所需的依赖项列表,如果您决定使用它而不是JDK 8。

  1. javax.activation-1.2.0.jar
  2. jaxb-api-2.3.0.jar
  3. jaxb-core-2.3.0.jar
  4. jaxb-impl-2.3.0.jar

相关问题