请求映射器为何未显示在网页上?

huangapple go评论67阅读模式
英文:

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 = &quot;api/v1/student&quot;)
public class StudentController {

    @GetMapping()
	List&lt;Student&gt; getStudents() {
		return List.of (
			new Student (
				1L,
				&quot;Joe&quot;,
				22,
				LocalDate.of(2001, Month.AUGUST, 15),
				&quot;Joe@gmail.com&quot;
			)
		);
	}
}

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 = &quot;api/v1/student&quot;)

with

@RequestMapping(path = &quot;/api/v1/student&quot;)

and replace

@GetMapping()
    List&lt;Student&gt; getStudents()

with

@GetMapping(&quot;/&quot;)
public List&lt;Student&gt; 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 = &quot;/api/v1/student&quot;)
public class StudentController {
@GetMapping
public List&lt;Student&gt; getStudents() {
return List.of (
new Student (
1L,
&quot;Joe&quot;,
22,
LocalDate.of(2001, Month.AUGUST, 15),
&quot;Joe@gmail.com&quot;
)
);
}
}

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"}]

huangapple
  • 本文由 发表于 2023年5月26日 08:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定