Spring boot whitelabel 404

时间:2015-04-25 21:17:25

标签: java html gradle spring-boot spring-social-facebook

大家好,即使我已经从他们的网站上关注了春季教程,我仍然会收到此错误。我一直试图弄清楚为什么我会收到这个错误。我可以连接到Facebook但是当试图检索像春天提供的教程这样的提要时,我不断收到白标错误。它说:

  

此应用程序没有/ error的显式映射,因此您将此视为回退。出现意外错误(type = Not Found,status = 404)。没有可用的消息

一切似乎都没问题,但不知道为什么我一直收到这个错误。如果有人可以提供帮助我会很感激。

我的控制器位于src / main / java / home:

@Controller
@RequestMapping(value="/")
public class HomeController {

private Facebook facebook;

@Inject
public HomeController(Facebook facebook) {
    this.facebook = facebook;
}

@RequestMapping( method =RequestMethod.GET)
public String getPhotos(Model model){

    if(!facebook.isAuthorized())
    {
        return "redirect:/connect/facebook";
    }
    model.addAttribute(facebook.userOperations().getUserProfile());
    PagedList<Post> homeFeed = facebook.feedOperations().getHomeFeed();
    model.addAttribute("feed", homeFeed);

    return "hello";
 }
}

Application.java文件放在src / main / java / home / main:

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

以下是我的build.gradle文件:

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

buildscript{
repositories{
   // maven { url "https://repo.spring.io/release" }
    maven { url "https://repo.spring.io/libs-milestone"}
   // mavenCentral()
      }
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.3.RELEASE")
  }
 }
repositories {
   maven { url "https://repo.spring.io/libs-milestone"}
   mavenCentral()
 }
bootRepackage {
  mainClass = 'facebookArchiver.Application'
}
 dependencies {
  compile ('org.springframework.social:spring-social-facebook:2.0.0.RELEASE')
  compile("org.springframework.boot:spring-boot-starter-thymeleaf")
  compile('org.springframework.boot:spring-boot-starter-web')
  testCompile group: 'junit', name: 'junit', version: '4.11'
 }
 task wrapper(type: Wrapper) {
      gradleVersion = '2.3'
 }

hello.html文件:它保存在src / main / resources / templates文件夹中:

<html>
 <head>
<title>Hello Facebook</title>
</head>
<body>
 <h3>Hello, <span th:text="${facebookProfile.name}">Some User</span>!</h3>

 <h4>Here is your home feed:</h4>

 <div th:each="post:${feed}">
 <b th:text="${post.from.name}">from</b> wrote:
 <p th:text="${post.message}">message text</p>
 <img th:if="${post.picture}" th:src="${post.picture}"/>
 <hr/>
 </div>
</body>
</html>

facebookConnect.html:它存储在src / main / resources / templates / connect文件夹下:

 <html>
 <head>
 <title>Hello Facebook</title>
 </head>
 <body>
  <h3>Connect to Facebook</h3>

  <form action="/connect/facebook" method="POST">
<input type="hidden" name="scope" value="read_stream" />
  <div class="formInfo">
    <p>You aren't connected to Facebook yet. Click the button to connect  this application with your Facebook account.</p>
 </div>
 <p><button type="submit">Connect to Facebook</button></p>
</form>
</body>
</html>

最后facebookConnected.html:还存储在src / main / resources / templates / connect文件夹下:以下是facebookConnected.html的文件:

<html>
<head>
  <title>Hello Facebook</title>
</head>
  <body>
   <h3>Connected to Facebook</h3>
   <p>
     You are now connected to your Facebook account.
    Click <a href="/">here</a> to see some entries from your Facebook photos.
  </p>
</body>
</html>

4 个答案:

答案 0 :(得分:31)

如果您的控制器采用不同的包结构,则需要将@ComponentScan添加到Spring Boot应用class

示例:

@ComponentScan(basePackages={"package name of where the controller is"})

答案 1 :(得分:2)

如果你把Controllers&amp;存储库在不同的包中,

然后 @ComponentScan @EnableMongoRepositories 注释,我们必须提供控制器的完整包名称&amp;库中。

@ComponentScan(basePackages={"package name of controller", "package name of repository"})
@EnableMongoRepositories("package name of repository")

答案 2 :(得分:0)

您可以尝试使用以下

@RestController and produces = MediaType.APPLICATION_JSON_VALUE

例如

@RestController
public class MyController

@GetMapping(value = "/xyz/{ab}", produces = MediaType.APPLICATION_JSON_VALUE)

答案 3 :(得分:0)

我有相同的白标错误。花了我几个小时才弄清楚实际上对百里香的依赖没有得到正确解决。解决方案非常简单而且愚蠢。

  1. 停止tomcat。
  2. 右键单击该项目。
  3. Gradle->刷新gradle项目(或与maven相似的步骤)
  4. 启动tomcat。
  5. 如果这不起作用,请更改build.gradle中下一行的位置(向上或向下移动),然后执行以上步骤。

    compile('org.springframework.boot:spring-boot-starter-thymeleaf')

重要的部分是,当tomcat不运行时,您必须逐步刷新项目。如果使用devtools,我们会盲目地依靠它来重新启动servlet容器和东西,但是有时手动启动停止很方便。