-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6. Path Variable.java
59 lines (39 loc) · 1.52 KB
/
6. Path Variable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//* 6. Path Variable
// We will create a REST end point that will return a List object to the client according to the path variable.
// This is StudentController file.
package com.springboot.first.app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; //importing
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.ArrayList;
@RestController
public class StudentController {
// http://localhost:8080/student
@GetMapping("/student")
public Student getStudent() {
return new Student("Ashish", "Singh");
}
@GetMapping("/students")
public List<Student> getStudents() {
List<Student> students = new ArrayList<>();
students.add(new Student("Ashish", "Singh"));
students.add(new Student("Prince", "Chaudhary"));
students.add(new Student("Agent", "Smith"));
students.add(new Student("Neo", "Anderson"));
students.add(new Student("Harvey", "Spector"));
return students;
}
// http://localhost:8080/student/Ashish/Singh
// {} is called URI template variable.
// We use @PathVariable to bind the URI template variable to the method arguments.
@GetMapping("/student/{firstName}/{lastName}") // We're using two path variables here.
public Student studentPathVarible(@PathVariable String firstName, @PathVariable String lastName) {
return new Student(firstName, lastName);
}
}
/*
Go to http://localhost:8080/student/Ashish/Singh
Output:
{"firstName":"Ashish","lastName":"Singh"}
*/