简体   繁体   中英

Variable length endpoints in jax-rs/microprofile client with Quarkus

I'm facing the following issue and have been able to find a proper fix.

As a use case example, let's imagine a Rest client that fetches a json object from a server, where the request is the path to the object. This api does not accept query parameters nor bodies, and no catalogue is available.

say that I have the following RestClient:

@Path("/json")
@RegisterRestClient(configKey="json-api")
public interface JsonService {

    @GET
    @Path("/{MyVariableLengthEndpoint}")
    Response getJson(@PathParam("MyVariableLengthEndpoint") ????? endpoint);
}

Examples of requests could be:

  • /json/employees/Dwight/jobs/assistantRegionalManager/salary
  • /json/games/theLastOfUs/rating

Passing a string with / characters gets encoded with %. To bypass this, I've tried:

  • Using the @Encoded annotation
  • Adding a regex in the pathParameter {MyVariableLengthEndpoint: .*}
  • Passing a List<PathSegment>

None of those worked.

Is there a proper way to do this?

You should use Regex in your @Path definition, something like this:

@GET
@Path(“/{varPath : .+}”)
Response getJson(@PathParam(“varPath”) String endpoint);

This will match anything that comes after /json.

For more info search: “JAXRS path Regex”, on Google.

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