YUVRAJ
YUVRAJ

Reputation: 109

Java spring boot restcontroller not working

I wrote simple java spring boot application code In the application, I have 3 class file

  1. DemoApplication -- contains main method

  2. Student class- POCO class contains following code

    package student;
    

    import java.time.LocalDate;

    public class Student {

    private Long id;
    private String name;
    private String email;
    private LocalDate dob;
    private int age;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public LocalDate getDob() {
        return dob;
    }
    public void setDob(LocalDate dob) {
        this.dob = dob;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(Long id, String name, String email, LocalDate dob, int age) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
        this.dob = dob;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", email=" + email + ", dob=" + dob + ", age=" + age + "]";
    }
    

    }

  3. StudentController class-- controller contains following code

    package student;

    import java.time.LocalDate; import java.util.List;

    import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

    @RestController @RequestMapping(path="/api/student") public class StudentController {

     @GetMapping(path="/all")
     public List<Student> getStudents()
     {
         return List.of(
                 new Student(
                         1L,
                         "Yuvraj Meshram",
                         "[email protected]",
                         LocalDate.of(2001, 5, 6),
                         21
                         )
                 );
     }
    

    }

If I run the application,it started without any error on localhost:8080, but when I hit the url "localhost:8080/api/student/all" , it show me error page "WhiteLabel Error Page".

My pom.xml contains following code

<?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.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <!-- 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</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>

Please help me with this guys.

Upvotes: 1

Views: 1284

Answers (1)

Choppa
Choppa

Reputation: 31

Try adding a @ComponentScan on top of your DemoApplication class. It is probably a package issue.

Upvotes: 0

Related Questions