skip to Main Content

I get a very basic error when trying to create a very basic Spring RestController. I must be missing something very basic here.

To reproduce:


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WeirdErrorController {
    @GetMapping("/test")
    public String tryIt(
        @RequestParam(value = "a") String a,
        @RequestParam(value = "b") String b
    ) {
        return ("Here are the args: " + a + ", " + b);
    }
}

  • Run it with ./gradlew bootRun.

  • Make a request:

curl http://localhost:8080/test?a=foo&b=bar

Get this error:

Required request parameter 'b' for method parameter type String is not present]

I am running this on Ubuntu 22.04 in WSL2 in Win 11.
I have tested this in multiple configurations with the same result. Tried both Java 17 and Java 21. Is this a WSL2 related bug?

2

Answers


  1. Chosen as BEST ANSWER

    As pointed out by @feel-free the issue was in my curl command. I need to quote my url:

    curl "http://localhost:8080/test?a=foo&b=bar"
    

  2. By default @RequestParam has a required flag that equals to true. You can make it optional using @RequestParam(required = false).

    I recommend to look into details at https://www.baeldung.com/spring-request-param

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search