Issue
I have my REST API that exposes a resource made of some readonly fields. These fields are calculated automatically from the system and exposed in the JSON representation, and they are part of a domain entity.
I've succeeded in using the framework (JacksonMapper) annotation on the method that calculates the result, like:
@JsonProperty
public boolean isAnonymous(){
//some logic
return result;
}
Until these resource are used for read purpose, everything is OK. t
The problems arise when I have to work with write operations; update
for example.
The framework I use for the client (angularjs), sends these fields to the update API when I search for a resource modify, and update it, for example:
function test(MyResource){
MyResource.get({id: 0}, function(myResource){
myResource.writeableProperty = 'modified';
myResource.$update(function(res){
console.log('everything went ok');
});
}
}
In this case the problem is the JSON sent by angular for the update:
{
"id": 0,
"writeableProperty": "modified"
"anonymous": true
}
In this case the deserialization of the JSON fail, due to the fact that the anonymous
property doesn't exist in my domain entity.
How can I manage these readonly property, is it right to have the representation of the JSON that differs from what you have from the server and what you have to send to the server?
Or maybe it's better to use a different resource, making it possible to send that property, creating a related DTO in my application, and ignore the property on it (or send an error if someone try to change it)?
Solution
If you want to ignore this property on the server-side, you can use the JsonIgnoreProperties annotation.
@JsonIgnoreProperties(ignoreUnknown=true)
public class MyObject {
....
}
This will serialize only attributes that was declared in your class.
Answered By - Deividi Cavarzan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.