0
def uri = new UriBuilder('http://someurl.com/api')

I want to append /$contacId/PhoneNumber to the above uri .

How Do I handle this case

deadpool
  • 61
  • 2
  • 9
  • `def` is not part of Java syntax. Please add `tag` of language which you are using. – Pshemo Sep 19 '18 at 16:09
  • As mentioned in other comment, correct the language tag. – Yug Singh Sep 19 '18 at 16:15
  • groovy is a subset of java!! everything is similar apart from the syntax .The point here is about the URI builder class which is common in both . – deadpool Sep 19 '18 at 16:22
  • https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/UriBuilder.html this is the api doc which is in Java. Anyhow thanks I fixed it . – deadpool Sep 19 '18 at 16:27
  • You linked to `UriBuilder` but your code is using `URIBuilder`. Java is case sensitive, is groovy not? If it is case sensitive then is it typo or did you mean some other class like http://javadox.com/org.codehaus.groovy.modules.http-builder/http-builder/0.6/groovyx/net/http/URIBuilder.html? – Pshemo Sep 19 '18 at 16:46
  • Typo* yup exactly UriBuilder of java which has a path method . – deadpool Sep 19 '18 at 21:26

3 Answers3

4

use path method

def uri = new URIBuilder('http://someurl.com/api');
 uri.path("/$contacId/PhoneNumber");
Issam EL-GUERCH
  • 408
  • 3
  • 8
2

One-Liner using UriBuilder:

import javax.ws.rs.core.UriBuilder

def contactId = 42

def uri = UriBuilder.fromUri("http://someurl.com/api").path("$contactId/PhoneNumber")

println uri

Output:

http://someurl.com/api/42/PhoneNumber

Test:

Copy-paste the code into https://groovy-playground.appspot.com/

zett42
  • 25,437
  • 3
  • 35
  • 72
-1
https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/UriBuilder.html

def uri = new URIBuilder('http://someurl.com/api')
String appendPath = "/contact/$contactId/PhoneNumber"
        uri.path += appendPath;
deadpool
  • 61
  • 2
  • 9