Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing @PathParam to Sub Resource locator class in Jersey

Tags:

java

rest

jersey

I want to call my api endpoint this way:

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

and retrieve the employees of company with code 1. I have this code:

@Path("companies")
public class CompanyResource {

    @Context
    ResourceContext resourceContext;

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

}

and

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

    @PathParam("idCompany")
    String idCompany;

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

But the path param is null. What am I doing wrong? Is there a more correct way to do this?

like image 862
Héctor Avatar asked Oct 22 '15 07:10

Héctor


3 Answers

Here is a summary of possible solutions with JAX-RS 2.0:

  • Request scoped sub resource with constructor:

    @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
        }
    }
    

    Disadvantages:

    • no dependency injection
  • Request scoped sub resource with 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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330
  • Request scoped sub resource with 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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330
  • Request scoped sub resource with @PathParam as field:

    @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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330
  • Request scoped sub resource with return type 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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330
  • Request scoped sub resource with @PathParam as method parameter:

    @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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330
  • Singleton sub resource with @PathParam as method parameter:

    @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
        }
    }
    

    Disadvantages:

    • no dependency injection with JSR-330

See also:

  • Jersey User Guide - Rules of Injection
  • Jersey User Guide - sub-resources
  • Java EE 7 and JAX-RS 2.0
  • JAX_RS_SPEC-55
like image 61
dur Avatar answered Oct 22 '22 20:10

dur


I couldn't reproduce the null id, but there are a couple things to point out

  • @GET should be removed from the EmployeeResource getEmployees() method. Sub Resource locators shouldn't have HTTP method annotations. See Sub-resources
  • Also the @Path on the EmployeeResource is ignored (not needed). Not a problem, just thought you should know.

Below a complete working example using Jersey Test Framework. Here are the test dependencies I used

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>${jersey2.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
    <scope>test</scope>
</dependency>

You can run it like any other JUnit test

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static junit.framework.Assert.*;

public class LocatorTest extends JerseyTest {

    public static class Employee {
        public String firstName;
        public String lastName;
        public Employee(){}
        public Employee(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }

    public static class Company {
        public String companyId;
        public List<Employee> employees;
        public Company(){}
        public Company(String companyId, List<Employee> employees) {
            this.companyId = companyId;
            this.employees = employees;
        }
    }

    @Path("companies")
    public static class CompanyResource {

        @Context
        private ResourceContext resourceContext;

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

    public static class EmployeeResource {

        @PathParam("companyId")
        public String companyId;

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Company getCompanyEmployees() {
            List<Employee> emps = new ArrayList<>();
            emps.add(new Employee("pee", "skillet"));
            emps.add(new Employee("Stack", "Overflow"));
            Company co = new Company(companyId, emps);
            return co;
        }
    }

    @Override
    public ResourceConfig configure() {
        return new ResourceConfig(CompanyResource.class);
    }

    @Test
    public void doit() {
        Response response = target("companies/1234/employees").request().get();
        assertEquals(200, response.getStatus());
        Company co = response.readEntity(Company.class);
        assertNotNull(co.companyId);
        assertEquals("1234", co.companyId);
        assertEquals(2, co.employees.size());
        response.close();
    }
}
like image 23
Paul Samsotha Avatar answered Oct 22 '22 20:10

Paul Samsotha


I use path param like this

@GET
public List<Employee> getEmployees(@PathParam("idCompany") String id ) {
    // here "idCompany" is null
    //some code
}
like image 29
Thanh Duy Ngo Avatar answered Oct 22 '22 19:10

Thanh Duy Ngo