Use webTarget in Dynamic logic

A webTarget object invokes the URI of a specified destination directly from a dynamic logic.

Following are the two ways to create a webTarget object:

  • Destination code

  • Destination code with URI

Destination Code

A destination code refers to a predefined address key of a host or a complete URI. Following is an example of passing a destination code to create a webTarget object:

webTarget([destination-code])

A path() method adds an extra path to a destination that does not point to a complete URI.

Consider the following path at the destination:

http://[hostName]:[portNumber]/[api-context-root]

Following is an argument to the path() method:

webTarget().path("/[argument1]/[argument2]")

Hence, the complete URI looks like:

http://[hostName]:[portNumber]/[api-context-root]/[argument1]/[argument2]

The path() method concatenates the two paths from the method argument and destination code. This option involves encoding some characters, like a question mark(?) to %3F. The request returns a 404 not found error when a question mark(?) is treated as a normal character and hence encoded to some value.

Destination Code and URI

This option involves using the destination code along with a URI to create a webTarget object.

Following is an example:

webTarget([destination-code],URI.create("http://[hostName]:[portNumber]/[api-context-root]/[argument1]/[argument2]"))

The path() method can be avoided here as the second parameter contains the complete URI. You can use the complete path within the code to invoke the URI.

Following is an example of a path at the destination:

http://[hostName]:[portNumber]/[api-context-root]

Following is a URI to create webTarget object:

http://[hostName]:[portNumber]/[api-context-root]/[argument1]/[argument2]

This option helps in cases when a URI contains characters that must not be encoded. The specified destination with a complete URI creates a webTarget object and invokes the URI directly without encoding any character.