gpt4 book ai didi

java - 将@PathParam 传递给 Jersey 中的子资源定位器类

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:26 25 4
gpt4 key购买 nike

我想这样调用我的 api 端点:

http://.../companies/1/employees

并使用代码 1 检索公司的员工。我有以下代码:

@Path("companies")
public class CompanyResource {

@Context
ResourceContext resourceContext;

@GET
@Path("{idCompany}/employees")
public EmployeeResource getEmployees() {
return resourceContext.getResource(EmployeeResource.class);
}

}

@Path("/employees")
public class EmployeeResource {

@PathParam("idCompany")
String idCompany;

@GET
public List<Employee> getEmployees() {
// here "idCompany" is null
//some code
}
}

但是路径参数为空。我究竟做错了什么?有没有更正确的方法来做到这一点?

最佳答案

这里总结了 JAX-RS 2.0 可能的解决方案:

  • 使用构造函数请求范围内的子资源:

    @Path("companies")
    public class CompanyResource {

    @Path("{idCompany}/employees")
    public EmployeeResource getEmployees(@PathParam("idCompany") String companyId) {
    return new EmployeeResource(companyId);
    }
    }

    public class EmployeeResource {

    private String companyId

    public EmployeeResource(String companyId) {
    this.companyId = companyId;
    }

    @GET
    public List<Employee> getEmployees() {
    //some code
    }
    }

    缺点:

    • 没有依赖注入(inject)
  • 使用 ResourceContext#initResource(Class) 请求范围内的子资源:

    @Path("companies")
    public class CompanyResource {

    @Context
    private ResourceContext resourceContext;

    @GET
    @Path("{idCompany}/employees")
    public EmployeeResource getEmployees(@PathParam("idCompany") String companyId) {
    EmployeeResource employeeResource = new EmployeeResource(companyId);
    return resourceContext.initResource(employeeResource);
    }
    }

    public class EmployeeResource {

    @Context
    private Request request;

    private String companyId

    public EmployeeResource(String companyId) {
    this.companyId = companyId;
    }

    @GET
    public List<Employee> getEmployees() {
    //some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330
  • 使用 ResourceContext#getResource(Class) 请求范围内的子资源:

    @Path("companies")
    public class CompanyResource {

    @Context
    private ResourceContext resourceContext;

    @GET
    @Path("{idCompany}/employees")
    public EmployeeResource getEmployees(@PathParam("idCompany") String companyId) {
    EmployeeResource employeeResource = resourceContext.getResource(EmployeeResource.class);
    employeeResource.setCompanyId(companyId);
    return employeeResource;
    }
    }

    public class EmployeeResource {

    @Context
    private Request request;

    private String companyId

    public setCompanyId(String companyId) {
    this.companyId = companyId;
    }

    @GET
    public List<Employee> getEmployees() {
    //some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330
  • 使用 @PathParam 请求范围内的子资源作为字段:

    @Path("companies")
    public class CompanyResource {

    @Context
    private ResourceContext resourceContext;

    @GET
    @Path("{idCompany}/employees")
    public EmployeeResource getEmployees() {
    return resourceContext.getResource(EmployeeResource.class);
    }
    }

    public class EmployeeResource {

    @Context
    private Request request;

    @PathParam("idCompany")
    private String companyId;

    @GET
    public List<Employee> getEmployees() {
    // some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330
  • 请求范围内的子资源,返回类型为 Class<T> :

    @Path("companies")
    public class CompanyResource {

    @GET
    @Path("{idCompany}/employees")
    public Class<EmployeeResource> getEmployees() {
    return EmployeeResource.class;
    }
    }

    public class EmployeeResource {

    @Context
    private Request request;

    @PathParam("idCompany")
    private String companyId;

    @GET
    public List<Employee> getEmployees() {
    // some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330
  • 使用 @PathParam 请求范围内的子资源作为方法参数:

    @Path("companies")
    public class CompanyResource {

    @Context
    private ResourceContext resourceContext;

    @GET
    @Path("{idCompany}/employees")
    public Class<EmployeeResource> getEmployees() {
    return resourceContext.getResource(EmployeeResource.class);
    }
    }

    public class EmployeeResource {

    @Context
    private Request request;

    @GET
    public List<Employee> getEmployees(@PathParam("idCompany") String companyId) {
    // some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330
  • 带有 @PathParam 的单例子资源作为方法参数:

    @Path("companies")
    public class CompanyResource {

    @Context
    private ResourceContext resourceContext;

    @GET
    @Path("{idCompany}/employees")
    public Class<EmployeeResource> getEmployees() {
    return resourceContext.getResource(EmployeeResource.class);
    }
    }

    @Singleton
    public class EmployeeResource {

    @Context
    private Request request;

    @GET
    public List<Employee> getEmployees(@PathParam("idCompany") String companyId) {
    // some code
    }
    }

    缺点:

    • 没有依赖注入(inject) JSR-330

另见:

关于java - 将@PathParam 传递给 Jersey 中的子资源定位器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com