spring - I want my controlleradvice to always produce JSON when handling exceptions -


i have centralized exception handling rest service neat controlleradvice.

i returning regular transfer-objects, in hope of cool mapping-jackson-converter convert json end client.

now here's thing. if don't set accept-header "application/json", don't converted json, default html instead, in tests seemingly generated jetty. have admit i'm not sure why, guess it's default spring resolver comes effect.

this got me thinking. clients call rest url's should know return json, service return json regardless.

is there way configure reqeustmappinghandleradapter produce json?

my current config:

<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter">     <beans:property name="messageconverters">         <beans:list>             <beans:ref bean="jsonconverter"/>         </beans:list>     </beans:property> </beans:bean>  <!-- instantiation of converter  in order configure --> <beans:bean id="jsonconverter" class="org.springframework.http.converter.json.mappingjacksonhttpmessageconverter">     <!--beans:property name="supportedmediatypes" value="application/json"/--> </beans:bean> 

i don't know if answers question, set default content type in spring configuration json (it defaults html without accept header in spring mvc). here sample configuration:

<bean id="contentnegotiationmanager" class="org.springframework.web.accept.contentnegotiationmanagerfactorybean">     <property name="defaultcontenttype" value="application/json" /> </bean>  <mvc:annotation-driven content-negotiation-manager="contentnegotiationmanager" /> 

source:

http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -