0

i'm creating a simple crud with spring boot. All function well but I have a little problem with the method findOne(Long id)
in postman when i put this url : http://localhost:8080/api/user/id/?id=13 , i get this exception :

"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!",

here is my code :
Repository

@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.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);
    }
}
ezedin
  • 11
  • 1
  • 2

2 Answers2

0

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

The.Laughing.Man
  • 479
  • 4
  • 11
0

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

Chris
  • 839
  • 2
  • 10
  • 33