0

我是Spring Boot的新手。在下面简单的代码,当我使用@RequestMapping,http://localhost:8080/person导致以下错误:RequestMapping 404结果 - Spring Boot

错误:

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"; 
    } 

} 
+0

你要什么样的回报?你想要返回HTML或JSON? –

+0

@ksokol addPerson会返回html,但testPath方法应该返回一个简单的String,但是找不到资源。有一点需要注意的是,如果我将ResponseBody与RequestMapping一起使用,它会返回 – user3163473

+0

您想使用哪种模板引擎? JSP,Freemarker,Velocity,Thymleaf? –

回答

1

考虑到你的答案,那么你实际上应该得到另一个错误。如果您使用弹簧引导起动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状态。

要解决,你实际上可以通过设置它的模式配置Thymeleaf的严格性来LEGACYHTML

spring.thymeleaf.mode=LEGACYHTML5 

但是,它需要你添加一个名为nekohtml另一依赖性:

<dependency> 
    <groupId>net.sourceforge.nekohtml</groupId> 
    <artifactId>nekohtml</artifactId> 
    <version>1.9.15</version> 
</dependency> 

找不到的错误通常意味着别的:

  • 您的视图无法解析:或许您拼错了HTML的名称,或者您没有将其放在正确的文件夹中
  • 无法解析您的请求映射:或许您拼写错了名称,或者您正在使用错误的上下文路径
  • 你没有一个模板引擎,以解决意见
0

它是由<meta>引起了不具有结局标签</meta>。显然Thymleaf对标签非常严格,希望返回更有意义的消息而不是404 Not Found。

+0

它实际上**会给你更多有意义的信息。 – g00glen00b