英文:
Why is the RequestMapper not displaying on Webpage?
问题
I am trying to use a RequestMapper and GetMapper to display a Java list using Spring Boot.
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
@GetMapping()
List<Student> getStudents() {
return List.of(
new Student(
1L,
"Joe",
22,
LocalDate.of(2001, Month.AUGUST, 15),
"Joe@gmail.com"
)
);
}
}
This code block should display the student's information, however, when I run the Spring server (localhost:8080/api/v1/student), I am led to a Whitelabel Error Page. Why are the mappers not getting and displaying my data?
英文:
I am trying to use a RequestMapper and GetMapper to display a Java list using Spring Boot.
@RestController
@RequestMapping(path = "api/v1/student")
public class StudentController {
@GetMapping()
List<Student> getStudents() {
return List.of (
new Student (
1L,
"Joe",
22,
LocalDate.of(2001, Month.AUGUST, 15),
"Joe@gmail.com"
)
);
}
}
This code block should display the student's information, however, when I run the Spring server (localhost:8080/api/v1/student), I am lead to a Whitelabel Error Page. Why are the mappers not getting and displaying my data?
答案1
得分: 1
看起来你的路径可能设置错了。
尝试将
@RequestMapping(path = "api/v1/student")
替换为
@RequestMapping(path = "/api/v1/student")
并将
@GetMapping()
List<Student> getStudents()
替换为
@GetMapping("/")
public List<Student> getStudents() {
然后你可以访问 localhost:8080/api/v1/student/
,应该可以正常工作。
英文:
It looks like your path may be set up wrong.
Try replacing
@RequestMapping(path = "api/v1/student")
with
@RequestMapping(path = "/api/v1/student")
and replace
@GetMapping()
List<Student> getStudents()
with
@GetMapping("/")
public List<Student> getStudents() {
Then you can visit localhost:8080/api/v1/student/
and it should work correctly.
答案2
得分: 0
I change a little your class as below and ran it with java 17 and spring boot 3.0.0:
import java.time.LocalDate;
import java.time.Month;
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/v1/student")
public class StudentController {
@GetMapping
public List<Student> getStudents() {
return List.of (
new Student (
1L,
"Joe",
22,
LocalDate.of(2001, Month.AUGUST, 15),
"Joe@gmail.com"
)
);
}
}
And the student class as:
public class Student {
private Long id;
private String name;
private int age;
private LocalDate date;
private String mail;
public Student(Long id, String name, int age, LocalDate date, String mail) {
this.id = id;
this.name = name;
this.age = age;
this.date = date;
this.mail = mail;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
Calling the URL http://localhost:8080/api/v1/student, I get a JSON response as below:
[{"id":1,"name":"Joe","age":22,"date":"2001-08-15","mail":"Joe@gmail.com"}]
英文:
I change a little your class as below and ran it with java 17 and spring boot 3.0.0:
import java.time.LocalDate;
import java.time.Month;
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/v1/student")
public class StudentController {
@GetMapping
public List<Student> getStudents() {
return List.of (
new Student (
1L,
"Joe",
22,
LocalDate.of(2001, Month.AUGUST, 15),
"Joe@gmail.com"
)
);
}
}
And the student class as:
public class Student {
private Long id;
private String name;
private int age;
private LocalDate date;
private String mail;
public Student(Long id, String name, int age, LocalDate date, String mail) {
this.id = id;
this.name = name;
this.age = age;
this.date = date;
this.mail = mail;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
}
Calling the url http://localhost:8080/api/v1/student I get a json response as below:
[{"id":1,"name":"Joe","age":22,"date":"2001-08-15","mail":"Joe@gmail.com"}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论