brain storm
brain storm

Reputation: 31252

How to return XML/JSON representation without the entity ID or a given attribute in JAX-RS at runtime?

Consider this entity:

@Entity
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;

private String username;

private String password;

// getters and setters

}

@Path("/users")
@Stateless
@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public class UserService {


@GET
@PATH("all")
//return list of all usernames

@GET
@PATH("{id}"}
//get a single user object
}

now the xml or JSON return includes id element; i.e

<user>
   <id>343</id>
   <username>name</username>
   <password>pwd</password>
</user>

Q1) The question is during the generation of resource (when GET request comes in), is it possible to remove the id element from return xml object. Here I know at deployment that I want to remove the id attribute from the xml.

I could do the following; but the problem is I have to detach the entity before changing its ID attribute and i am not sure how it can be done (since it is container managed )

 @GET
@Path("{id}")
public User getUser(@PathParam("id") int id){
      User u = u=em.find(User.class, id);
      //detach the object u
      u.setId(-9999)
     return u;
}

Q2) At a one step higher, is it possible to return a User object based on what attributes are needed at run-time For ex: www.foo.com/users/1?att=username.

Here I specify, that I need only the attribute username of User object with id 1. In my GET request method, I can do the following, but this looks brittle to me. I guess there is a cleaner solution to this.

@GET
@Path("{id}")
public User getUser(@PathParam("id") int id, @QueryParam("att") String attribute){
      User u = u=em.find(User.class, id);
      User u2= null;
      if (attribute==null){ 
          return u;
      }
       else if (att="id") {
         u2.setUserName(u.getUserName());
         u2.setUserPassword(u.getUserPassword());
       } else if (att="password"){
          //same logic as above
         } else if (att="username") {

          }
             return u2;
}

}

Thanks

Upvotes: 0

Views: 150

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209004

"Q1) The question is during the generation of resource (when GET request comes in), is it possible to remove the id element from return xml object. Here I know at deployment that I want to remove the id attribute from the xml".

You could simply use @XmlTransient -

The @XmlTransient annotation is useful for resolving name collisions between a JavaBean property name and a field name or preventing the mapping of a field/property.

@Id
@XmlTransient
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;

"Q2) At a one step higher, is it possible to return a User object based on what attributes are needed at run-time For ex: www.foo.com/users/1?att=username"

As far as this question, I don't see any real "elegant" way to do this, other than something in the range of what you're currently doing (assuming you still want an xml response)

Upvotes: 1

Related Questions