使用java的spring boot创建RESTful风格的服务端,并创建客户端程序。场景:用接口添加,获取所有学生信息或者指定学生信息,客户端添加学生信息并输出所有或指定学生信息
时间: 2023-08-21 22:07:37 浏览: 53
好的,我可以为您提供一些基本的代码示例。
首先,我们需要创建一个Spring Boot应用程序,该程序将提供RESTful API以添加和获取学生信息。下面是一个简单的示例:
```java
@RestController
@RequestMapping("/students")
public class StudentController {
private List<Student> students = new ArrayList<>();
@GetMapping
public List<Student> getAllStudents() {
return students;
}
@GetMapping("/{id}")
public Student getStudentById(@PathVariable int id) {
for (Student student : students) {
if (student.getId() == id) {
return student;
}
}
return null;
}
@PostMapping
public void addStudent(@RequestBody Student student) {
students.add(student);
}
}
```
在这个示例中,我们创建了一个`StudentController`类,该类包含了三个RESTful API:`getAllStudents`、`getStudentById`和`addStudent`。`getAllStudents`方法返回所有学生信息,`getStudentById`方法根据ID返回指定学生信息,`addStudent`方法用于添加新的学生信息。
接下来,我们需要创建一个客户端程序,该程序将使用上述API添加和获取学生信息。下面是一个简单的示例:
```java
public class StudentClient {
private static final String BASE_URL = "http://localhost:8080/students";
public static void main(String[] args) {
// 添加新学生
addStudent(new Student(1, "张三", 20, "男"));
addStudent(new Student(2, "李四", 21, "女"));
// 获取所有学生信息
List<Student> students = getAllStudents();
System.out.println("所有学生信息:");
for (Student student : students) {
System.out.println(student);
}
// 获取指定学生信息
int id = 1;
Student student = getStudentById(id);
if (student != null) {
System.out.println("ID为" + id + "的学生信息:" + student);
} else {
System.out.println("找不到ID为" + id + "的学生信息。");
}
}
private static void addStudent(Student student) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(BASE_URL, student, Void.class);
}
private static List<Student> getAllStudents() {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Student>> response = restTemplate.exchange(
BASE_URL,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<Student>>() {});
return response.getBody();
}
private static Student getStudentById(int id) {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Student> response = restTemplate.getForEntity(
BASE_URL + "/" + id,
Student.class);
return response.getBody();
}
}
```
在这个示例中,我们创建了一个`StudentClient`类,该类使用`RestTemplate`类与上述API进行交互。`addStudent`方法用于添加新的学生信息,`getAllStudents`方法用于获取所有学生信息,`getStudentById`方法用于根据ID获取指定学生信息。
当我们运行`StudentClient`程序时,它将向API添加两个新的学生信息,然后输出所有学生信息和指定学生信息。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)