简体   繁体   中英

MyClass as @PathParam in Jersey resource method

In my Jersey resource i have:

@GET
@Path("/{dataType}/{dataSet}")
public Response search(
        @PathParam("dataType") String dataType,
        @PathParam("dataSet") String dataSet){
    ...
}

Instead of strings i want to use my own classes:

@GET
@Path("/{dataType}/{dataSet}")
public Response search(
        @PathParam("dataType") DataType dataType,
        @PathParam("dataSet") DataSet dataSet){
    ...
}

However DataSet is dependent on DataType(DataSet uses DataType in it's constructor). Is there a way to do this with Jersey?

You can either use Jersey's built-in transformation using a static fromString() method (see the Jersey documentation ), or use a custom provider to handle the path segments. For the latter, you will need a class something like this:

public class MyProvider extends PerRequestTypeInjectableProvider<Context, DataType> {
    @Context UriInfo uriInfo;

    public Injectable<DataType> getInjectable(ComponentContext componentCtx, Context ctx) {
        uri.getPathSegments();
        ...
    }
}

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