Adds support for jsonp requests in Spring MVC controllers.
This project contains artifacts you can use for adding support for jsonp in your Spring MVC Controllers.
You can also use this in your Spring Roo projects to add jsonp support for controllers generated using @RooJson
and @RooWebJson
.
For more information on how this works, please read the following article.
Special thanks to the original author of the article.
-
Checkout the project from github or download the tar/zip file and extract it.
-
Run
mvn clean install
-
Add the following dependency to your project.
<dependency> <groupId>com.intera.util</groupId> <artifactId>spring-jsonp-support</artifactId> <version>${version}</version> </dependency>
-
Modify and add the following lines to each of the following files.
applicationContext.xml
<bean class="com.intera.util.web.servlet.filter.JsonpCallbackFilter" id="jsonpCallbackFilter" />
web.xml
<filter>
<filter-name>jsonpCallbackFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>jsonpCallbackFilter</filter-name>
<url-pattern>*.json</url-pattern>
</filter-mapping>
OR
In Sprintboot
@Bean
public FilterRegistrationBean myFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new JsonpCallbackFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter("paramName", "paramValue");
registration.setName("JsonpCallbackFilter");
registration.setOrder(1);
return registration;
}
- When you are making the request, add .json at the end of the jsonp request and use a callback parameter.
Example : http://localhost:8080/mylistingsapp/listings.json?callback=myFunction