This commit is contained in:
many2many 2024-11-05 11:28:53 +08:00
parent c446e028fd
commit 529d237591
5 changed files with 141 additions and 42 deletions

View File

@ -1,26 +0,0 @@
package com.lk.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping
public String hello() {
return "Hello World";
}
@GetMapping("/json")
public Map<String,String> json() {
Map<String,String> data = new HashMap<>();
data.put("message","Hello World");
return data;
}
}

View File

@ -1,6 +1,8 @@
package com.lk.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

View File

@ -1,15 +1,12 @@
package com.lk.demo;
public class User {
private Long id;
private String name;
private String password;
private String email;
private String phone;
public User(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
}
private Integer age;
public String getName() {
return name;
@ -34,4 +31,28 @@ public class User {
public void setPhone(String phone) {
this.phone = phone;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -1,10 +1,9 @@
package com.lk.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@ -12,10 +11,82 @@ import java.util.List;
public class UserController {
@Autowired
private UserService userService;
UserService userService;
/**
* 获取指定ID的用户信息
* Method: GET
* URL: /users/{id}
* 输入参数:
* - @PathVariable Long id: 用户ID
* 返回数据结构:
* - User对象包含id, name, age
*/
@GetMapping("/{id}")
public User getUser(@PathVariable Long id){
System.out.println("Got id from http request: " + id);
return userService.getUser(id);
}
/**
* 根据名称分页查询用户列表
* Method: GET
* URL: /users?name={name}&page={page}&pageSize={pageSize}
* 输入参数:
* - @RequestParam String name: 用户名称
* - @RequestParam int page: 当前页码
* - @RequestParam int pageSize: 每页大小
* 返回数据结构:
* - List<User> 用户列表每个User对象包含id, name, age
*/
@GetMapping("")
public List<User> index() {
return userService.getUsers();
public List<User> findUsers(@RequestParam("name") String name, @RequestParam int page, @RequestParam int pageSize){
System.out.println("Got name: " + name+ " page: " + page+ " pageSize: " + pageSize);
return userService.queryUsers();
}
/**
* 创建新用户
* Method: POST
* URL: /users
* 输入参数:
* - @RequestBody User user: 用户对象包含name, age, password,email
* 返回数据结构:
* - User对象包含id, name, age, password,email
*/
@PostMapping
public User createUser(@RequestBody User user){
System.out.println("Got name: "+user.getName()+ "pass:" +user.getPassword());
return userService.createUser(user);
}
/**
* 更新指定ID的用户信息
* Method: PUT
* URL: /users/{id}
* 输入参数:
* - @PathVariable Long id: 用户ID
* - @RequestBody User user: 用户对象包含name, age, password
* 返回数据结构:
* - User对象包含id, name, age, password
*/
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User user){
System.out.println("Got id: "+id+ "name: "+user.getName()+ " pass:" +user.getPassword()+" age: "+user.getAge());
return userService.updateUser(id, user);
}
/**
* 删除指定ID的用户
* Method: DELETE
* URL: /users/{id}
* 输入参数:
* - @PathVariable Long id: 用户ID
* 返回数据结构:
* - String "ok" 表示删除成功
*/
@DeleteMapping("/{id}")
public String deleteUser(@PathVariable Long id){
System.out.println("delete id: "+id);
return "ok";
}
}

View File

@ -1,17 +1,48 @@
package com.lk.demo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserService {
public List<User> getUsers(){
public User getUser(Long id){
User user = new User();
user.setId(id);
user.setName("lk");
user.setAge(18);
return user;
}
public List<User> queryUsers(){
List<User> users = new ArrayList<>();
users.add(new User("mike","mike@mail.com", "188111111"));
users.add(new User("john","john@mail.com", "133222222"));
users.add(new User("tom","tom@mail.com", "199222222"));
User u1 = new User();
u1.setName("John");
u1.setEmail("john@gmail.com");
u1.setPhone("123456789");
users.add(u1);
User u2 = new User();
u2.setName("Mike");
u2.setEmail("mike@gmail.com");
u2.setPhone("123456789");
users.add(u2);
return users;
}
public User createUser(User user){
user.setId(22L);
return user;
}
public User updateUser(Long id, User user){
user.setId(id);
return user;
}
}