0

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?

  • What is the exact error and stacktrace? – Unmitigated May 26 '23 at 00:32
  • Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Fri May 26 13:38:06 EDT 2023 There was an unexpected error (type=Not Found, status=404). – Brian Liao May 26 '23 at 17:56

2 Answers2

1

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.

MattArgo
  • 21
  • 5
  • Also make sure Student class is abled to serialize to json successfully. e.g. It has setter/getter methods, LocalDate could be serialized, etc. – Stone May 26 '23 at 14:18
  • Thank you for the answer, but it is still not working – Brian Liao May 26 '23 at 17:56
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"}]