英文:
The given id must not be null!; nested exception is java.lang.IllegalArgumentException
问题
我正在使用Spring Boot创建一个简单的CRUD应用。所有功能都正常,但在方法findOne(Long id)中遇到了一个小问题。
在Postman中,当我使用以下URL:http://localhost:8080/api/user/id/?id=13 时,我收到以下异常:
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!",
以下是我的代码:
**Repository**
```java
@SuppressWarnings("unused")
@Repository
public interface UserRepository extends JpaRepository<Utilisateur, Long> {
}
Service
public interface UserService {
Utilisateur save(Utilisateur utilisateur);
List<Utilisateur> findAll();
Utilisateur findOne(Long id);
}
ServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
public UserServiceImpl() {
}
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public Utilisateur save(Utilisateur utilisateur) {
return userRepository.save(utilisateur);
}
@Override
public List<Utilisateur> findAll() {
return userRepository.findAll();
}
public Utilisateur findOne(Long id) {
return userRepository.findById(id).orElse(null);
}
}
Controller
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
public class UserController {
@Autowired
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users")
public List<Utilisateur> getUsers() {
return userService.findAll();
}
@GetMapping("/user/id/{id}")
public ResponseEntity<Utilisateur> getUser(@PathVariable Long id) {
Utilisateur utilisateur = userService.findOne(id);
return ResponseEntity.ok().body(utilisateur);
}
@PostMapping("/user")
public ResponseEntity<Utilisateur> saveUser(@RequestBody Utilisateur utilisateur) throws URISyntaxException {
Utilisateur result = userService.save(utilisateur);
return ResponseEntity.created(new URI("/api/user/" + result.getId()))
.body(result);
}
@PutMapping("/user")
public ResponseEntity<Utilisateur> updateUser(@RequestBody Utilisateur utilisateur) throws URISyntaxException {
Utilisateur result = userService.save(utilisateur);
return ResponseEntity.ok().body(result);
}
}
<details>
<summary>英文:</summary>
i'm creating a simple crud with spring boot. All function well but I have a little problem with the method findOne(Long id)<br>
in postman when i put this url : *http://localhost:8080/api/user/id/?id=13* , i get this exception :
*"**error**": "Internal Server Error",<br>
"**exception**": "org.springframework.dao.InvalidDataAccessApiUsageException",<br>
"**message**": "The given id must not be null!; nested exception is* *java.lang.IllegalArgumentException: The given id must not be null!",*
here is my code :<br>
**Repository**<br>
@SuppressWarnings("unused")
@Repository
public interface UserRepository extends JpaRepository<Utilisateur, Long> {
}
<br>
**Service**<br>
public interface UserService {
Utilisateur save(Utilisateur utilisateur);
List<Utilisateur> findAll();
Utilisateur findOne(Long id);
}
**ServiceImpl**
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserRepository userRepository;
public UserServiceImpl() {
}
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public Utilisateur save(Utilisateur utilisateur) {
return userRepository.save(utilisateur);
}
@Override
public List<Utilisateur> findAll() {
return userRepository.findAll();
}
public Utilisateur findOne(Long id) {
return userRepository.findOne(id);
}
}
**Controller**
@RestController
@RequestMapping("/api")
@CrossOrigin(origins="http://localhost:4200",allowedHeaders="*")
public class UserController {
@Autowired
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
public UserController() {
super();
// TODO Auto-generated constructor stub
}
@GetMapping("/users")
public List<Utilisateur> getUsers() {
return userService.findAll();
}
@GetMapping("/user/id/{id}")
public ResponseEntity<Utilisateur> getUser(@PathVariable Long id) {
Utilisateur utilisateur = userService.findOne(id);
return ResponseEntity.ok().body(utilisateur);
}
/*
@DeleteMapping("/user/{id}")
public Boolean deleteUser(@PathVariable Long id) {
userRepository.delete(id);
return true;
} */
@PostMapping("/user")
public ResponseEntity<Utilisateur> saveUser(@RequestBody Utilisateur utilisateur) throws URISyntaxException {
Utilisateur result = userService.save(utilisateur);
return ResponseEntity.created(new URI("/api/user/" + result.getId()))
.body(result);
}
@PutMapping("/user")
public ResponseEntity<Utilisateur> updateUser(@RequestBody Utilisateur utilisateur) throws URISyntaxException {
Utilisateur result = userService.save(utilisateur);
return ResponseEntity.ok().body(result);
}
}
</details>
# 答案1
**得分**: 0
你已将映射设置为URL路径变量
@GetMapping("/user/id/{id}")
但是您尝试的URL具有查询参数:?id=13
请尝试使用:http://localhost:8080/api/user/id/13
这里是两者的一个很好的比较,详情请参阅[stackoverflow][1]。
[1]: https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam
<details>
<summary>英文:</summary>
You have your mapping set as a URL path variable
@GetMapping("/user/id/{id}")
but the URL you tried has a query parameter: ?id=13
Try using: http://localhost:8080/api/user/id/13
Here is a good comparison of the two on [stackoverflow][1]
[1]: https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam
</details>
# 答案2
**得分**: 0
URL有误。您在代码中将其设置为路径变量。在这种情况下,不应该在Postman中访问localhost:8080/api/user/id/?id=13,而应该访问localhost:8080/api/user/id/3,
但是如果您遵循REST标准,更好的URL应该是这样的(URL中不需要包含“id”)。localhost:8080/api/user/3
<details>
<summary>英文:</summary>
The URL is incorrect. You have set it up as a path variable in your code. In which case, instead of hitting localhost:8080/api/user/id/?id=13 in postman, you should hit localhost:8080/api/user/id/3 instead,
But if you were following REST standards, a better URL would look like this (no need to have the "id" in the URL). localhost:8080/api/user/3
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论