I try to do the same thing
Client side
ClientResource consumer = new ClientResource("// uri //");
Campus camp = new Campus("Montréal");
try {
consumer.post(camp,
MediaType.APPLICATION_JSON).write(System.out);
// should echo
{key:""ahJzfm5vdGV0b25zdGExMzQ1MDhyDQsSBkNhbXB1cxjpBww,name:"Montréal"}
} catch (ResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Server side
@POST @Path("campus")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Campus create(Campus campus){
// but introspecting the campus object and get nothing
// store in database and retrieve the new created campus object
//
{key:""ahJzfm5vdGV0b25zdGExMzQ1MDhyDQsSBkNhbXB1cxjpBww,name:"Montréal"}
return campus;
}
My Campus Object
@Entity
public class Campus implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger log =
Logger.getLogger(Campus.class.getSimpleName());
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private Key key;
@Basic
private String name;
@Basic
@JsonIgnore
private List interventionIds;
public Campus(){}
public Campus(Key key, String name) {
this.key = key;
this.name = name;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getInterventionIds() {
return interventionIds;
}
public void setInterventionsKey(List interventionIds) {
this.interventionIds = interventionIds;
}
public void addInterventionId(Long id){
if(!this.interventionIds.contains(id)) {
this.interventionIds.add(id);
}
}
public void removeInterventionId(Long id) throws KeyNotFoundException {
if(!this.interventionIds.contains(id)){
throw new KeyNotFoundException();
}
this.interventionIds.remove(id);
}
@JsonProperty
public String key(){
log.log(Level.INFO, "Key conversion (before)", this.key);
String convert_key = KeyFactory.keyToString(this.key);
log.log(Level.INFO, "Key conversion (after)", convert_key);
return convert_key;
}
@JsonProperty
public void key(String k){
this.setKey(KeyFactory.stringToKey(k));
}
@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
return EqualsBuilder.reflectionEquals(this, arg0);
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
@Produces works fine and I get the good representation but @Consumes not
consumes the JSON to Java Object
If someone can help us.
--
View this message in context:
http://restlet-discuss.1400322.n2.nabble.com/jaxrs-processing-response-tp7084522p7313223.html
Sent from the Restlet Discuss mailing list archive at Nabble.com.
------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2926001