我有一个 Controller :
@RestController
@RequestMapping(value = UserRestController.REST_URL, produces =
MediaType.APPLICATION_JSON_VALUE)
public class UserRestController {
static final String REST_URL = "/customers";
@GetMapping
public List<User> getAll() {
return service.getAll();
}
}
它成功处理此类请求,如:
GET: /customers/
我想通过一些参数来获取用户。例如,电子邮件:
GET: /customers?email=someemail@gmail.
我尝试过:
@GetMapping("/")
public User getByEmail(@RequestParam(value = "email") String email) {
return super.getByEmail(email);
}
预计我会收到一个异常,因为“/”已经映射到 getAll 类上。 有什么办法可以解决这个问题吗?
最佳答案
@GetMapping
public Object get((@RequestParam(value = "email", required = false) String email) {
if (email != null && !email.isEmpty()) {
return super.getByEmail(email);
} else {
return service.getAll();
}
}
关于java - @RestController 中的动态 @RequestParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60300457/