ResponseEntity is meant to represent the entire HTTP response. You can control anything that goes into it: status code, headers, and body.
@ResponseBody is a marker for the HTTP response body and @ResponseStatus declares the status code of the HTTP response.
@ResponseStatus isn't very flexible. It marks the entire method so you have to be sure that your handler method will always behave the same way. And you still can't set the headers. You'd need the
HttpServletResponse or a HttpHeaders parameter.
because of same-origin policy. According to this policy a web browser can allow the script code of one web browser to access data from another web browser within the same domain. Same-origin policy is because of web application security model. But this policy
is not forced to <script> tag by web browser. From here the role of JSONP comes into picture. JSONP allows to access data from different domain using<script> tag by web browser. If we have a URL as http://localhost:8080/concretepage-1/book1?callback=myfunction
and if it throws the JSONP response as
In our client code to access JSONP, there should already be a function named
myfunction() defined in script code.To throw the JSONP response using spring, it provides AbstractJsonpResponseBodyAdvice class which is implemented by a class annotated with @ControllerAdvice.
Spring 4 Support for JSONP with AbstractJsonpResponseBodyAdvice
Find the our class which will support JSONP response. JsonpAdvice.java
In the constructor, we need to call super method passing a key. This key will be used in query string of URL while requesting JSONP data. The above method facilitates REST web service to respond JSONP data and also controller method which respond using @ResponseBody
JSONP with Spring REST
Find the bean being used in the example to generate JSON. Book.java