Spring MVC @ResponseBody

作者:範宗雲 来源:原创 发布时间:2015-03-23 归档:springmvc

开发环境 : JDK 7 Maven 3 Tomcat 7 Spring 4.1.5 Eclipse Luna
@ResponseBody 注解表明处理方法的返回值不作为视图名称来解析, 而直接写出到 Http Response Body。
示例代码片段 1 ( pojo )
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/latest", method = POST)
				public @ResponseBody Product latestProduct() {
					return new Product(1001, "戴尔-XPS-13(XPS13D-9343-1508)笔记本", 6999);
				}

				public static class Product {
			
					private int id;
					private String name;
					private double price;
			
					public Product() {
			
					}
			
					public Product(int id, String name, double price) {
						this.id = id;
						this.name = name;
						this.price = price;
					}
			
					// ignore getters and setters
			
				}
			
			}
			
			<p>
			  <textarea disabled="disabled"></textarea>
			  <button id="latest">latest</button>
			</p>
			<script type="text/javascript">
			  $(function(){
				  $("#latest").click(function() {
					  var self = $(this);
					  _post(self, function(data){
						  var result = data.id + "  " + data.name + "  " + data.price;
						  self.parent().find("textarea").val(result);
					  });
				  });
				  var _post = function(e, _callback) {
					  $.ajax({
						  type : "post",
						  url  : "product/" + e.text(),
						  success : function(result) {
							  _callback(result);
						  }
					  });
				  }
			  });
			</script>
			
示例代码片段 2 ( list )
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/serial", method = POST)
				public @ResponseBody List<Product> serialProduct() {
					List<Product> list = new ArrayList<>();
					list.add(new Product(1001, "戴尔-XPS-13(XPS13D-9343-1508)笔记本", 6999));
					list.add(new Product(1002, "戴尔-XPS-13(XPS13D-9343-1708)笔记本", 8999));
					return list;
				}
			
			}
			
			<p>
			  <textarea disabled="disabled"></textarea>
			  <button id="serial">serial</button>
			</p>
			<script type="text/javascript">
			  $(function(){
				  $("#serial").click(function() {
					  var self = $(this);
					  _post(self, function(data){
						  var result = "";
						  $.each(data, function(i, v){
							  result += v.id + "  " + v.name + "  " + v.price + "\r\n";
						  });
						  self.parent().find("textarea").val(result);
					  });
				  });
			  });
			</script>
			
示例代码片段 3 ( map )
			@Controller
			@RequestMapping("/product")
			public class ProductController {
			
				@RequestMapping(value = "/brand", method = POST)
				public @ResponseBody Map<String, Product> brandProduct() {
					Map<String, Product> map = new HashMap<>();
					map.put("dell", new Product(1003, "戴尔-XPS-13(XPS13D-9343-1808T)笔记本", 13000));
					return map;
				}
			
			}
			
			<p>
			  <textarea disabled="disabled"></textarea>
			  <button id="brand">brand</button>
			</p>
			<script type="text/javascript">
			  $(function(){
				  $("#brand").click(function() {
					  var self = $(this);
					  _post(self, function(data){
						  var result = data.dell.id + "  " + data.dell.name + "  " + data.dell.price;
						  self.parent().find("textarea").val(result);
					  });
				  });
			  });
			</script>
			
不要忘记引入依赖
		    <dependency>
		      <groupId>com.fasterxml.jackson.core</groupId>
		      <artifactId>jackson-databind</artifactId>
		      <version>2.4.1</version>
		    </dependency>
		    

示例代码下载
spring-mvc-response-body.zip