How To Fix Your First Spring Boot API Without Facing White Label Error
Posted By : Mohit Jain | 29-Apr-2018
Spring supports following types of stereotype annotations :-
1.) @Controller :- If our class instance(Object) is used as controller to get request from incoming url
2.)@Service :- If our class instance(Object) has bussiness logic(some calculation or filteration on data came as input from controller/as input from repository) to implement
3.)@Repository :-To persist or retrieve our data after or before our bussiness logic in @Service annotated class
4.)@Component :- If we want some utilities for our application(may be third party web service or our own created services)
For Our first Spring boot "Hello My First Spring Boot Application" as response "without getting white lablel error with status 404" ,we required only @Controller annotated class instance(Object)
/*******************************************************************************************************/
Main class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFirstApplication .class, args);
}
}
/********************************************************************************************************/
Controller class
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
@Controller
public
class
HelloController {
@RequestMapping
(
"/"
)
String home() {
return
"
Hello My First Spring Boot Application"
;
}
}
Actually the instance of above controller is created and loaded in Application context(spring Ioc container) if and only if, above controller is scanned by spring container either of the following 3 ways (else you will encounter " white lablel error with status 404"):-
1.)If your controller class is within the same package of your main class
Example:- Main class and controller class is within " package com.mainpackage"
2. If you controller class is within child package of you main class package
Example:- Main class is in "package com.mainpackage" and controller class is within child of main package "package com.mainpackage.
controller
"
3.)If yourMain class and controller class package has no relation,then you must annotate or pass “com.someotherpackage.controller”) in your main class (
Main class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@
ComponentScan(
“
com.someotherpackage.controller
”
)
public class SpringBootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFirstApplication .class, args);
}
}
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Mohit Jain
Mohit Jain working as backend java developer having knowlege of core java,spring,hibernate.