简体   繁体   English

给定的 id 不能是 null;。 嵌套异常是 java.lang.IllegalArgumentException

[英]The given id must not be null!; nested exception is java.lang.IllegalArgumentException

i'm creating a simple crud with spring boot.我正在用 spring 引导创建一个简单的 crud。 All function well but I have a little problem with the method findOne(Long id)所有 function 都很好,但我对方法 findOne(Long id) 有一点问题
in postman when i put this url: http://localhost:8080/api/user/id/?id=13 , i get this exception:在 postman 当我把这个 url: http://localhost:8080/api/user/id/?id=13 ,我得到这个异常:

" error ": "Internal Server Error", " error ": "内部服务器错误",
" exception ": "org.springframework.dao.InvalidDataAccessApiUsageException", "异常": "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,", " message ": "给定的 id 不能是 null;;嵌套异常是java.lang.IllegalArgumentException: 给定的 id 不能是 Z37A6259CC0C1DAE299A7866489DFF0,

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 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);
    }
}

You have your mapping set as a URL path variable您将映射设置为 URL 路径变量

@GetMapping("/user/id/{id}")

but the URL you tried has a query parameter: ?id=13但是您尝试的 URL 有一个查询参数: ?id=13

Try using: http://localhost:8080/api/user/id/13尝试使用: http://localhost:8080/api/user/id/13

Here is a good comparison of the two on stackoverflow这是stackoverflow上两者的一个很好的比较

The URL is incorrect. URL 不正确。 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,在这种情况下,您应该点击 localhost:8080/api/user/id/3 而不是在 postman 中点击 localhost:8080/api/user/id/?id=13,

But if you were following REST standards, a better URL would look like this (no need to have the "id" in the URL).但是,如果您遵循 REST 标准,则更好的 URL 看起来像这样(不需要在 URL 中包含“id”)。 localhost:8080/api/user/3本地主机:8080/api/user/3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flyway 嵌套异常是 java.lang.IllegalArgumentException: Name must not be null - Flyway nested exception is java.lang.IllegalArgumentException: Name must not be null 线程“ main”中的异常java.lang.IllegalArgumentException:in不能为null - Exception in thread “main” java.lang.IllegalArgumentException: in must not be null java.lang.IllegalArgumentException:方法不能为null - java.lang.IllegalArgumentException: Method must not be null java.lang.IllegalArgumentException:上下文不得为null - java.lang.IllegalArgumentException: Context must not be null java.lang.IllegalArgumentException:目标不能为空 - java.lang.IllegalArgumentException: Target must not be null 获取异常:java.lang.IllegalArgumentException:无法添加到布局:约束必须为字符串(或为null) - getting Exception : java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null) JAXB给我:java.lang.IllegalArgumentException:参数不能为null - JAXB gives me: java.lang.IllegalArgumentException: is parameter must not be null java.lang.IllegalArgumentException:[断言失败]-此参数是必需的; 它不能为空 - java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null 线程“ main”中的异常java.lang.IllegalArgumentException:绑定必须为正 - Exception in thread “main” java.lang.IllegalArgumentException: bound must be positive 线程“main”中的异常java.lang.IllegalArgumentException:n必须为正数 - Exception in thread “main” java.lang.IllegalArgumentException: n must be positive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM