You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Alex Soto <as...@gmail.com> on 2014/11/03 11:34:57 UTC

Class as return type in subresources

Hi,

I would like to create a subresource which returns a class type (annotated
with javax.injection.Singleton). The example comes from Jersey
documentation. Let me show you how it looks:

@Path("/v1")
public class DispatcherResourceV1 {

@Path("/chapter")
public Class<ChapterResourceV1> v1Chapters() {
return ChapterResourceV1.class;
}
 //.. and other resources
}

import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Singleton
public class ChapterResourceV1 {

@GET
@Path("/")
@Produces(MediaType.TEXT_PLAIN)
public String chapter() {
return "Hello V1";
}
}

But running this code next log message is printed:

WARNING: No resource methods have been found for resource class
java.lang.Class
Nov 03, 2014 11:30:14 AM org.apache.cxf.jaxrs.JAXRSInvoker invoke
SEVERE: No subresource locator found for path /

Obviously it seems I am missing something but I don't know exactly what,
because if I change returning an class type to returning an instance it
works perfectly.



-- 
+----------------------------------------------------------+
  Alex Soto Bueno
  www.lordofthejars.com
+----------------------------------------------------------+

Re: Class as return type in subresources

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Think it does the same excepted .class can use a cdi instance where
resourcecontext ignores cdi
Le 19 nov. 2014 22:42, "Sergey Beryozkin" <sb...@gmail.com> a écrit :

> JAX-RS 2.0 ResourceContext.init(MyClass.class) is definitely a more
> portable
> way to do it, though the style where a .class is used is a bit intrusive as
> far as the application code is concerned IMHO, the application code just
> does what it is supposed to do without having the code which would only
> work
> if the external party, the runtime in this case, would instantiate a class
>
> Thanks, Sergey
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Class-as-return-type-in-subresources-tp4672736p4672941.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: Class as return type in subresources

Posted by Sergey Beryozkin <sb...@gmail.com>.
JAX-RS 2.0 ResourceContext.init(MyClass.class) is definitely a more portable
way to do it, though the style where a .class is used is a bit intrusive as
far as the application code is concerned IMHO, the application code just
does what it is supposed to do without having the code which would only work
if the external party, the runtime in this case, would instantiate a class

Thanks, Sergey



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Class-as-return-type-in-subresources-tp4672736p4672941.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Class as return type in subresources

Posted by Romain Manni-Bucau <rm...@gmail.com>.
I understood what it should do but JAXRS spec doesn't speak about it
(or I missed it, if so please point it out then we'll fix it)


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-03 12:33 GMT+00:00 Alex Soto <as...@gmail.com>:
> Well it is a class annotated with CDI annotation (@Singleton) so for what I
> understand the instance should be initialized by CDI container.
>
> Look Jersey example is:
>
> import javax.inject.Singleton;
>
> @Path("/item")
> public class ItemResource {
>     @Path("content")
>     public Class<ItemContentSingletonResource> getItemContentResource() {
>         return ItemContentSingletonResource.class;
>     }
> }
>
> @Singleton
> public class ItemContentSingletonResource {
>     // this class is managed in the singleton life cycle
> }
>
> 2014-11-03 11:43 GMT+01:00 Romain Manni-Bucau <rm...@tomitribe.com>:
>
>> Spec says "Objects returned by sub-resource locators (see Section
>> 3.4.1) are expected to be
>> initialized by their creator." + reading 3.4.1 not sure Class is valid
>> (even if I get it could make sense).
>>
>> Romain Manni-Bucau
>> Twitter: @rmannibucau
>> Blog: http://rmannibucau.wordpress.com/
>> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> Github: https://github.com/rmannibucau
>>
>>
>>
>> 2014-11-03 11:34 GMT+01:00 Alex Soto <as...@gmail.com>:
>> > Hi,
>> >
>> > I would like to create a subresource which returns a class type
>> (annotated
>> > with javax.injection.Singleton). The example comes from Jersey
>> > documentation. Let me show you how it looks:
>> >
>> > @Path("/v1")
>> > public class DispatcherResourceV1 {
>> >
>> > @Path("/chapter")
>> > public Class<ChapterResourceV1> v1Chapters() {
>> > return ChapterResourceV1.class;
>> > }
>> >  //.. and other resources
>> > }
>> >
>> > import javax.inject.Singleton;
>> > import javax.ws.rs.GET;
>> > import javax.ws.rs.Path;
>> > import javax.ws.rs.Produces;
>> > import javax.ws.rs.core.MediaType;
>> >
>> > @Singleton
>> > public class ChapterResourceV1 {
>> >
>> > @GET
>> > @Path("/")
>> > @Produces(MediaType.TEXT_PLAIN)
>> > public String chapter() {
>> > return "Hello V1";
>> > }
>> > }
>> >
>> > But running this code next log message is printed:
>> >
>> > WARNING: No resource methods have been found for resource class
>> > java.lang.Class
>> > Nov 03, 2014 11:30:14 AM org.apache.cxf.jaxrs.JAXRSInvoker invoke
>> > SEVERE: No subresource locator found for path /
>> >
>> > Obviously it seems I am missing something but I don't know exactly what,
>> > because if I change returning an class type to returning an instance it
>> > works perfectly.
>> >
>> >
>> >
>> > --
>> > +----------------------------------------------------------+
>> >   Alex Soto Bueno
>> >   www.lordofthejars.com
>> > +----------------------------------------------------------+
>>
>
>
>
> --
> +----------------------------------------------------------+
>   Alex Soto Bueno - Computer Engineer
>   www.lordofthejars.com
> +----------------------------------------------------------+

