Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.1 REST support and exposing REST Services accessible via JSONP requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1 REST support and exposing REST Services accessible via JSONP requests

    I have written a number of REST web services using Spring 3.1 and the REST support introduced int 3.x. I have no issue accessing these services with something like JQuery when originating from the same domain in which these services are deployed.

    However, I have been running into issues trying to access these from different domains via a JSONP request. I have been looking through the forums to see what others have done in this scenario, but I don't seem to be finding anyone doing this.

    Does anyone have any insight on what I need to do differently to my REST Services to allow them to be accessible via a JSONP request?

    Here are a few of my endpoints in my controller.
    Code:
    @RequestMapping( method = RequestMethod.GET, consumes = { "application/json" }, produces = { "application/json" } )
    public @ResponseBody List<Restaurant> restaurantsJson() {
    	return service.list();
    }
    
    @RequestMapping( method = RequestMethod.GET, consumes = { "application/xml", "text/xml" }, produces = { "application/xml", "text/xml" } )
    public @ResponseBody List<Restaurant> restaurantsXml() {
    	return service.list();
    }
    
    	@RequestMapping( value = "{restaurantId}", method = RequestMethod.GET, consumes = { "application/json" }, produces = { "application/json" } )
    
    public @ResponseBody Restaurant restaurantJson( @PathVariable Long restaurantId ) {
    	return service.findById( restaurantId );
    }
    	
    @RequestMapping( value = "{restaurantId}", method = RequestMethod.GET, consumes = { "application/xml", "text/xml" }, produces = { "application/xml", "text/xml" } )
    public @ResponseBody Restaurant restaurantXml( @PathVariable Long restaurantId ) {
    	return service.findById( restaurantId );
    }
    When I try to call these via a JSONP request with jquery, in firebug or chrome inspector, I never seem to be able to set the content type to 'application/json' or 'application/xml' so these are never getting executed. So the end up erroring out because these same endpoints also return an html view, and that is what gets returned on a JSONP request.

    Thanks for any assistance.

  • #2
    You'll need the functionality of the MappingJacksonHttpMessageConverter but producing "application/javascript" rather than "application/json" and also adding the json padding. One option would be for the controller method to return a special type (e.g. JsonpContainer):

    Code:
    public class JsonpContainer {
    
      private final String padding;
    
      private final Object data;
    
      // ...
    
    }
    Your custom converter can then add the padding and delegate to its parent for serializing the body. The MappingJacksonHttpMessageConverter actually supports a prefixJson property, which is very similar to what you would need to do.

    Comment

    Working...
    X