users@jersey.java.net

[Jersey] Re: Singleton class in Jersey

From: Dietz, Randall <randall_at_dietz.id.au>
Date: Sun, 1 Feb 2015 07:09:10 +1000

Hi Robert,

Thanks for the code snippet, that was very helpful.

You were right, it was something in the way... turns out my ResourceConfig
was not being executed correctly. Your code does work.

Thanks mate, U R A Champion!

cheers

On Sun, Feb 1, 2015 at 4:45 AM, Robert DiFalco <robert.difalco_at_gmail.com>
wrote:

> This works perfectly for me every time. What is your setup i.e.
> dependencies, container, are you using spring? What version of Jersey? Mine
> is super simple, Jersey and Grizzly, no Spring, no J2EE, no Servlets. I'm
> guessing you have something going on that is getting in your way.
>
> @Singleton
> @Path("/test")
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_JSON)
> public class TestResource {
>
> private final TestService service;
>
> @Inject
> public UserResource(TestService service) {
> this.service = service;
> }
>
> @GET
> public String get() {
> return service.getString();
> }
> }
>
> public class TestService {
> public String getString() {
> return "test";
> }
> }
>
> public class JerseyResourceConfiguration extends ResourceConfig {
>
> public JerseyResourceConfiguration() {
>
> // where the TestResource class is
> packages("com.example.rest.resources");
>
> register(new AbstractBinder() {
> @Override
> protected void configure() {
> bind(new TestService()).to(TestService.class);
> }
> });
> }
> }
>
>
>
> On Fri, Jan 30, 2015 at 11:14 PM, Dietz, Randall <randall_at_dietz.id.au>
> wrote:
>
>> Nah, that doesn't work either, same exception... nothing to inject.
>>
>> I'm going to have to find some other way to do this I think...
>>
>> Thanks anyway for the help
>>
>>
>> On Saturday, January 31, 2015, Robert DiFalco <robert.difalco_at_gmail.com>
>> wrote:
>>
>>> Works for me and then I @Inject MyService into the constructor of my
>>> resource class.
>>>
>>> Sent from my iPhone
>>>
>>> On Jan 30, 2015, at 9:15 PM, Dietz, Randall <randall_at_dietz.id.au> wrote:
>>>
>>> Thanks Robert,
>>>
>>> I've tried that approach (a variation of it anyway) and still get the
>>> following exception:
>>> org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no
>>> object available for injection at
>>> SystemInjecteeImpl(requiredType=ConfigurationManager,parent=UserApiService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1807772254)
>>>
>>> I'm using the following to do the injection:
>>>
>>> @Inject
>>> private ConfigurationManager configurationManager;
>>>
>>> If I use @Context instead of @Inject (as per someone elses suggestion),
>>> I don't get an exception but the class is not injected.
>>>
>>> I can't help but think there's something missing here, is that code
>>> below all I should have to do?
>>>
>>> Thanks
>>>
>>>
>>> On Saturday, January 31, 2015, Robert DiFalco <robert.difalco_at_gmail.com>
>>> wrote:
>>>
>>>> I'm not exactly sure what you are asking but if you have some service
>>>> that you want to be able to inject into your Resource instances you can
>>>> just add this to the constructor of your ResourceConfig class:
>>>>
>>>> register(new AbstractBinder() {
>>>> @Override
>>>> protected void configure() {
>>>> bind(MyService.getInstance()).to(MyService.class);
>>>> }
>>>> });
>>>>
>>>> On Fri, Jan 30, 2015 at 1:11 PM, Dietz, Randall <randall_at_dietz.id.au>
>>>> wrote:
>>>>
>>>>> Hi all,
>>>>>
>>>>> Can someone please point me to some up-to-date documentation that has
>>>>> an example of how to add a singleton class to a Jersey (v2.15) REST API?
>>>>>
>>>>> I have successfully built a REST application in Jersey, now all I want
>>>>> to do is inject a Singleton class (that controls access to my central
>>>>> configuration) in my service classes that is shared across all requests.
>>>>> Really, really simple use case... but I've wasted hours trying to get it
>>>>> working.
>>>>>
>>>>> The problem I've encountered is that there are a number of "solutions"
>>>>> on the InterWeb, but they're either out of date, confusing or just plain
>>>>> wrong.
>>>>>
>>>>> Would appreciate some guidance or examples of how it should be done in
>>>>> the latest Jersey.
>>>>>
>>>>> Thanks -- Randall
>>>>>
>>>>>
>>>>
>