简体   繁体   English

MyClass作为Jersey资源方法中的@PathParam

[英]MyClass as @PathParam in Jersey resource method

In my Jersey resource i have: 在我的Jersey资源中,我有:

@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). 但是,DataSet依赖于DataType(DataSet在其构造函数中使用DataType)。 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. 您可以通过静态fromString()方法使用Jersey的内置转换(请参见Jersey文档 ),也可以使用自定义提供程序来处理路径段。 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();
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM