URL encoding using the new Spring UriComponentsBuilder

后端 未结 3 803
清歌不尽
清歌不尽 2020-11-30 08:31

I\'m attempting to use spring\'s UriComponentsBuilder to generate some urls for oauth interaction. The query parameters include such entities as callback urls and parameter

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 09:14

    Try to scan the UriComponentsBuilder doc, there is method named build(boolean encoded)

    Sample code 1:

    UriComponents uriComponents = UriComponentsBuilder.fromPath("/path1/path2").build(true);
    

    Here is my sample code 2:

    UriComponents uriComponents = UriComponentsBuilder.newInstance()
                .scheme("https")
                .host("my.host")
                .path("/path1/path2").query(parameters).build(true);
    
    URI uri= uriComponents.toUri();
    
    ResponseEntity responseEntity = restTemplate.exchange(uri,
                HttpMethod.GET, entity, typeRef);
    

提交回复
热议问题