java - Spring 4: MappingJackson2HttpMessageConverter does not support application/javascript for jsonp -
using spring 4.1.6.release , jackson libraries v2.5.4
want use jsonp in rest services using spring 4; spring 4 supports jsonp out of box.
configuration
<mvc:annotation-driven content-negotiation-manager="contentnegotiationmanager"> ... </mvc:annotation-driven>
using contentnegotiationmanager in following manner
<bean id="contentnegotiationmanager" class="org.springframework.web.accept.contentnegotiationmanagerfactorybean"> <property name="defaultcontenttype" value="application/json" /> <!-- configuration of path extension based contentnegotiationstrategy --> <property name="favorpathextension" value="false" /> <!-- configuration of request parameter based contentnegotiationstrategy --> <property name="favorparameter" value="true" /> <property name="parametername" value="formattype" /> <property name="mediatypes" ref="mediatypesmapping" /> <!-- configuration of http accept header based contentnegotiationstrategy --> <property name="ignoreacceptheader" value="true" /> </bean>
here mediatypemapping
<util:map id="mediatypesmapping"> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> <entry key="jsonp" value="application/javascript" /> </util:map>
here jsonpparameternames
<util:set id="jsonpparameternames"> <value>param1</value> <value>param2</value> <value>param3</value> </util:set>
deployed rest service.
hitting following url gives data in json format
/contextroot/info/1?formattype=json
hitting following url gives data in xmlformat
/contextroot/info/1?formattype=xml
hitting following url gives 406 exception
/contextroot/info/1?formattype=jsonp¶m1=callback1
observations
on doing investigation, discovered mappingjackson2httpmessageconverter not supports application/javascript.
surprised since spring 4 claims support jsonp out of box.
furthermore, mappingjackson2jsonview support jsonp out of box.
question
am missing in configuration once added enable me use content type application/javascript out of box spring 4 jsonp?
edit
here controlleradvice configuration:
<bean id="jsonpadvice" class="com.advice.accountservicecontrollerjsonpadvice"> <constructor-arg ref="jsonpparameternames" /> </bean>
and here controlleradvice
public class accountservicecontrollerjsonpadvice extends abstractjsonpresponsebodyadvice { private string[] callbacklist = {"callback"}; public string[] getcallbacklist() { return callbacklist; } public void setcallbacklist(string[] callbacklist) { this.callbacklist = callbacklist; } public accountservicecontrollerjsonpadvice(string... callbacklist) { super(callbacklist); } }
however, not enable application understand url following , returns 406 error
/contextroot/info/1?formattype=jsonp¶m1=callback1
it makes application understand request url following , return jsonp response:
/contextroot/info/1?param1=callback1
yes, spring 4.1+ supports jsonp, it's not conversion format per se.
mappingjackson2httpmessageconverter
supports "application/json"
, "*+json"
media types, not support "application/javascript"
. if did, you'd expect parse javascript code, not case.
here, we're merely wrapping output make "application/javascript"
, really, it's still json.
your current configuration disabling content negotiation http accept headers (why?). in order support jsonp need in mvc configuration:
<mvc:annotation-driven/>
and additional controlleradvice
(see reference documentation):
@controlleradvice public class jsonpadvice extends abstractjsonpresponsebodyadvice { public jsonpadvice() { super("callback"); } }
that's it.
now you'll get:
get /contextroot/info/1 // return what's best depending on accept header sent client /contextroot/info/1.xml // return xml /contextroot/info/1.json // return json /contextroot/info/1.json?callback=myfunction // return jsonp wrapped in "myfunction" call
in case, should:
- remove
"application/javascript"
media types mappings, or backwards compatibility associate"jsonp"
"application/json"
media type. - use
get /contextroot/info/1?formattype=json¶m1=callback1
see abstractjsonpresponsebodyadvice more details on implementation.
Comments
Post a Comment