Spring boot whitelabel 404
Asked Answered
I

4

5

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:

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

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:

@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:

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

Below is my build.gradle file:

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:

<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:

 <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:

<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>
Iterate answered 25/4, 2015 at 21:17 Comment(7)
The error you have specified is generic error page. Can you post the exception details? If you are running your application in terminal then there must be an exception stack trace.Fourwheeler
Do you see this? 2015-04-26 16:11:53.987 INFO 4817 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String hello.HelloController.helloFacebook(org.springframework.ui.Model)Masoretic
@AlJacinto the error came because i placed my Application class in a different package from my HomeController.java. It could not find home.html which is placed in templates. Do you know how i can fix it?Iterate
You have a home.html? Should just be hello.html. Do you see any pages at all?Masoretic
@AlJacinto it's hello.html and not home.html. I got to see the pages when i removed Application.java from the main package and placed it in the same package as HomeController.java.Iterate
I think it should work even on separate package, just removed the bootRepackage { mainClass = 'facebookArchiver.Application' } and just let springboot scan for the main classMasoretic
@AlJacinto would try that and let you knowIterate
H
31

If your controllers are in a different package structure, then @ComponentScan needs to be added to the Spring Boot application class.

Example :

@ComponentScan(basePackages={"package name of where the controller is"})
Hambrick answered 7/5, 2015 at 12:52 Comment(1)
thanks , it worked , do we need also include the package name , that have the POJO classes also ?Elledge
R
2

If you put the Controllers & Repositories in different packages,

then @ComponentScan and @EnableMongoRepositories annotation, we have to give the full package name of the controller & repository.

@ComponentScan(basePackages={"package name of controller", "package name of repository"})
@EnableMongoRepositories("package name of repository")
Roby answered 1/2, 2017 at 21:25 Comment(0)
L
0

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)
Logjam answered 31/10, 2017 at 9:35 Comment(0)
S
0

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.

  1. Stop tomcat.
  2. Right click on the project.
  3. Gradle -> Refresh gradle project (Or similar step for maven)
  4. Start tomcat.
  5. 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')

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.

Spheroidal answered 16/7, 2018 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.