1

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.

asked Jun 30, 2022 at 5:26
4
  • What http status code do you get? Commented Jun 30, 2022 at 5:42
  • 1
    Is your DemoApplication in a different package than of Controller's? If yes, Add @ComponentScan(basePackages={student}) on top of DemoApplication class and see if it works! Commented Jun 30, 2022 at 5:52
  • Small recommendation: read about projectlombok.org and you won't need to write hundreds of lines of getters and setters, and some others Commented Jun 30, 2022 at 7:01
  • Thanks @vladtkachuk for suggestion. I already know about it, but want to start from basic. Commented Jun 30, 2022 at 9:29

1 Answer 1

0

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

answered Jun 30, 2022 at 9:44
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.