I have written a simple code for springboot with rest controller. It runs fine but does not hit the endpoint.
My Application class:
#SpringBootApplication
public class SpringbootRestTemplateDemo {
public static void main(String[] args) {
SpringApplication.run(SpringbootRestTemplateDemo.class, args);
System.out.println("Springboot Rest template demo");
}
}
My controller class:
#RestController
public class ProductController {
#RequestMapping(value="/product/details", method=RequestMethod.GET)
public String getProduct() {
return "Product details";
}
}
I expect a simple message using get request but it is not hitting the endpoint.
Input:
http://localhost:8080/product/details
output:
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback.
Thu Mar 28 19:34:44 IST 2019 There was an unexpected error (type=Not
Found, status=404). No message available
We would likely need to see the folder structure to fully understand. My current guess is its possible that your main class is not in a root package above other classes.
When you run a Spring Boot Application, (i.e. a class annotated with #SpringBootApplication), Spring will only scan the classes below your main class package.
see previous answer: This application has no explicit mapping for /error
Related
I am following the basic Spring Boot application steps to setup the basic Spring Boot app. Have created REST controller as required. After running the application, however, I get this text on localhost:8080- "Whitelabel Error Page
This application has no explicit mapping for /error,
so you are seeing this as a fallback.
Thursday Nov 07 01:41:42 IST 2019
There was an unexpected error (type=Not Found, status=404).
No message available "
Tried adding another callback method apart from the '/' mapping, but it gives the same error.
Controller code:
#RestController
public class TestController {
#RequestMapping(value = "/")
public String home() {
return "Spring boot is working!";
}
#RequestMapping("/testing")
public String testing() {
return "Spring boot is working!";
}
Explicit #ComponentScan doesn't require if your Application class annotated with #SpringBootApplication is in root and others are sub packages of it.
Try putting in below class level annotation in your main class:
#ComponentScan(basePackages = {(name of your controller package)}
I created a front end controller within a project containing multiple (REST) applications. The issue is now that, the controller gets applied for all applications I try to access through the browser. I would like to ask whether the is a configuration or annotation to define for which application the controller should get applied.
This is the code of the controller:
#Controller
public class FrontendController {
#RequestMapping(value = "/")
public String index() {
return "index";
}
}
In the same package the application which serves the front end sources is implemented:
#SpringBootApplication
public class WebServer {
public static void main(String[] args) {
// Tell server to look for web-server.properties or web-server.yml
System.setProperty("spring.config.name", "web-server");
SpringApplication.run(com.studienarbeit.chaoscenter.services.departments.DepartmentsServer.class, args);
}
}
The other applications are in different packages and yet they still serve the front end sources. There are no other controllers in the project and the other applications use Spring Data REST repositories.
Note: Each application runs on its own port.
Note 2: I tried the approach using a profile for the controller:
#Profile("web-server")
Since I work with IntelliJ, I set the active profile to web-server and add the following flag in the VM Options for the specific application:
-Dspring.profiles.active=web-server
Somehow still my other applications access the front end controller. Maybe I did miss something?
Note 3: The other application implementations look basically exactly like the WebServer application and they use Spring Data REST repositories which look like this:
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}
Use specific RequestMapping values for each of your controller classes like :
#Controller
#RequestMapping("/controller1")
public class FrontendController {
#RequestMapping(value = "/")
public String index() {
return "index";
}
}
So you would consume this endpoint with the url http://localhost:8080/controller1
Also, if you're not going to use Mvc Views and this will be only a Rest Controller, use #RestController insted #Controller.
#RestController
#RequestMapping("/controller1")
public class FrontendController
It's a combination of #Controller and #ResponseBody annotations. Detailed information could be found at here.
I used #RequestMapping or #GetMapping for routing.
But, it doesn't work. It makes whitelabel error page.
(spring boot)
my Application.java
#RestController
#SpringBootApplication
public class SmDemoApplication {
#RequestMapping("/")
String home() {
return "Hello World! smDemo 111";
}
public static void main(String[] args) {
SpringApplication.run(SmDemoApplication.class, args);
}
}
And controller is
#RestController
public class WebRestController {
//#GetMapping("/hello")
#RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello() {
return "Hello World! 222";
}
}
http://localhost:8080 is good.
but, http://localhost:8080/hello makes white label error !!
what's the problem in my code?? (I made my project, using spring starter project)
Springboot reads its stereotype beans ,controllers and configurations from package and subpackages of main class,(until you define #ComponentScan to read other packages) I feel your project main class is in another package than the controller i.e. Controller is not in same or in a sub package where Main class is.
For example , If your main package is in
com.xyz.abc
|_ SmDemoApplication
Keep controller,other beans and configuration in com.xyz.abc package like
com.xyz.abc
|_WebRestController
or sub-packages like
com.xyz.abc.controllers
|_WebRestController
I have a very simple webflux demo application with freemarker which has got following files:
1.WebFluxDemoApplication.java
#SpringBootApplication
public class WebFluxDemoApplication {
public static void main(String[] args) {
SpringApplication.run(WebFluxDemoApplication.class, args);
}
#Controller
class HomeController {
#GetMapping("/hello")
public String hello() {
return "index";
}
}
}
2.index.ftl(located under classpath:/templates)
<html>
<head>
<title>demo</title>
</head>
<body></body>
</html>
3.application.yml(without any configuration)
4.pom.xml(with spring-boot-starter-freemarker and spring-boot-starter-webflux)
I can get normal page via http://localhost:8080/hello using these files, but if I add #EnableWebFlux to WebFluxDemoApplication, there's an error shows java.lang.IllegalStateException: Could not resolve view with name 'index'.
I notice that the Spring WebFlux's official guide states to use #EnableWebFlux to configure freemarker. Actually it works for template files, but there seems something wrong with static resources.
For example, I put a main.js file under classpath:/templates/js/, and add <script src="/js/main.js"></script> in index.ftl, then I get an error says WARN 3046 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/js/main.js]: Response status 404 with reason "No matching handler"
UPDATE
For static resources problem, I found a solution which is to add a RouterFunction that resolves the static resources as the post stated.
As stated in the Spring Boot reference documentation, #EnableWebFlux will tell Spring Boot that you wish to take full control over the WebFlux configuration and disable all auto-configuration for this (including static resources).
#EnableWebFlux doesn't configure Freemarker, it actually sets up the whole WebFlux infrastructure. In the case of Spring Boot, adding the spring-boot-starter-freemarker as a dependency (and optionally configuring it through configuration properties) is all you need.
I tried to implement a “Hello Word” demo with Spring 4 that maps the "/" http request to a method call. I found an example doing this but wanted to do the same from scratch. I got it running, the implementation classes (application and controller) do not deviate from the demo but the request mapper in my own app causes an error handler to be called. The differences in the log of the running demo and my app:
——>this line is missing in the bad case: [ main]
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped
“{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}”
onto public java.lang.String
com.goodvideotutorials.spring.controllers.RootController.home()
Could you please have a look at it and advise me why the request mapping does not work in my case? Thank you also in advance. The code sample I copied below:
Application:
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller:
#RestController
public class RootController {
#RequestMapping("/")
public String home(){
return "Hello World!";
}
}
In spring boot applications, the Bean that are found be automatic class scanning must be located in subpackges (or the same package) of the package that contains the class that is used to start the application.