jersey
  1. jersey
  2. JERSEY-1529

Jersey is unable to create Resource with more constructors

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.0-m09
    • Fix Version/s: 2.0-m13, 2.0
    • Component/s: core
    • Labels:
      None

      Description

      Spec. Section 3.1.2, says:

      If more than one public constructor is suitable then an implementation MUST use the one with the most
      parameters.

      However, the following code:

      @Path("Resource")
      public class Resource {
      	private HttpHeaders headers;
      	private UriInfo info;
      	private Application application;
      	private Request request;
      	private Providers provider;
      	
      	public Resource(){		
      	}		
      	
      	public Resource(@Context HttpHeaders headers){		
      		this.headers = headers;
      	}	
      
      	
      	public Resource(@Context HttpHeaders headers, @Context UriInfo info){		
      		this.headers = headers;
      		this.info = info;
      	}	
      	
      	public Resource(@Context HttpHeaders headers, @Context UriInfo info, @Context Application application){
      		this.application = application;
      		this.headers = headers;
      		this.info = info;
      	}
      		
      	public Resource(@Context HttpHeaders headers, @Context UriInfo info, @Context Application application, @Context Request request){
      		this.application = application;
      		this.headers = headers;
      		this.info = info;
      		this.request = request;
      	}
      	
      	protected Resource(@Context HttpHeaders headers, @Context UriInfo info, @Context Application application, @Context Request request, @Context Providers provider){
      		this.application = application;
      		this.headers = headers;
      		this.info = info;
      		this.request = request;
      		this.provider = provider;
      	}	
      	
      	@GET
      	@Path("mostAttributes")
      	public Response isUsedConstructorWithMostAttributes(){
      		boolean ok = application != null;
      		ok &= headers != null;
      		ok &= info != null;
      		ok &= request != null;
      		ok &= provider == null;
      		Status status = ok ? Status.OK : Status.NOT_ACCEPTABLE;
      		return Response.status(status).build();
      	}
      }
      

      Throws :

      MultiException stack 1 of 1
      java.lang.IllegalArgumentException: There is more than one constructor on class Resource
      at org.jvnet.hk2.internal.Utilities.findProducerConstructor(Utilities.java:824)
      at org.jvnet.hk2.internal.Utilities.justCreate(Utilities.java:632)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.create(ServiceLocatorImpl.java:601)
      at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:657)
      at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:186)
      at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
      at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:102)
      at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:115)
      at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
      at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
      at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
      at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
      at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:101)
      at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61)
      at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:189)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:316)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:174)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:761)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:309)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:349)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:312)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:195)

        Issue Links

          Activity

          Hide
          Marek Potociar added a comment -

          We should use the new HK2 constructor selection feature to resolve this issue.

          Show
          Marek Potociar added a comment - We should use the new HK2 constructor selection feature to resolve this issue.

            People

            • Assignee:
              Marek Potociar
              Reporter:
              jan.supol
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 3 hours Original Estimate - 3 hours
                3h
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 7 hours
                7h