Re: Class as return type in subresources

Posted by Alex Soto <as...@gmail.com>.
Well it is a class annotated with CDI annotation (@Singleton) so for what I
understand the instance should be initialized by CDI container.

Look Jersey example is:

import javax.inject.Singleton;

@Path("/item")
public class ItemResource {
    @Path("content")
    public Class<ItemContentSingletonResource> getItemContentResource() {
        return ItemContentSingletonResource.class;
    }
}

@Singleton
public class ItemContentSingletonResource {
    // this class is managed in the singleton life cycle
}

2014-11-03 11:43 GMT+01:00 Romain Manni-Bucau <rm...@tomitribe.com>:

> Spec says "Objects returned by sub-resource locators (see Section
> 3.4.1) are expected to be
> initialized by their creator." + reading 3.4.1 not sure Class is valid
> (even if I get it could make sense).
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2014-11-03 11:34 GMT+01:00 Alex Soto <as...@gmail.com>:
> > Hi,
> >
> > I would like to create a subresource which returns a class type
> (annotated
> > with javax.injection.Singleton). The example comes from Jersey
> > documentation. Let me show you how it looks:
> >
> > @Path("/v1")
> > public class DispatcherResourceV1 {
> >
> > @Path("/chapter")
> > public Class<ChapterResourceV1> v1Chapters() {
> > return ChapterResourceV1.class;
> > }
> >  //.. and other resources
> > }
> >
> > import javax.inject.Singleton;
> > import javax.ws.rs.GET;
> > import javax.ws.rs.Path;
> > import javax.ws.rs.Produces;
> > import javax.ws.rs.core.MediaType;
> >
> > @Singleton
> > public class ChapterResourceV1 {
> >
> > @GET
> > @Path("/")
> > @Produces(MediaType.TEXT_PLAIN)
> > public String chapter() {
> > return "Hello V1";
> > }
> > }
> >
> > But running this code next log message is printed:
> >
> > WARNING: No resource methods have been found for resource class
> > java.lang.Class
> > Nov 03, 2014 11:30:14 AM org.apache.cxf.jaxrs.JAXRSInvoker invoke
> > SEVERE: No subresource locator found for path /
> >
> > Obviously it seems I am missing something but I don't know exactly what,
> > because if I change returning an class type to returning an instance it
> > works perfectly.
> >
> >
> >
> > --
> > +----------------------------------------------------------+
> >   Alex Soto Bueno
> >   www.lordofthejars.com
> > +----------------------------------------------------------+
>



-- 
+----------------------------------------------------------+
  Alex Soto Bueno - Computer Engineer
  www.lordofthejars.com
+----------------------------------------------------------+

Re: Class as return type in subresources

Posted by Romain Manni-Bucau <rm...@tomitribe.com>.
Spec says "Objects returned by sub-resource locators (see Section
3.4.1) are expected to be
initialized by their creator." + reading 3.4.1 not sure Class is valid
(even if I get it could make sense).

Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-11-03 11:34 GMT+01:00 Alex Soto <as...@gmail.com>:
> Hi,
>
> I would like to create a subresource which returns a class type (annotated
> with javax.injection.Singleton). The example comes from Jersey
> documentation. Let me show you how it looks:
>
> @Path("/v1")
> public class DispatcherResourceV1 {
>
> @Path("/chapter")
> public Class<ChapterResourceV1> v1Chapters() {
> return ChapterResourceV1.class;
> }
>  //.. and other resources
> }
>
> import javax.inject.Singleton;
> import javax.ws.rs.GET;
> import javax.ws.rs.Path;
> import javax.ws.rs.Produces;
> import javax.ws.rs.core.MediaType;
>
> @Singleton
> public class ChapterResourceV1 {
>
> @GET
> @Path("/")
> @Produces(MediaType.TEXT_PLAIN)
> public String chapter() {
> return "Hello V1";
> }
> }
>
> But running this code next log message is printed:
>
> WARNING: No resource methods have been found for resource class
> java.lang.Class
> Nov 03, 2014 11:30:14 AM org.apache.cxf.jaxrs.JAXRSInvoker invoke
> SEVERE: No subresource locator found for path /
>
> Obviously it seems I am missing something but I don't know exactly what,
> because if I change returning an class type to returning an instance it
> works perfectly.
>
>
>
> --
> +----------------------------------------------------------+
>   Alex Soto Bueno
>   www.lordofthejars.com
> +----------------------------------------------------------+