Can I make the Controller endpoint accept different json object (I have the matched class in java side)? -


i have different java class in java side .

public class prov{   private string providerid = "";   private string upin = "";   ...   //with getter , setter }  public class proc{   private string procid = "";   private string description = "";   ...   //with getter , setter }  @controller public class controller{    @requestmapping(value = "service/reports/prov", method = requestmethod.post)   @responsebody   public string searchprov(@responsebody prov prov){   return "prov"   }    @requestmapping(value = "service/reports/proc", method = requestmethod.post)   @responsebody   public string searchprov(@responsebody proc proc){   return "proc"   } } 

now have 2 endpoints defined in controller , wonder if possible passing different json object same endpoint.

i tried using inheritance class c. proc extends c , prov extends c. change controller to

@controller public class controller{    @requestmapping(value = "service/reports", method = requestmethod.post)   @responsebody   public string searchprov(@responsebody c c){     if(c instanceof prov){       return "prov"     }             if(c instanceof proc){       return "proc"     }   return "error"   } 

but it's not working. gets classcastingexception. think maybe caused javascript don't have class inheritance.(please tell me if i'm wrong .if can explain reason, nice) think maybe generic class might help. i'm not quite familiar it. or other way it. thank you.

first thing need make sure client sends inside json data jackson knows how deserialize object. examples below, tested 2 jsons:

{   "type": "cat",   "name": "skippy" } 

and

{   "type": "dog",   "name": "spike" } 

first map yor classes using jackson polymorfic deserialization. in case i'm using attribute called type decide class deserialized to. following base class animal:

@jsontypeinfo(use = jsontypeinfo.id.name, include = jsontypeinfo.as.property, property = "type") @jsonsubtypes({ @type(value = cat.class, name = "cat"), @type(value = dog.class, name = "dog") }) public class animal {    private string name;   public string getname() {     return name;   }   public void setname(string name) {     this.name = name;   } } 

and classes dog , `cat:

public class dog extends animal {   public void bark() {     system.out.println("au!");   } }  public class cat extends animal {   public void miau() {     system.out.println("miau!");   } } 

in controller can did normally:

@controller public class examplecontroller {    @requestmapping(value = "animal", method = requestmethod.post)   @responsebody   public string searchprov(@requestbody animal animal) {     if (animal instanceof dog) {       ((dog) animal).bark();       return "dog";     }     if (animal instanceof cat) {       ((cat) animal).miau();       return "cat";     }     return "error";   } } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -