Java 弹簧靴白标 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29870852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot whitelabel 404
提问by Jenn Fitz
Hello guys I keep getting this error even though I have followed spring tutorial from their website. I have been trying to figure it out why I'm getting this error. I am able to connect to facebook but when trying to retrieve the feeds like the tutorial provided by spring, I keep getting a whitelabel error. It says:
大家好,即使我从他们的网站上遵循了 spring 教程,我仍然收到此错误。我一直在试图弄清楚为什么我会收到这个错误。我能够连接到 facebook,但是当尝试检索像 spring 提供的教程一样的提要时,我不断收到白标错误。它说:
This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404). No message available
此应用程序没有明确的 /error 映射,因此您将其视为后备。出现意外错误(类型=未找到,状态=404)。没有可用的消息
Everything seems to be okay but dont know why I keep getting this error. if someone can assist I will appreciate it.
一切似乎都很好,但不知道为什么我不断收到此错误。如果有人可以提供帮助,我将不胜感激。
My controller placed in src/main/java/home:
我的控制器放在 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 file placed in src/main/java/home/main:
Application.java 文件放在 src/main/java/home/main 中:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Below is my build.gradle file:
下面是我的 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 file: It's saved in src/main/resources/templates folder:
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: it's stored under src/main/resources/templates/connect folder:
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>
And finally facebookConnected.html: also stored under src/main/resources/templates/connect folder: Below is the file for facebookConnected.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>
回答by Shano
If your controllers are in a different package structure, then @ComponentScan
needs to be added to the Spring Boot
application class
.
如果您的控制器处于不同的包结构中,则@ComponentScan
需要将其添加到Spring Boot
应用程序中class
。
Example :
例子 :
@ComponentScan(basePackages={"package name of where the controller is"})
回答by Avijit Karmakar
If you put the Controllers & Repositories in different packages,
如果您将控制器和存储库放在不同的包中,
then @ComponentScanand @EnableMongoRepositoriesannotation, we have to give the full package name of the controller & repository.
然后@ComponentScan和@EnableMongoRepositories注释,我们必须给出控制器和存储库的完整包名称。
@ComponentScan(basePackages={"package name of controller", "package name of repository"})
@EnableMongoRepositories("package name of repository")
回答by Amit Datta
You can try to use the following
您可以尝试使用以下
@RestController and produces = MediaType.APPLICATION_JSON_VALUE
For example
例如
@RestController
public class MyController
@GetMapping(value = "/xyz/{ab}", produces = MediaType.APPLICATION_JSON_VALUE)
回答by Saquibul Islam Waheed
I was having the same white label error. Took me couple of hours to figure out that actually thymeleaf dependency wasn't getting resolved properly. The solution was pretty simple and silly both.
我有同样的白标错误。我花了几个小时才发现实际上百里香依赖没有得到正确解决。解决方案既简单又愚蠢。
- Stop tomcat.
- Right click on the project.
- Gradle -> Refresh gradle project (Or similar step for maven)
- Start tomcat.
If that doesn't work, change the position of below line in build.gradle (move up or down), and perform above steps.
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
- 停止Tomcat。
- 右键单击项目。
- Gradle -> 刷新 gradle 项目(或类似的 maven 步骤)
- 启动tomcat。
如果这不起作用,请更改 build.gradle 中以下行的位置(向上或向下移动),并执行上述步骤。
编译('org.springframework.boot:spring-boot-starter-thymeleaf')
The important part is, you have to gradle refresh your project while tomcat is NOT running. If we use devtools, we depend on it blindly for restarting our servlet container and stuffs, but sometimes a manual start stop comes handy.
重要的部分是,您必须在 tomcat 未运行时刷新您的项目。如果我们使用 devtools,我们会盲目地依赖它来重新启动我们的 servlet 容器和其他东西,但有时手动启动停止会很方便。