Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Spring Config for RestTemplate to use contextPath from Consul
* Workaround to let RestTemplate use the contextPath from Consul when calling URLs
* (instead of only using the host and port)
* See
* Put this in any spring config class
LoadBalancerRequestTransformer consulContextPathTransformer() {
new LoadBalancerRequestTransformer() {
HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
String contextPath = instance?.server?.metadata?.contextPath
contextPath ? new ConsulContextPathServiceRequestWrapper(request, contextPath) : request
class ConsulContextPathServiceRequestWrapper extends ServiceRequestWrapper {
final String contextPath
ConsulContextPathServiceRequestWrapper(ServiceRequestWrapper serviceRequestWrapper, String contextPath) {
super(serviceRequestWrapper.request, serviceRequestWrapper.instance, serviceRequestWrapper.loadBalancer)
this.contextPath = contextPath
URI getURI() {
URI origURI = super.getURI()
new URIBuilder(origURI).setPath(contextPath + origURI.path).build()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment