0

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.

Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
  • Essentially this looks to be a duplicate of http://stackoverflow.com/questions/10085088/jackson-annotations-being-ignored-in-spring – Steve Nov 11 '13 at 16:04

1 Answers1

1

I think you might be better off following the tutorial provided at spring.io:

http://spring.io/guides/gs/rest-service/

If you're using a recent version of Spring, it automatically does Jackson marshalling. So you shouldn't have any of the code above.

Steve
  • 9,270
  • 5
  • 47
  • 61
  • The problem is that I need the response to be marshaled into [JSONP](http://en.wikipedia.org/wiki/JSONP), not JSON, to be able to bypass [the same origin policy](http://en.wikipedia.org/wiki/Same-origin_policy). – Dušan Rychnovský Nov 11 '13 at 16:14
  • In that case, use the answer provided here: http://stackoverflow.com/questions/15282617/implementing-jsonp-in-spring-mvc-3-2 – Steve Nov 11 '13 at 16:23
  • Thank you. Despite the fact that this solution (IMO) breaks the MVC abstraction rules (presentation issues are dealt with inside the controller) it seems to be the best currently available one for Spring based REST services. – Dušan Rychnovský Nov 11 '13 at 16:59
  • It's a bit puzzling actually. Spring-data-rest does provide built in JSONP config (https://github.com/spring-projects/spring-data-rest/wiki/JSONP-Support-in-Spring-Data-REST), but Spring MVC doesn't seem to support it out of the box yet. Maybe soon? – Steve Nov 11 '13 at 17:20