i have class follows:
@xmlrootelement(name="user") @xmlaccessortype(xmlaccesstype.none) public class user { private int id; private string name; public user() { } @xmlattribute public int getid() { return id; } public void setid() { this.id = id; } @xmlelement public string getname() { return first + " " + last; } public void setname(string name) { this.name = name; } // other class code }
i using class jax-rs service. when client wishes create new user, xml representation of following format must sent.
<user> <name>john doe</name> </user>
on receiving such snippet, service creates new user correctly. however, if client includes id attribute user (e.g. <user id="100">...</user>
) id value of attribute assigned the user.
as can imagine, wish use id field primary key of user class , not wish user able specify it. however, when return representation of user instance (also in xml), wish able specify id attribute.
how achieve this?
there few ways done:
option #1
you null id field out when create operation done.
@post @consumes(mediatype.application_xml) public void create(user user) { user.setid(0); entitymanager.persist(customer); }
option #2
alternatively have second user class without id field, use parameter of create operation:
@post @consumes(mediatype.application_xml) public void create(userwithoutid userwithoutid) { user user = new user(); // copy userwithoutid user entitymanager.persist(customer); }
option #3
if using property access, option provide getter id property. jaxb include in on write (marshal), not on read (unmarshal).
Comments
Post a Comment