SpringBoots获取请求findByName不起作用

时间:2019-05-12 19:19:13

标签: java api spring-boot get

我试图用springboots创建获取请求,但是我可以获取所有用户请求,但是我想通过姓氏发出获取请求。 所以我尝试使用@requestparam,但是它没有显示任何信息。 我真的不知道该怎么解决

用户

public class User {

private String name;
private String lastname;
private String email;

public User(String name, String lastname, String email) {
    super();
    this.name = name;
    this.lastname = lastname;
    this.email = email;
}


public String getName() {
    return name;
}

public String getLastname() {
    return lastname;
}

public String getEmail() {
    return email;
}


public void setName(String name) {
    this.name = name;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public void setEmail(String email) {
    this.email = email;
}



@Override
public String toString() {
    return String.format("User[name=%s,lastname=%s,email=%s]", name, lastname, email);

}
}

UserService

@Component
public class UserService {

private  static List<User> users =new ArrayList<>();

static {
    users.add(new User("jj","kim","kk@gmail.com"));
    users.add(new User("cc","smith","cc@gmail.com"));
    users.add(new User("ss","jake","ss@gmail.com"));

}

public  List<User>findAll(){
return users;
}

public  User save(User user){
    users.add(user);
    return user;
}



public User findByLastName(String lastname){
    for(User user:users){
        if(user.getLastname()==lastname){
            return user;

        }
    }
    return  null;
    }

 }

UserData

@RestController

public class UserData {

@Autowired
private UserService service;


@GetMapping("/users")
public List<User> getAllUsers(){
    return service.findAll();

}


@GetMapping("/users/{lastname}")
public List<User> getUser(@RequestParam(value ="lastname", required = true)String lastname){

    return service.findByLastName(lastname);

     }

  }

0 个答案:

没有答案