java - Interface as return on GET method using JAX-RS -
the code below not follow pattern , created me illustrate problem.
the return type defined method interface, fields in concrete class created in json.
in example below, "anything" field not belong iperson interface, considered when producing result in json, although return of method iperson interface.
is right?
interface
public interface iperson { int getid(); string getname(); } concrete class
import javax.ws.rs.path; import javax.ws.rs.get; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; @path("/person") public class personrest implements iperson { //iperson implementation private int id; private string name; @override public int getid() { return this.id; } @override public string getname() { return this.name; } //other field not contained on iperson private string anything; @get @produces(mediatype.application_json) public iperson get() { //set values on iperson fields this.id = 15; this.name = "marcelo ribeiro"; //set values on other fields, not contained on iperson this.anything = "only example"; system.out.println(this.anything); //here returned instance converted on iperson return (iperson) this; } } json created
{"name":"marcelo ribeiro","anything":"only example","id":15} json expected
{"name":"marcelo ribeiro","id":15}
serializing interface not make sense. deserialization unknown. json picks concrete type. can tell ignore particular fields if want.
edit
you have take consideration class can implement several interfaces. in circumstance json can not make decision of interface methods use
Comments
Post a Comment