Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6759071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:53:18+00:00 2026-05-26T13:53:18+00:00

I did try going through the following links How to wire in a collaborator

  • 0

I did try going through the following links
How to wire in a collaborator into a Jersey resource?
and
Access external objects in Jersey Resource class
But still i am unable to find a working sample which shows how to inject into a Resource class.
I am not using Spring or a web container.

My Resource is

package resource;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/something")
public class Resource
{
    @MyResource
    Integer foo = null;
    private static String response = "SampleData from Resource";

    public Resource()
    {
        System.out.println("...constructor called :" + foo);
    }

    @Path("/that")
    @GET
    @Produces("text/plain")
    public String sendResponse()
    {
        return response + "\n";
    }
}

My Provider is

package resource;

import javax.ws.rs.ext.Provider;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

@Provider
public class MyResourceProvider implements InjectableProvider<MyResource, Integer>
{
    @Override
    public ComponentScope getScope()
    {
       return ComponentScope.PerRequest;
    }

     @Override
    public Injectable getInjectable(final ComponentContext arg0, final MyResource arg1, final Integer arg2)
    {
       return new Injectable<Object>()
        {
            @Override
            public Object getValue()
            {
              return new Integer(99);
            }
        };
    }
}

My EndpointPublisher is

import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;

class EndpointPublisher
{
    public static void main(final String[] args)
    {

        final String address = "http://localhost:8080/";
        final Map<String, String> config = new HashMap<String, String>();
        config.put("com.sun.jersey.config.property.packages", "resource");
        try
        {
            GrizzlyWebContainerFactory.create(address, config);
            System.out.println("server started ....." + address);
            callGet();
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void callGet()
    {
        Client client = null;
        ClientResponse response = null;
        client = Client.create();
        final WebResource resource =
                client.resource("http://localhost:8080/something");
        response = resource.path("that")
                .accept(MediaType.TEXT_XML_TYPE, MediaType.APPLICATION_XML_TYPE)
                .type(MediaType.TEXT_XML)
                .get(ClientResponse.class);
        System.out.println(">>>> " + response.getResponseDate());
    }
}

My annotation being

@Retention(RetentionPolicy.RUNTIME)
public @interface MyResource
{}

But when i execute my EndpointPublisher i am unable to inject foo!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:53:19+00:00Added an answer on May 26, 2026 at 1:53 pm

    Your InjectableProvider is not implemented correctly. The second type parameter should not be the type of the field you are trying to inject – instead it should be the context – either java.lang.reflect.Type class or com.sun.jersey.api.model.Parameter class. In your case, you would use Type. So, your InjectableProvider implementation should look as follows:

    package resource;
    
    import javax.ws.rs.ext.Provider;
    import com.sun.jersey.core.spi.component.ComponentContext;
    import com.sun.jersey.core.spi.component.ComponentScope;
    import com.sun.jersey.spi.inject.Injectable;
    import com.sun.jersey.spi.inject.InjectableProvider;
    import java.lang.reflect.Type;
    
    @Provider
    public class MyResourceProvider implements InjectableProvider<MyResource, Type> {
    
        @Override
        public ComponentScope getScope() {
            return ComponentScope.PerRequest;
        }
    
        @Override
        public Injectable getInjectable(final ComponentContext arg0, final MyResource arg1, final Type arg2) {
            if (Integer.class.equals(arg2)) {
                return new Injectable<Integer>() {
    
                    @Override
                    public Integer getValue() {
                        return new Integer(99);
                    }
                };
            } else {
                return null;
            }
        }
    }
    

    There is a helper class for per-request injectable providers (PerRequestTypeInjectableProvider) as well as singleton injectable providers (SingletonTypeInjectableProvider), so you can further simplify it by inheriting from that:

    package resource;
    
    import javax.ws.rs.ext.Provider;
    import com.sun.jersey.core.spi.component.ComponentContext;
    import com.sun.jersey.spi.inject.Injectable;
    import com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider;
    
    @Provider
    public class MyResourceProvider extends PerRequestTypeInjectableProvider<MyResource, Integer> {
        public MyResourceProvider() {
            super(Integer.class);
        }
    
        @Override
        public Injectable<Integer> getInjectable(ComponentContext ic, MyResource a) {
            return new Injectable<Integer>() {
                @Override
                public Integer getValue() {
                    return new Integer(99);
                }
            };
        }
    }
    

    Note that for these helper classes the second type parameter is the type of the field.

    And one more thing – the injection happens after the constructor is called, so the constructor of your resource will still print out ...constructor called :null, but if you change your resource method to return foo, you’ll see the response you’ll get will be 99.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Did anyone try that feature and has some feedback? Or Does anyone know some
I admit that I have almost none experience of unittesting. I did a try
just to put it out there -I really did search and try to find
I did some googling to try to answer this question but even after that
I did this: git branch --track stats_page to create a separate branch to try
Hi I try to calculate fundamental matrix using emguCV library. Here what i did
I'm going through a book of general c# development, and I've come to the
I'm totally new to Ruby but not to programming. All I did was going
I'm going through a book focusing on x86 programming (Professional Assembly Language, WROX 2005).
I don't know what's going on here... but the Microsoft.Win32.Registry class is returning all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.