No injection source found an error when using multiple @PathParams in Restful Resource using Jersey

I'm working on a simple Restful web service using Rest / Jersey with Tomcat 7. Everything works fine if all my Paths contain one parameter. If I include one that uses multiple parameters, I run into a Missing Injection Source error when I try to hit any of the resource paths supported by my resource class - even those that worked before. If I comment this piece of code, all my other paths work as expected. But if I uncomment this piece of code, I cannot use ANY of my paths.

Here is a snippet of my code:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

// get all the information about a specific item
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getItem(@PathParam("id") String itemId)
{
    String answer = "{";
    answer += "\"itemid\":\"" + itemId + "\",";
    answer += "\"type\":0,";
    answer += "\"sector\":322948,";
    answer += "\"created\":53249098220";
    answer += "}";

    return answer;
}

// if I comment out this method, all is fine; 
// if I leave it in, error is thrown when I make any restful call
// query to see if there is a new item for user
@Path("/loc/{userid}/{xloc}/{yloc}")
@Produces(MediaType.APPLICATION_JSON)
public String getNewitem(@PathParam("userid") String userId,
    @PathParam("xloc") Number xLoc,
    @PathParam("yloc") Number yLoc)
{
    String answer = "{\"itemid\":\"abcdefgh\"}";
    return answer;
}

      

Here's a trace of the error:

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
root cause

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.

[[FATAL] No injection source found for a parameter of type public java.lang.String com.drunkware.geobijous.resources.GBBijouResource.getNewBijou(java.lang.String,java.lang.Number,java.lang.Number) at index 1.; source='ResourceMethod{httpMethod=GET, consumedTypes=[], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.drunkware.geobijous.resources.GBBijouResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@757bdd3c]}, definitionMethod=public java.lang.String com.drunkware.geobijous.resources.GBBijouResource.getNewBijou(java.lang.String,java.lang.Number,java.lang.Number), parameters=[Parameter [type=class java.lang.String, source=userid, defaultValue=null], Parameter [type=class java.lang.Number, source=latitude, defaultValue=null], Parameter [type=class java.lang.Number, source=longitude, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
        org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:467)
        org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:163)
        org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:323)
        org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
        org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
        org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
        org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:320)
        org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
        org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
        org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
        org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
        org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
        java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Unknown Source)

      

Any suggestions on how to solve this problem? I am using jersey 2.12. Is this a known bug?

+3


source to share


1 answer


The injection error received is caused by the parameters Number

. Because complex objects such as Number are not supported in the @PathParam implementation. You can fix it easily with the following code (also add @GET)

@GET
@Path("loc/{userid}/{xloc}/{yloc}")
public String getNewitem(@PathParam("userid") String userId,
    @PathParam("xloc") int xLoc,
    @PathParam("yloc") int yLoc)
{
    String answer = "{\"itemid\":\"abcdefgh\"}";
    return answer;
}

      

There are many ways to work with complex objects if you need more information than it int

can provide you with. But then you have to change the way you get them, since it's @PathParam

pretty simple. For more complex usage check Jackson to help you with object / json conversion. You will probably have something like this:



@POST
@Path("loc/{userid}")
public String createNewitem(@PathParam("userid") String userId, MyLocalization aLoc)
{
    String answer = "{\"itemid\":\"abcdefgh\"}";
    return answer;
}

      

A class MyLocalization

is a pojo that you will create to be processed with jackson libs with the following dependency:

  <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.2.3</version>
  </dependency>

      

+6


source







All Articles