Reputation: 113
I have created very simple spring boot application. But it dose not return the expected output. I have mentioned tried code below.
Main:
package com.example.practice;
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);
}
}
Controller:
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ControllerClass {
@GetMapping("/all")
public String getAllUsers() {
return "get All users";
}
}
Properties:
server.port=8090
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.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.practice</groupId>
<artifactId>practice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Practice 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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I tried in my browser by using this endpoint: http://localhost:8090/api/all
expected output:
get All users
current output:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Nov 01 00:44:41 IST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
What is the wrong I have done in my code. I am new to spring boot
. Please help me to solve this problem. Thank you.
Upvotes: 0
Views: 1036
Reputation: 1271
This can be the problem with packages of classes.
Make sure your ControllerClass
is in the subpackage of DemoApplication
(for example, DemoApplication
is in org.example
package and controller is in org.example.controllers
package) or use @SpringBootApplication(scanBasePackages = "package.with.your.controller")
The problem is, the @SpringBootApplication
annotation consists of:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@ComponentScan
here is without arguments, so according to docs, scanning will occur from the package of the class that declares this annotation. It will scan com.example.practice
and subpackages, but not com.example.controllers
.
Upvotes: 1
Reputation: 1779
Try to use this
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ControllerClass {
@GetMapping("/all")
public ResponseEntity<String> getAllUsers() {
return new ResponseEntity<>("get All users",HttpStatus.OK);
}
}
Upd. It looks that you have also different packages
package com.example.practice;
package com.example.controller;
and @SpringBootApplication can't autoscan your beans. There are two ways to resolve it:
Upvotes: 0