2016-12-16 112 views
0

我想运行一个应用程序,所以我可以在本地进行测试,但目前出现问题。在Spring Boot中运行localhost时出错

我使用gradle这个和下面这个教程

https://spring.io/guides/gs/serving-web-content/

不过,我完成了教程,并运行此命令:

./gradlew bootRun 

应用程序启动,但我不能打到最后点。

它引发以下错误:

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Fri Dec 16 16:25:06 GMT 2016 
There was an unexpected error (type=Not Found, status=404). 
No message available 

任何想法如何解决这一问题?

package conf; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

/** 
*/ 

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 

} 

问候语类

package controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam; 

/** 
*/ 

@Controller 
public class GreetingController { 

    @RequestMapping("/greeting") 
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { 
     model.addAttribute("name", name); 
     return "greeting"; 
    } 

} 

感谢

+0

你曾经通过完整的示例代码检查你的代码 “GS-服务 - 网络内容/完成” 文件夹中的[ zip文件](https://github.com/spring-guides/gs-serving-web-content.git)? – Alic

+0

我从头开始,所以我没有检查zip文件中的代码。 – Sgr

+0

通过检查已完成的示例,您将能够看到您做错了什么。 – Alic

回答

2

我相信这是因为你的封装结构。根据您提供的代码,您的Application类无法看到GreetingController,因为它们在兄弟软件包中。 @SpringBootApplication需要能够组件扫描相同的包和子包。它无法看到兄弟软件包。所以GreetingController永远不会连线。

将不起作用:

com.conf.Application 
com.controller.GreetingController 

将工作:

com.conf.Application 
com.conf.controller.GreetingController 
+0

谢谢格雷格,我会尝试提出的解决方案,但我想分开包依赖于类的类型。有没有办法让应用程序扫描所有其他包(包括控制器包)? – Sgr

+0

是的,您必须包含'@ ComponentScan'注释,它应该覆盖'@ SpringBootApplication'提供的注释,并且您可以控制正在扫描的内容和位置。 – Gregg