json - Best way to use spring for Web and mobile application -


i new web development. planning create web service going act end both web site , mobile application. want know if possible use same method return data in different type.

for example: if use http://somewebsite/getdetails.jsp should give me , modelview return type , http://somewebsite/getdetails.json should give model in json format.

i don't want create 2 different controller handle this.

if there other better way please share comments.

i open alternative solutions

spring 4.0 / spring boot enables achieve quite easily. developing web-service (api) mobile , backend browser based clients , split api mobile under url @requestmapping("/api"). in addition, spring allows implement restful url based application. recommend have 2 different controllers api , web mvc because ensures complete separation between 2 different logics. e.g.

would implement following?

@suppresswarnings("unchecked")     public map<object, object> test(@requestparam(value="mobileyes") boolean mobile){      if(mobile){         map<object, object> m = new hashmap<object, object>();         m.put("test", "test")         return m;     } else {         modelandview mv = new  modelandview();         mv.addobject("test", "test");         mv.setviewname("test");         return (map<object, object>) mv;     }      } 

above example might work, ugly , cause maintenance disaster in near future.

this overall structure of spring mvc using spring boot :

ordinary url accessed desktop based , mobile based browsers

these controllers use @controller annotation because doesn't automatically enables @responsebody

www.mybusinesscard.com.au/ -> index


//displaying businesscards

www.mybusinesscard.com.au/businesscards -> view all


//for saving form

www.mybusinesscard.com.au/businesscard/save -> save card

following controller examples mobile api:

following controllers use @restcontroller annotation automatically enable requirements necessary webservices. e.g: @responsebody

www.mybusinesscard.com.au/api -> index


//displaying businesscards

www.mybusinesscard.com.au/api/businesscards -> view all


//for saving form

www.mybusinesscard.com.au/api/businesscard/save -> save card


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -