RequestMapping 结果为 404 - Spring Boot
RequestMapping results in 404 - Spring Boot
我是 Spring Boot 的新手。在下面的简单代码中,当我使用@RequestMapping 时,http://localhost:8080/person 导致以下错误:
错误:
There was an unexpected error (type=Not Found, status=404).
代码:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class SpringBootAlphaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAlphaApplication.class, args);
}
@RequestMapping("/person")
public String addPerson(Model model){
Person person = new Person();
person.setName("SomeName");
person.setAge(28);
model.addAttribute("person", person);
return "personview";
}
@RequestMapping("/")
public String testPath(){
return "Test URL";
}
}
这是由于 <meta>
没有结束标记 </meta>
造成的。显然 Thymeleaf 对标签非常严格,希望返回更有意义的消息而不是 404 Not Found。
考虑到你的回答,那么你实际上应该得到另一个错误。如果您使用 spring-boot-starter-thymeleaf,如果您的视图可以解析(但不包含 </meta>
标记,您将收到的错误), 应该是:
org.xml.sax.SAXParseException: The element type "meta" must be terminated by the matching end-tag "</meta>".
并且您应该获得 type=Internal Server Error, status=500
状态。
要解决这个问题,您实际上可以通过将其模式设置为 LEGACYHTML
:
来配置 Thymeleaf 的严格性
spring.thymeleaf.mode=LEGACYHTML5
但是,它需要您添加另一个名为 nekohtml 的依赖项:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
未找到错误通常意味着其他原因:
- 您的视图无法解析:可能您拼错了 HTML 的名称,或者您没有将它放在正确的文件夹中
- 无法解析您的请求映射:可能您拼错了名称,或者您使用了错误的上下文路径
- 您没有解析视图的模板引擎
我是 Spring Boot 的新手。在下面的简单代码中,当我使用@RequestMapping 时,http://localhost:8080/person 导致以下错误:
错误:
There was an unexpected error (type=Not Found, status=404).
代码:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class SpringBootAlphaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAlphaApplication.class, args);
}
@RequestMapping("/person")
public String addPerson(Model model){
Person person = new Person();
person.setName("SomeName");
person.setAge(28);
model.addAttribute("person", person);
return "personview";
}
@RequestMapping("/")
public String testPath(){
return "Test URL";
}
}
这是由于 <meta>
没有结束标记 </meta>
造成的。显然 Thymeleaf 对标签非常严格,希望返回更有意义的消息而不是 404 Not Found。
考虑到你的回答,那么你实际上应该得到另一个错误。如果您使用 spring-boot-starter-thymeleaf,如果您的视图可以解析(但不包含 </meta>
标记,您将收到的错误), 应该是:
org.xml.sax.SAXParseException: The element type "meta" must be terminated by the matching end-tag "</meta>".
并且您应该获得 type=Internal Server Error, status=500
状态。
要解决这个问题,您实际上可以通过将其模式设置为 LEGACYHTML
:
spring.thymeleaf.mode=LEGACYHTML5
但是,它需要您添加另一个名为 nekohtml 的依赖项:
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.15</version>
</dependency>
未找到错误通常意味着其他原因:
- 您的视图无法解析:可能您拼错了 HTML 的名称,或者您没有将它放在正确的文件夹中
- 无法解析您的请求映射:可能您拼错了名称,或者您使用了错误的上下文路径
- 您没有解析视图的模板引擎