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)}
Related
I am trying to create a simple spring boot application which has jersey jars as well for some internal rest client implementation to call. My application is starting fine but when trying to hit the controller method, nothing is called & on postman its saying as 404 Not Found.
Spring boot version: 1.5.14.RELEASE
Jersey Client: 2.25.1 (org.glassfish.jersey.core)
Controller class
#EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class})
#RequestMapping("/report")
public class ReportProcessController {
Controller method which I am trying to access
#RequestMapping(value = "/email", method = RequestMethod.GET)
#ResponseBody
public String sendEmail(HttpServletResponse response) {
When I am running this app from IDE, it is working fine, but when app running from the command line, it gives me the following error:
http://localhost:8080/welcome
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing
this as a fallback. Thu Dec 30 21:46:01 BDT 2021 There was an
unexpected error (type=Not Found, status=404)
My controller is :
package com.example.demo;
#Controller
public class HelloController {
#RequestMapping("/welcome")
public String home() {
System.out.println("this is home page");
return "home";
}
}
application.properties
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp
Jsp file loction is :
webapp\views\home.jsp
#PostMapping("/api/file/delete")
public String deleteMultipartFile(HttpServletRequest request) {
try {
String keyname=request.getParameter("keyname");
s3Services.deleteFile(keyname);
return "redirect:/welcome";
} catch(Exception e) {
return "redirect:/welcome";
}
}
Actual result :
printing "redirect:/welcome " in white page
Expected Result : it redirect to welcome modal.
Spring is a Java application framework and Spring boot is an evloution of Spring that helps create stand-alone, production-grade spring based application that you can "just run".
Spring boot supports #Controller and #RestController annotation, where
#Controller annotation indicates that the annotated class is a controller. It is a specialization of #Component and is autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the #RequestMapping annotation. #RestController is a sibling convenience annotation for creating Restful controllers.
If you created spring boot application having controller with annotation #Controller and you have #GetMapping method which return index.html(file location /src/main/resources/templates/index.html) view page. like code below :-
#Controller
public class MyController{
#GetMapping("/")
public String indexPage(){
return "index";
}
#GetMapping("/index")
public String routeWithIndex(){
return "redirect:/";
}
}
Both function returns index.html page. If you want more knowledge about spring annotation of #Controller and #RestController with examples visit baeldung site or official site about spring.
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
I have a total simple Spring Boot app written in Kotlin.
#SpringBootApplication
class DemoApplication
fun main(args: Array<String>)
{
SpringApplication.run(DemoApplication::class.java, *args)
}
#Controller
class HomeController
{
#GetMapping("/")
fun home() = "index.html"
}
In the resource/public folder I have index.html.
However, when I open my browser and typelocalhost:8080/ I get the error java.lang.IllegalStateException: Could not resolve view with name 'index.html' in the running Spring Boot console.
I just want the most minimal code possbile to serve index.html. Shouldnt be that hard? ;)
NOTE: I am using Spring-Web-Reactive