Hi David,
Note that the list is moderated to stop spam. I recommend you add
subscribe to the list, that way i don't have to approve your emails and
remember to CC you.
The reason for your issue is you have annotated the method
User.getUser() with an HttpMethod. Methods annotated with HttpMethod
will process an HTTP request and return an HTTP response. This means
that the returned object User is meant to returned as a representation
of HTTP response and not a sub-resource. Since the Jersey runtime does
not know how to serialize User you should be getting an exception thrown.
If you change UserResource to:
public class UserResource {
@UriTemplate("user/{key}/")
public User getUser(@UriParam("key")
String key) {
return new UserDAO().findByUser(key);
}
}
Then this tells the runtime the User.getUser method will return an
object to continue matching, as you intended.
I am not sure if you intended the class Url to be a representation or
another sub-resource. If the former then by default the runtime will not
know how to serialize the type Url so you will have to:
1) return something different e.g. String; or
2) write an EntityProvider supporting the serializing of the type Url.
It looks like this is a confusing area. You are the second person i know
to get caught out like this. The issue might be that the @HttpMethod
annotation without a parameter obtains the HTTP method to respond to
from the first bit of the method name. If we did not have this you would
of had to do:
public class UserResource {
@HttpMethod(GET)
@UriTemplate("user/{key}/")
public User getUser(@UriParam("key")
String key) {
return new UserDAO().findByUser(key);
}
}
and then you may have realized you were trying to do two different
things conflicting things?
Hope this helps, and any feedback on how to improve things would be
greatly appreciated.
Paul.
davidecerbo_at_email.it wrote:
> Hi all,
> I'm tryng to use Jersey, but I have some problems with sub resources.
> My resources are User and Url and I want get by array id a url associated to
> user.
> The main class is:
>
> public class UserResource {
> @HttpMethod
> @UriTemplate("user/{key}/")
> public User getUser(@UriParam("key")
> String key) {
> return new UserDAO().findByUser(key);
> }
> }
>
> while class User is:
>
> public class User{
>
> /*constructor set and get methods
> ...
> */
> private Set<Urls> urls = new HashSet<Urls>();
>
> @HttpMethod
> @UriTemplate("url/{keyUrl}/")
> public Urls getUrl(@UriParam("keyUrl")
> int keyUrl) {
> Url url = null;
> if (this.getUrls() != null) {
> url = (Url)this.getUrls().toArray()[keyUrl];
> }
> return url;
> }
>
> }
> }
>
> and class Url is:
> public class Url{
> /*constructor set and get methods
> ...
> */
> }
>
> If I try to access to http://localhost/user/jesty the service works well,
> but if i tryng to access to http://localhost/user/jesty/url/0 the method
> called is only getUser with key: "jesty/url/0" without calling getUrl after.
> How can I do?
> Thanks,
> Davide
>
>
>
> --
> Email.it, the professional e-mail, gratis per te: http://www.email.it/f
>
> Sponsor:
> 250 biglietti da visita Gratis + 42 modelli e Etichette per Indirizzo
> Gratis + Porta biglietti Gratis -Offerta limitata!
> Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=6783&d=20070713
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: dev-help_at_jersey.dev.java.net
>
--
| ? + ? = To question
----------------\
Paul Sandoz
x38109
+33-4-76188109