users@jersey.java.net

Re: [Jersey] Sub resource Locators in Jersey -cannot access attributes of the sub resource

From: Naresh <Srinivas.Bhimisetty_at_Sun.COM>
Date: Thu, 23 Jul 2009 11:22:23 +0530

Hi Suchitha,

I think adding a @GET annotation to your getFirstName( ) method would
help fix the problem.
-----------------------------------
@Path("firstName")
@GET

public String getFirstName() {

return firstName;

}
-----------------------------------

-Naresh


Suchitha Koneru (sukoneru) wrote:
>
> Hello Jersey Users,
>
> I am using Jersey 1.1.0-ea version, with Java 1.6 and Tomcat 5.5. I
> tried using sub resource locators as shown in the User-management
> example below and I cannot get it to work
>
> *//Main Resource *
>
> @Path("Users")
>
> public class UserMgmtService {
>
> static UserStore store;
>
> static {
>
> store = UserStore.getInstance();
>
> }
>
> @Produces("application/xml")
>
> @GET
>
> public UserBean[] getAllUsers() throws Exception {
>
> UserBean[] users = store.getAllUsers();
>
> System.out.println("the users are :");
>
> for(int i=0;i<users.length;i++){
>
> System.out.println(users[i]);
>
> }
>
> return users;
>
> }
>
> @Context
>
> @PUT
>
> @Consumes("application/xml")
>
> public JAXBElement<UserBean> updateUser(JAXBElement<UserBean> uBean)
> throws Exception{
>
> try{
>
> System.out.println("the name of the user to be updated is
> :"+uBean.getValue().getFirstName());
>
> store.updateUser(uBean.getValue());
>
> return uBean;
>
> }catch(Exception e){
>
> e.printStackTrace();
>
> throw e;
>
> }
>
> }
>
> *//Sub resource Locator *
>
> * *
>
> * *_at_Path("{userID}/")
>
> public UserBean getUser(@PathParam("userID") String userID) throws
> Exception {
>
> try{
>
> UserBean ubean = store.getUser(Long.parseLong(userID));
>
> return ubean;
>
> }catch(Exception e){
>
> e.printStackTrace();
>
> throw e;
>
> }
>
> }
>
> * *
>
> *//Sub resource *
>
> * *_at_XmlRootElement
>
> public class UserBean {
>
> private long userId = 0;
>
> private String firstName = null;
>
> private String lastName = null;
>
> private String phoneNumber = null;
>
> private String email = null;
>
> public UserBean(){}
>
> @GET
>
> @Produces ("application/xml")
>
> public UserBean getUser(){
>
> return this;
>
> }
>
> @Path("firstName")
>
> public String getFirstName() {
>
> return firstName;
>
> }
>
> @Path("lastName")
>
> public String getLastName() {
>
> return lastName;
>
> }
>
> public long getUserId() {
>
> return userId;
>
> }
>
> public void setUserId(long userId) {
>
> this.userId = userId;
>
> }
>
> }
>
> Iam able to access the user resource with the url , where “1” is the
> id of the user
>
> *http://localhost.cisco.com:8081/SvcWsJaxRs/Users/1*
>
> However ,When I try to access any of the arrtibutes of the user , it
> leads to a 404 (page not found). Here the attribute firstName is
> annotated with @Path("firstName") as shown above
>
> *http://localhost.cisco.com:8081/SvcWsJaxRs/Users/1/firstName*
>
> could you please let me know, if I am missing something
>
> thank you,
>
> suchitha.**
>
> * *
>
> * *
>