Generated Swagger REST client does not handle + character correctly for query parameter

后端 未结 1 592
死守一世寂寞
死守一世寂寞 2021-01-22 14:12

I have this Spring REST controller method:

@ApiOperation(\"My method\")
@RequestMapping(method = RequestMethod.POST, value = \"/myMethod\")
public void myMethod(         


        
相关标签:
1条回答
  • 2021-01-22 14:30

    A + usually means a space in url params. This is a standard. Your url gets generated on below line of code

    final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
    

    And + is not encoded as it is a valid value to have which will be then later converted to space at the receiving end. Now when you try use %2B the above line of code sees that you have a un-encoded % character and it converts it to %252B.

    When you receive this back at Spring it converts it back to %2B for you. One way to solve the issue is to send encoded values yourself. So you will change

     final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
    

    to

    final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build(true).toUri());
    

    And then call the API method as

    client.myMethod("tarun%2blalwani")
    

    And now spring will receive tarun+lalwani as shown below

    0 讨论(0)
提交回复
热议问题