Required String parameter 'id' is not present [duplicate]

  • A+

This question already has an answer here:

For some reason, I can't get the value of id when I enter this url: http://localhost:8080/1. Why's that?

This is my controller:

package com.example.demo.controller;  import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;  @RestController public class IndexController {      private UserService userService;      public IndexController(UserService userService) {         this.userService = userService;     }      @GetMapping     String index() {         return "hallo maestro";     }      @GetMapping("/{id}")     ResponseEntity user(@RequestParam String id) {         return ResponseEntity.ok().body(id);         //return userService.getUserJson(id);     }      @PostMapping     ResponseEntity create(User user) {         return userService.create(user);     }      @PutMapping     ResponseEntity update(User user) {         return userService.update(user);     } } 

Also changing the annotation to GetMapping("/user/{id}") and the URL to http://localhost:8080/user/1 doesn't change anything.


You should use @PathVariable instead of @RequestParam.

See @PathVariable vs @RequestParam for a comparison between the two.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: