JSON reponse with Spring Controllers in Jetty vs Tomcat -


i building simple spring mvc webapp , developing on jetty. controller binding used this:

@requestmapping(value = restroutes.create_doc, method = requestmethod.post)     public  @responsebody string getdoc 

and returning string jsonobject correctly resolves json in ajax response.

but using same controllers, deployed gradle war tomcat , json came wrapped true strings.

so changed headers use map , seems fix things in both jetty , tomcat:

@requestmapping(value = restroutes.create_doc, method = requestmethod.post)         public  @responsebody map<string, string> getdoc 

i convert string map this:

        hashmap<string, string> jsonresponse = new hashmap<string, string>();         if(claimfolder.has("error")){             response.setstatus(500);         }else{             jsonresponse = new objectmapper().readvalue(claimfolder.tostring(), hashmap.class);         }         return jsonresponse; 

my question why nessesary?

here's jackson converter configuration:

<bean id="formconverter" class="org.springframework.http.converter.formhttpmessageconverter" />  <!-- add byte[] converter --> <bean id="bytearrayconverter" class="org.springframework.http.converter.bytearrayhttpmessageconverter">    <property name="supportedmediatypes" value="application/octet-stream" /> </bean>    <!--  add in our json message converter --> <bean id="jsonconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter">     <property name="supportedmediatypes" value="application/json;charset=utf-8" /> </bean>  <!-- add in our plain string message converter --> <bean id="stringhttpmessageconverter" class="org.springframework.http.converter.stringhttpmessageconverter">     <property name="supportedmediatypes" value="text/plain;charset=utf-8" /> </bean>  <!-- expose authenticated handler beans have been declared via annotation --> <bean class="org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping"> </bean> 

tl;dr: why jetty , tomcat return stringified json differently?

well, it's absolutely normal spring content negotiation translate string object simple string without marshalling json object. in order serialize java string object in json object need wrap in java class. example:

questionstatus {  private string status;  public questionstatus(string status) { this.status = status; }  public getstatus() { return status; } } 

hence have return in controller method not string questionstatus.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -