@ResponseBody与ResponseEntity

转载 2016年05月31日 13:45:48

第一篇文章:

from:

http://stackoverflow.com/questions/22725143/what-is-the-difference-between-responseentityt-and-responsebody

ResponseEntity will give you some added flexibility in defining arbitrary HTTP response headers. See the 4th constructor here:

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/ResponseEntity.html

ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode) 

A List of possible HTTP response headers is available here:

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses

Some commonly-used ones are Status, Content-Type and Cache-Control.

If you don't need that, using @ResponseBody will be a tiny bit more concise.



第二篇文章:

from:

http://stackoverflow.com/questions/26549379/when-use-responseentityt-and-restcontroller-for-spring-restful-applications

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.

Basically, ResponseEntity lets you do more.



第三篇文章:

from:

http://www.concretepage.com/spring-4/spring-4-mvc-jsonp-example-with-rest-responsebody-responseentity

In this page we will provide Spring 4 MVC and JSONP example with REST, @ResponseBody and ResponseEntity. JSONP is JSON with padding. It supports JavaScript code running in web browser to request data from a server in different domain which is normally prohibited 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

myfunction({"bookName":"Godan","writer":"Premchand"});
 
then in another domain, we can use it as
<script src=" http://localhost:8080/concretepage-1/book1?callback=myfunction " type="application/javascript"> </script>
 
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
@ControllerAdvice
public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
    public JsonpAdvice() {
        super("callback");
    }
} 
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 and ResponseEntity.

JSONP with Spring REST

Find the bean being used in the example to generate JSON.
Book.java
public class Book {
        private String bookName;
        private String writer;
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
} 
We are creating a web service which will respond JSONP.
BookService.java
@RestController
class BookService {
    @RequestMapping(value= "/book1", produces = MediaType.APPLICATION_JSON_VALUE)
    Book bookInfo1() {
    	Book book = new Book();
    	book.setBookName("Godan");
    	book.setWriter("Premchand");
        return book;
    }
} 
When we access the URL http://localhost:8080/concretepage-1/book1?callback=functionCall, it will throw the response as
functionCall({"bookName":"Godan","writer":"Premchand"});
 

JSONP with @ResponseBody and ResponseEntity

Now find a controller in which we have created methods that will return @ResponseBody and ResponseEntity.
@Controller
class BookController {
    @RequestMapping(value ="/book2", produces =MediaType.APPLICATION_JSON_VALUE )
    @ResponseBody
    Book bookInfo2() {
    	Book book = new Book();
    	book.setBookName("Ramcharitmanas");
    	book.setWriter("TulasiDas");
    	return book;
    }
    @RequestMapping(value ="/book3", produces =MediaType.APPLICATION_JSON_VALUE )
    public ResponseEntity<Book> bookInfo3() {
    	Book book = new Book();
    	book.setBookName("Ramayan");
    	book.setWriter("Valmiki");
        return ResponseEntity.accepted().body(book);
    } 
} 
When we access the URL http://localhost:8080/concretepage-1/book2?callback=functionCall, we will get the output in browser as
functionCall({"bookName":"Ramcharitmanas","writer":"TulasiDas"});
 
And for the URL http://localhost:8080/concretepage-1/book3?callback=functionCall, the output will be
functionCall({"bookName":"Ramayan","writer":"Valmiki"});
 

JSONP Client Code

Now we will write client code which can be used in any other domain.
jsonptest.html
<html>
 <head>
   <script>
     function functionCall(data) {   
		console.log(data.bookName);
		console.log(data.writer);
		console.log('-----------');
     }
   </script>
  </head>
  <body>
        <!-- Using REST URL-->
        <script src="http://localhost:8080/concretepage-1/book1?callback=functionCall" type="application/javascript"> </script>
	
	<!--Using @ResponseBody -->
	<script src="http://localhost:8080/concretepage-1/book2?callback=functionCall" type="application/javascript"> </script>
	
	<!--Using ResponseEntity -->
	<script src="http://localhost:8080/concretepage-1/book3?callback=functionCall" type="application/javascript"> </script>
  </body>
</html> 
Run this page and check the output in console.
Spring 4 MVC  + JSONP Example with REST, @ResponseBody and ResponseEntity


Now we are done. Happy Learning!

Download Source Code


相关文章推荐

解决SpringMVC返回的ResponseEntity出现乱码

问题:前台通过数据到后台查询相关数据,结果是后台返回的数据出现中文出现乱码,如下图所示 原因:对于String类型,在没有在代码中明确指定编码类型的情况下,可能后台对传输的数据没有正确编码(本人的...

SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity

在传统的开发过程中,我们的控制CONTROLL层通常需要转向一个JSP视图;但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面。   SPRING MVC3的@...

SpringMVC中controller中方法返回类型为ResponseEntity<String>乱码的问题

返回类型为ResponseEntity 代表我们返回的数据是一个对象,在springMVC中,请求数据到对象和对象到响应数据的转换是通过消息转换器来完成的。 HttpMessageConverter是...

spring mvc @ResponseStatus 和 ResponseEntity

@ResponseStatus 是标记一个方法或异常类在返回时响应的http状态。其代码注释如下:* * The status code is applied to the HTTP response...

SpringMVC使用ResponseEntity实现文件下载

上一篇写了文件上传,文件下载的配置过程同文件上传,有需要的请http://blog.csdn.net/c______________/article/details/77512017 本文主要通过R...

JavaWEB后端支付银联,支付宝,微信对接

JavaWEB后端支付银联,支付宝,微信对接标签(空格分隔): java项目概述最近项目需要后端打通支付,所以对接部分做成了一个小模块. 先说下项目要求: 后端要对接银联无跳转Token支付,支付...

解决 ie 返回json提示下载 ResponseEntity方法

js 配合java  springMVC后台,成功后返回消息,chrom ff都正常,只有IE提交后返回的JSON提示下载,查看类型 application/json  google后发现原来是IE...

CSDN一天到晚搞什么

CSDN一天到晚不知在搞什么,又换编辑器了,也不见这个比以前的好,应该说是从来就没好用过。

spring aop 无法在内部嵌套的方法上生效

1,问题例子:public void method1(){ ......... method2(); ........... } public void method2(){ .....

Java POI 读取Office excel (2003,2007)及相关jar包

poi-3.7-20101029.jar,下载地址:http://download.csdn.net/detail/evangel_z/4107089 geronimo-stax-api_1.0_s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)