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