使用java的spring boot创建RESTful风格的服务端,并创建客户端程序。服务器端使用post注解资源接口用于增加一个学生的学号,姓名,出生年月等信息;使用get注解资源接口用于获得所有学生的信息;使用get注解的资源接口用于获得指定学号的学生信息。客户端访问服务,实现增加多个学生的信息,并输出所有学生的信息,以及指定学号的学生信息。
时间: 2024-01-22 13:21:16 浏览: 29
首先,我们需要创建一个Spring Boot项目,可以使用IDE工具如Eclipse或IntelliJ IDEA来创建项目。在创建项目时勾选Web依赖项。
接下来,我们需要创建一个学生实体类,包含学号、姓名和出生年月等信息。
```java
public class Student {
private int id;
private String name;
private LocalDate dateOfBirth;
// getters and setters
}
```
然后,我们需要创建一个控制器类,用于处理HTTP请求和响应。在控制器类中,我们需要定义三个方法,用于处理增加学生、获取所有学生和获取指定学生的请求。
```java
@RestController
public class StudentController {
private List<Student> students = new ArrayList<>();
@PostMapping("/students")
public ResponseEntity<Student> addStudent(@RequestBody Student student) {
students.add(student);
return ResponseEntity.ok(student);
}
@GetMapping("/students")
public ResponseEntity<List<Student>> getAllStudents() {
return ResponseEntity.ok(students);
}
@GetMapping("/students/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable int id) {
Optional<Student> student = students.stream().filter(s -> s.getId() == id).findFirst();
if (student.isPresent()) {
return ResponseEntity.ok(student.get());
} else {
return ResponseEntity.notFound().build();
}
}
}
```
在上述代码中,`@PostMapping`注解用于处理POST请求,`@GetMapping`注解用于处理GET请求,`@PathVariable`注解用于获取请求中的路径参数,`@RequestBody`注解用于获取请求体中的数据。
最后,我们需要创建一个客户端程序,用于向服务器发送HTTP请求并获取响应。我们可以使用Java的HttpClient类来发送HTTP请求。
```java
public class StudentClient {
private static final String BASE_URL = "http://localhost:8080";
public static void main(String[] args) throws IOException {
// add students
addStudent(new Student(1, "Alice", LocalDate.of(2000, 1, 1)));
addStudent(new Student(2, "Bob", LocalDate.of(2001, 2, 2)));
// get all students
List<Student> students = getAllStudents();
students.forEach(System.out::println);
// get student by id
Student student = getStudentById(1);
System.out.println(student);
}
private static void addStudent(Student student) throws IOException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/students"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(new ObjectMapper().writeValueAsString(student)))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
private static List<Student> getAllStudents() throws IOException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/students"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(response.body(), new TypeReference<List<Student>>() {});
}
private static Student getStudentById(int id) throws IOException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/students/" + id))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(response.body(), Student.class);
}
}
```
在上述代码中,我们使用`HttpClient`类来发送HTTP请求,使用`ObjectMapper`类来序列化和反序列化JSON数据。
现在,我们可以启动服务器和客户端程序,并测试增加学生、获取所有学生和获取指定学生的功能。