I am fairly new in Spring Boot and trying to build a simple web app. I have defined a controller class containing my mapping for url, but on browser it is giving me a white label page error(404). I am not able to understand why it is not able to map. I have tried changing my component scan base package, but it still doesn't redirect me to page "abc" or print "in controller" in the console.
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>bms</groupId>
<artifactId>com.wellmanage.bms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BMS</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<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>
Controller class
package com.wellmanage.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class BookController {
#RequestMapping("/")
public String hello() {
System.out.println("in controller");
return "abc";
}
}
Main class
package com.wellmanage.bms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class BmsApplication {
public static void main(String[] args) {
SpringApplication.run(BmsApplication.class, args);
}
}
The BmsApplication class is placed under the com.wellmanage.bms package and by default the #SpringBootApplication annotation runs component scan using this package as the root. Since the BookController is inside com.wellmanage.controller, the package is omitted by default configuration.
You have two options:
Change default component scan settings and set which package would you like to scan:
#ComponentScan("com.wellmanage")
#SpringBootApplication
public class BmsApplication {
public static void main(String[] args) {
SpringApplication.run(BmsApplication.class, args);
}
}
Move the main Application class to root package of you app so that all components which you want to scan automatically are under its package, e.g. to com.wellmanage.
From the configuration you have posted, it seems you didn't set any template engine. Returning the String "abc" from your controller is hence ignored.
Related
I am just trying to implement a sample rest controller on spring boot for learning. When i am sending the request through web browser i am getting a white label error.
My applications context path is set to /learn. Below are my source files, project structure and pom.xml.
TestController.java
package com.practice.learn.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/learn")
public class TestController {
TestController(){
System.out.println("TestController initialized");
}
#GetMapping("/test")
public String testMethod() {
System.out.println("Inside test method");
return "This is sample text";
}
}
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 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.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>learn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
LearnApplication.java
package com.practice.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class LearnApplication {
public static void main(String[] args) {
SpringApplication.run(LearnApplication.class, args);
}
}
application.properties
server.port=8081
server.servlet.context-path=/learn
since you already set the servlet context path as '/learn' in your configuration file, you do not need to add '/learn' again in the RequestMapping in your controller. just remove it and let us know.
I am trying to debug why a my application won't show, and in doing so I have this small application running hello world. This does work, it shows the text on the screen, great. But then if I add spring-boot-starter-data-jpa to the pom file, I get a 404 error instead. I need to use spring-boot-starter-data-jpa and have no idea why it doesn't work as expected.
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>
<artifactId>spring-boot-hello</artifactId>
<packaging>war</packaging>
<name>Spring Boot Hello World Example</name>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
</parent>
<properties>
<java.version>11</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>-->
<!--this is the issued one-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>testapp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller
package org.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloController {
#RequestMapping("/")
String hello() {
return "Hello World, Spring Boot!";
}
}
main class
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class MyWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyWebApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(MyWebApplication.class);
}
}
I have gone through every other answer I could find and made sure I was using restcontroller instead of controller, I believe my main class is in the correct spot, I am at a loss as to why one dependency, especially one that isn't being used yet, causes a 404.
In application.properties file in resource folder did you set database configuration such asusername, password and url? It should be set like this.
spring.datasource.url=jdbc:mysql://localhost:3306/restapi
spring.datasource.username=root
spring.datasource.password=
And it's better to use #GetMapping annotation instead of #RequestMapping.
this is pom xml
i have main class and one simple controller which returns string as json.
i am just running spring boot 2.x by adding below dependency.
in console it shows tomcat is started and application is started.but when i am hitting from postman or url in browser it will 404.
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.kd</groupId>
<artifactId>restdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>restdemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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-jersey</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>
this is my controller:
package com.kd.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ServiceControllerKd {
#RequestMapping("/")
public Hello greeting() {
System.out.println("hello");
return new Hello();
}
}
I have taken your code, built it, ran it and hit the endpoint and have successfully received a response. However, it is difficult to assess what may be causing your issue without the full project.
I have created an example project using the code you provided here that runs successfully. It may be best to compare this project against your own to attempt to find any inconsistencies.
https://github.com/michaelmcfadyen/spring-boot-example
double check that you have your controller in the same package as your SpringBootApplication or in a child package
package com.kd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
In the log you have to see something similar to
2018-07-28 12:34:01.669 INFO 10425 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.kd.controller.ServiceControllerKd.greeting()
I think the problem is that you must first try to return a single string
example:
return "Hello world!"
To make sure that the problem is not from your Hello object.
Another thing that you have to make sure is that you have the right port on the postman request example http://localhost:8000/ and that your method on post man get the right verb, in your case you can add
#RequestMapping(value = "/", method = RequestMethod.GET)
and it's gonna be a "get".
I have a very simple web application for testing the #Scheduled annotation.
The method read() in the class RetrievePrices is annotated with #Scheduled . After deploying the war on Tomcat, I was expecting that the read method should be executed every 5 seconds, but nothing is displayed in the Tomcat console.
The Main SpringBoot class
package com.aaaa.main;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
#SpringBootApplication
public class BatchMain {
public static void main(String[] args) {
SpringApplication.run(BatchMain.class, args);
}
}
The Class whith a method annotated with #Scheduled
package com.aaaa.schedule;
import org.springframework.scheduling.annotation.Scheduled;
public class RetrievePrices {
#Scheduled(fixedRate = 5000)
public void read() {
System.out.println(" ************* Scheduled(fixedRate = 5000) ");
}
A very simple Spring Configuration Class
package com.aaaa.config;
#Configuration
#ComponentScan("com.aaaa")
#EnableScheduling
public class MyConfiguration {
}
The POM
<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>1.5.10.RELEASE</version>
</parent>
<artifactId>batch</artifactId>
<name>Batch processes</name>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<Postgres.version>9.4-1200-jdbc41</Postgres.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The updated class RetrievePrices
package com.aaaa.schedule;
import javax.annotation.PostConstruct;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
#Component
public class RetrievePrices {
#Scheduled(fixedRate = 5000)
public void read() {
System.out.println(" ************* into #Scheduled(fixedRate = 5000) ");
}
#PostConstruct
public void postConstruct() {
System.out.println(" ************* postConstruct ************** ");
}
}
Your RetrievePrices class does not have any sort of annotation to get picked up by the Spring scanning. Add #Component annotation for example and it should run fine.
Example:
#SpringBootApplication(scanBasePackages = { "com.aaaa" })
#EnableScheduling
public class BootApplication {
//
}
Creating a Simple Scheduler in SpringBoot (Working Code)
Application Class
package com.springboot.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
#EnableScheduling
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
Controller Class
package com.springboot.test;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class SchedulerTest {
#Scheduled(fixedRate=2000)
public void name() {
System.out.println("HI");
}
}
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.springboot</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</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>
This Simple test code is Working for me.
If you use springBoot 2.4.x (Spring 5 version), to get #Scheduled to work, you will have to define its method in a seperate file class from the #SpringBootApplication file annotated class.
The new class must be a component, so must be annotated with #Component, also add the annotation #EnableScheduling at the same class level, for the all thing to work.
Hope this helped.
I encountered a similar issue and I felt like I should mention what worked for me, it might help someone else.
I added #EnableScheduling, the component scan was fine, sources checked out fine but there was one issue, the project was an inherited groovy project and I did not know that I needed to mark my project source folder as 'sources root' in intellij, this is not necessary in java projects in intellij. So if you have done everything you might need to check your build tool or IDE to make sure that your sources root is marked appropriately.
I have a basic spring boot RESTful application which works fine but when I try to add authorization using spring-boot-starter-security the password is not printed on the console. Following are the contents of the relevant files
application.properties
logging.file=route.log
logging.level.org.springframework.web=ERROR
logging.level.org.springframework.boot.test=ERROR
logging.level.org.springframework.boot.autoconfigure.security=INFO
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.be.test</groupId>
<artifactId>testName</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>
<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>
<start-class>com.be.test.TestNameApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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>
<!-- https://mvnrepository.com/artifact/com.google.maps/google-maps-services -->
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller class
package com.be.test.api;
#RestController
public class TestController {
#RequestMapping(value = "/test", method=RequestMethod.GET)
public
#ResponseBody
long test() {
return 1000;
}
}
Main class
package com.be.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication()
#ComponentScan
#EnableAutoConfiguration
public class TestNameApplication {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = SpringApplication.run(TestNameApplication.class, args);
}
}
Is there anything missing from any of the files ?
Add
logging.level.org.springframework.security=INFO
to your application.properties file. This came from the first note on this page, and an example of it is here.
You add #EnableAutoConfiguration to file TestNameApplication
#EnableWebSecurity
public class TestNameApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(TestNameApplication .class, args);
}
}
You can run file TestNameApplication, then you can see pass in console log IDE.
Spring will auto generate password belong to format:
Using default security password: a72f8e06-d1ac-4211-a84d-933534359c52