I am building a simple REST service which should return the data encoded as either JSON or JSONP (depending on what the client requests). I have followed the tutorial on vivin.net.
WEB-INF/config/config.xml:
<beans ...>
...
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="favorPathExtension" value="true"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="jsonp" value="application/javascript" />
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<bean class="cz.dusanrychnovsky.utils.json.MappingJacksonJsonpView" />
</list>
</property>
</bean>
</beans>
MappingJacksonJsonpView.java
public class MappingJacksonJsonpView extends MappingJacksonJsonView
{
public static final String DEFAULT_CONTENT_TYPE = "application/javascript";
@Override
public String getContentType() {
return DEFAULT_CONTENT_TYPE;
}
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
...
}
}
When I try to request http://localhost:8080/service/resource.jsonp
, though, Spring would still use the MappingJacksonJsonView (as the log reveals) and returns the output encoded as JSON (instead of JSONP).
What am I doing wrong?
In case I have omitted some important details, please ask for them. I will update the post right away.