spring #controller vs #RestController error whitelabel error - spring

I created a basic Spring MVC project in STS IDE.
When I try to add a controller I add the annotation #Controller and run the spring boot program, I get the following error:-(Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.), but when I add a #RestController the method returns the 'hello' string. Please help me out on how to fix it using #Controller and please let me know why this happened.
Sample code:
package com.adithya.spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Controller
public class ApplicationController {
#RequestMapping("/")
public String Hello() {
return "Hello";
}
}
Also, my pom.xml file is as below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adithya</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyApplication</name>
<description>Login & Reg</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Project directory structure .

Related

Spring boot: Getting 404 status always

I tried to setup Spring boot basic example in my machine. BUt after setup, i am getting 404 status code returned.
I tried so many solutions provided on stack overflow. But none of them resolved my problem. Please help me, TIA.
I tried sub package structure to main mathod present package.
I tried adding scanBasePackages to #SpringBootApplication. Still no luck.
my code: Main Class . I tried by using without and with scanBasePackages.
package com.online.sbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = {"com.online.sbo"})
public class OnlineSboApplication {
public static void main(String[] args) {
SpringApplication.run(OnlineSboApplication.class, args);
}
}
My controller: In server logs its not showing that this controller is registerd.
package com.online.sbo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController("/home")
public class HomeController {
#GetMapping(value="/render", produces =
MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public String renderHomre() {
return "gfvbytfgh";
}
}
My Pom.xml: I have not added any security in my project. I have created this project from start.spring.iosite.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.online.sbo</groupId>
<artifactId>OnlineSbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>OnlineSbo</name>
<description>Odisha Bank</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application.properties:- There is No entry in this file.
You use #RestController attribute wrong.
This should work:
#RestController
#RequestMapping("/home")
public class HomeController {
#GetMapping(value="/render", produces =
MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public String renderHomre() {
return "gfvbytfgh";
}
}
ps: In your example localhost:8080/render should work, too.

Unable to run Spring Boot simple REST service

I'm trying to run a basic Spring Boot application created with the Spring Initializr. I see the application is pretty much the same of many examples available on the Web but I get a '404' page not found:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
#RestController
public class Controller {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
return "it works!";
}
}
This is the main class:
package com.example.springtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The pom.xml includes just the web starter:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The execution:
$ curl http://localhost:8080
{"timestamp":"2018-12-11T09:18:59.236+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
Do I miss any starter to get it working?
Thanks
use #ComponentScan() annotation in the main method class and pass the path to the rest service component scan as a parameter.
because by default it only include the service which is in the package and sub package of the main class . so for adding other path service you have to pass the path in component scan annotation.

Spring Boot Not Finding Any Controllers

Hello i've tried to create a simple controller and put it in the same package with the main class but it looks like it's not finding it.Basically, when i run the app it's not mapping the endpoint.
I just can't figure out what is wrong with it.
Here is the code:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestProject1Application {
public static void main(String[] args) {
SpringApplication.run(TestProject1Application.class, args);
}
}
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HomeController {
#RequestMapping(value="/hello", method = RequestMethod.GET)
public String sayHello() {
return "HELLO";
}
}
Project Structure and Console:
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tsv</groupId>
<artifactId>TestProject-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestProject-1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Although in your log, I do not see 'ServletRegistrationBean' getting initialized, I find no issue with your configuration. Try execute the Spring boot app again & you should be able to invoke the HomeController via 'localhost:8080/hello'.

Spring-boot scheduler runs without #EnableScheduling annotation

I was following this example to create a scheduled task in a sample project: https://spring.io/guides/gs/scheduling-tasks
It says, #EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
But, by mistake i didn't use it. How come it still works?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Other Classes:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#Component
public class ScheduledTaskRunner {
Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);
#Scheduled(cron = "0/1 * * * * *")
public void run(){
log.info("Hello");
}
}
Isn't the scheduled task supposed to not run?
Your code added the spring-boot-starter-actuator when that is added a scheduler is added as well. This scheduler is used by the actuator to schedule the automatic export of the metrics.
The code for this is in the MetricExportAutoConfiguration and has been added in Spring Boot 1.3.0.
So if you remove the actuator scheduling won't work anymore.

SpringBootApplication cannot be resolved to a type

Hi below is my Spring code and pom.xml. Maven is not able to update even though I tried to force project update. Any resolution on this or direction where I am getting wrong.
package com.boot;
import org.springframework.*;
#SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println("Hello World");
}
}
pom
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.boot.App</start-class>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I am not getting why Maven is not able to get the dependencies for my project. A
I faced the same issue.
I changed the version from 2.0.5.RELEASE to 2.0.3.RELEASE to make it work
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
If you are fine to move to 2.x then please follow below steps:
Step 1 copy below pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>das-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Here is your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DasBootApplication {
public static void main(String[] args) {
SpringApplication.run(DasBootApplication.class, args);
}
}
or you can also stay with 1.4.2, just change java version to 1.6.
You should provide the class name in the run() method like below:
#SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(App.class, args);
Sytem.out.print("Started");
}
And remove the below line from pom.xml
<start-class>com.boot.App</start-class>

Resources