@Controller
public class ProductController {
/**
* 用于将Controller方法返回的对象,通过HttpMessageConverter转化为指定格式
* 写入到Response对象的body数据区
*/
@RequestMapping("product/queryProduct")
@ResponseBody
public Product queryProduct(){
Product p=new Product();
p.setId(1);
p.setName("product");
return p;
}
//另一种写法
public @ResponseBody Product queryProduct1(){
Product p=new Product();
return p;
}
@RequestMapping("product/queryProducts")
@ResponseBody
public List<Product> queryProducts(){
List<Product> products=new ArrayList<Product>();
Product p1=new Product();
p1.setId(1);
p1.setName("product1");
Product p2=new Product();
p2.setId(2);
p2.setName("product2");
products.add(p1);
products.add(p2);
return products;
}
@RequestMapping("product/addProduct")
@ResponseBody
public Product queryProduct2(@RequestBody Product product){
System.out.println(product.getId()+" "+product.getName());
return product;
}
}
ajax
function addProduct(){
$.ajax({
type:"post",
url:"product/addProduct.do",
contentType:"application/json;charset=utf-8",
data:'{"id":"1","name":"admin"}',
dataType:"json",
success:function (data){
console.log(data);
alert(data.id);
}
})
}
标签:product,Product,json,数据处理,new,data,public
From: https://www.cnblogs.com/lwx11111/p/16787816.html