I started learning Spring and been watching this video https://youtu.be/9SGDpanrc8U from Amigoscode. I followed along and did what he did but my code acts differently than his, maybe i missed something, but I set on it for two hours trying to figure out what's not working and nothing works. here is the code
package com.example.demo;
import java.time.LocalDate;
import java.time.Month;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.Student.*;
@SpringBootApplication
@RestController
public class Demo2Application {
public static void main(String[] args) {
SpringApplication.run(Demo2Application.class, args);
}
@GetMapping
public List<Student> hello() {
return List.of(
new Student(
1L,
"Mariam",
"[email protected]",
LocalDate.of(2000, Month.JANUARY, 5),
21
)
);
}
}
And the Student class
package com.example.demo.Student;
import java.time.LocalDate;
public class Student {
private long id;
private String name;
private String email;
private LocalDate dob;
private Integer age;
public Student(Long id, String name, String email, LocalDate dob, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.dob = dob;
this.age = age;
}
public Student(String name, String email, LocalDate dob, Integer age) {
this.name = name;
this.email = email;
this.dob = dob;
this.age = 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Student{"
"id=" id
", name='" name "'"
", email='" email "'"
", dob=" dob
", age=" age
"}";
}
}
when he runs the code in the video, the websites loads a list with a single student details in it. when I run it, it shows this:
I checked Stack Overflow and other websites for this problem, most say it is because the controller doesn't handle what to do in case of an error, but that's not the case here. and the others say it's because packaging hierarchy, i tried splitting Demo2Application into two, one for the Application one for the RestController and put them in hierarchical order where Demo2Application shows before RestController, still didn't work so I reversed the splitting back to what the video showed.
Here is the log when I run:
Truly Frustrated, will appreciate anything you have for me.
CodePudding user response:
maybe you can assign a value to the annotation GetMapping,like @GetMapping("/query")
.then by this url http://localhost:8080/query
to visit.