简体   繁体   中英

how to escape, escape character('\') backslash in java microservices

I am using microservices using spring boot in my application, I use RestTemplate to call other component services from "org.springframework.web.client.RestTemplate".
I have a common service from my component which calls other components to fetch data.

Whenever my search text has a backslash like "test\\" my API call fails. After debugging I came to the conclusion that till restTemplete.exchange API is not failing and I am not getting any requests in other component API methods.

// restTemplete.exchange(serviceUrl, HttpMethod.POST, request, responseType);

If I put 'text\\t' API call is successful and control is reaching the other component not sure if I am missing anything on this. How do I escape the escape character?

I have tried text.replace("\\\\", "\\\\\\\\"); , but still no success.

You can use something called UriComponentsBuilder. https://stackoverflow.com/a/28183190/8278152

Building your url using above component should help you.

The better approach to utilize the REST API's of the microservices, registered under the same architecture, is to use Feign Client. For more details click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM