Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly manage PathVariables with spring

Tags:

java

spring

I'm hoping this isn't too simple of a question. I'm new to the java web services world and cant seem to get access to my PathVariables in my controller. I'm using STS and it's not complaining that my syntax is wrong.

So more important than the correct answer, I'd really like to know why this isn't working.

Here's some example code that I cant get to work:

@RestController
public class TestController {

    @RequestMapping("/example")
    public String doAThing(
        @PathVariable String test
    ) throws MessagingException {
        return "Your variable is " + test;
    }
}

If I run a curl against it like so:

curl http://localhost:8080/example?test=foo

I receive the following response:

{"timestamp":1452414817725,"status":500,"error":"Internal Server Error","exception":"org.springframework.web.bind.MissingPathVariableException","message":"Missing URI template variable 'test' for method parameter of type String","path":"/example"}

I know that I have everything else wired up correctly, other controllers work.

I feel like I must be missing some fundamental principal here.

Thanks in advance.

like image 336
Bryan Avatar asked Dec 28 '25 16:12

Bryan


2 Answers

Spring support different ways how to map stuff from the url to method parameters: request parameters and path variables

  • Request Parameters are taken from url-query parameters (and request body, for example in http-POST requests). The annotation to mark the java method parameter that should take its value from a request parameter is @RequestParam

  • Path Variables (somtimes called path templates) are parts of the url-path. The annotation to mark the java method parameter that should take its value from a request parameter is @PathVariable

Have a look at this answer of mine, for an example an links to the Spring Reference.

So what your problem is: you want to read a Request Parameter (from the url-query part), but used the annotation for the Path Variables. So you have to use @RequestParam instead of @PathVariable:

@RestController
public class TestController {

    @RequestMapping("/example")
    public String doAThing(@RequestParam("test") String test) throws MessagingException {
        return "Your variable is " + test;
    }
}
like image 52
Ralph Avatar answered Dec 30 '25 05:12

Ralph


If you are using path variable, then it has to be part of the URI. As you have not mentioned in the URI but used in the method arguments, spring tries to find out and assign this value from the path URI. But this path variable is not there in the path URI , therefore throwing MissingPathVariableException.

This should work.

@RestController
public class TestController {

@RequestMapping("/example/{test}")
public String doAThing(
    @PathVariable String test
) throws MessagingException {
    return "Your variable is " + test;
}
}

And your curl request would be like

curl http://localhost:8080/example/foo
//here the foo can be replace with other string values
like image 34
prem kumar Avatar answered Dec 30 '25 04:12

prem kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!