2

Is there any reason that explains why I get a compilation error (Body parameters cannot be used with form parameters) when declare a feign interface method like this:

1) foo(@PathVariable("pathVariableName") String pathVariable, 
       @RequestBody List<String> anyVariable)

but everything works fine when I use this (switching the parameters order):

2) foo(@RequestBody List<String> anyVariable, 
       @PathVariable("pathVariableName") String pathVariable)

In the current project all other feign methods work just fine in the former form. I don't know if this information is important, but this is the only one in which I try to use a list annotated with @RequestBody parameter.

Does the order of the parameters matters here like this answer? In this case, in which situations should I pay attention to the parameters order? Feign documentation seems to use it the way I have the problem.

By the way: I stopped my project for a few minutes to post this here, before trying to run the application, just after getting a successful compilation. I'm not really sure the second way above works fine at runtime. It's just passed by the compilation phase without errors.

EDIT: This is not really a compilation error. The error occurs during the startup process of the springboot application.

Community
  • 1
  • 1
julianobrasil
  • 8,954
  • 2
  • 33
  • 55

1 Answers1

6

You need to check your url template string. If it doesn't contain {pathVariableName} in the url, @PathVariable("pathVariableName") will be treated as form parameter. In feign, form parameters can't be used with a body parameter (@RequestBody) in the same method. That's why the 1) throws exception.

In the second case, when validation @RequestBody annotation, feign doesn't know the existence of the second parameter that will be treated as form parameter. So it doesn't throw any exception now. I'm not sure that is an intentional behavior or not, but probably it could be a validation bug.

yongsung.yoon
  • 5,489
  • 28
  • 32
  • The path variable is in url. It left us one open question: if this seems like it's a validation bug, why did it occur just with this particular case. I have a lot of feign method calls in this projects and like 20%-30% of them make a post with PathVariable and RequestBody. None of them complains about using PathVariable as the first parameter at the compilation time. I think what you said makes sense, about not using the two annotations together. But, as I said, I haven't tested them yet (I'll start the testing phase today). I intend to post the results here as soon as I finish the POST tests. – julianobrasil May 04 '17 at 10:15
  • 1
    Does your url string have path parameter place holder with `{xxx}` in the error case ? What I found out from feign source is that feign treats PathVariable as Form parameter if there is no PathVariable placeholder in url string. And Form parameter and RequestBody can't not be used together. – yongsung.yoon May 04 '17 at 10:21
  • my mistake. Ashamed of myself. I mistyped "appID" in PathVariable. I had typed "appId" (lowercase "d"). Very very sorry for taking your time because of this silly mistake. – julianobrasil May 04 '17 at 18:47
  • Your answer solved my issue. Thanks. – Sayed Sep 20 '21 at 19:34