标签:
1 @RequestMapping(value = "/hello") 2 public ModelAndView handleRequest(UserModel um,HttpServletResponse response) throws IOException { 3 response.setCharacterEncoding("utf-8"); 4 response.getWriter().println("{uuid:‘"+um.getUuid()+"‘,name:‘"+um.getName()+"‘}"); 5 return null; 6 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4 </head> 5 <script language="javascript" src="/mvcexample/static/js/jquery-1.3.2.min.js"></script> 6 <script language="javascript"> 7 $().ready(function(){ 8 $.getJSON(‘/mvcexample/hello‘,{uuid:‘1‘,name:‘test‘},function(data){ 9 alert(data.uuid+" , "+data.name); 10 }); 11 }); 12 </script> 13 </html>
1 @RequestMapping(value = "/hello") 2 public byte[] handleRequest(@RequestBody String body)
@RequestMapping(value = “/hello") @ResponseBody public byte[] handleRequest(@RequestBody String body)
1 @RequestMapping(value = "/hello") 2 @ResponseBody 3 public UserModel handleRequest(@RequestBody String reqBody, UserModel um) { 4 System.out.println("the reqBody="+reqBody); 5 um.setName(um.getName()+",server"); 6 return um; 7 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 3 </head> 4 <script language="javascript" src="/mvcexample/static/js/jquery-1.3.2.min.js"></script> 5 <script language="javascript"> 6 $().ready(function(){ 7 $.getJSON(‘/mvcexample/hello‘,{uuid:‘1‘,name:‘test‘},function(data){ 8 alert(data.uuid+" , "+data.name); 9 }); 10 }); 11 </script>
1 @RequestMapping(value = "/hello") 2 @ResponseBody 3 public List<UserModel> handleRequest(@RequestBody String reqBody, UserModel um) { 4 System.out.println("the reqBody="+reqBody); 5 um.setName(um.getName()+",server"); 6 List<UserModel> list = new ArrayList<UserModel>(); 7 list.add(um); 8 UserModel um2 = new UserModel(); 9 um2.setUuid("22"); 10 um2.setName("222"); 11 list.add(um2); 12 return list; 13 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> 3 <script language="javascript" src="/mvcexample/static/js/jquery-1.3.2.min.js"></script> 4 <script language="javascript"> 5 $().ready(function(){ 6 $.getJSON(‘/mvcexample/hello‘,{uuid:‘1‘,name:‘test‘},function(data){ 7 $.each(data,function(index,v){ 8 alert("tr="+v.uuid+",v="+v.name); 9 }); 10 }); 11 }); 12 </script>
1 @RequestMapping(value = "/hello") 2 public ResponseEntity<List<UserModel>> handleRequest(HttpEntity<String> req, UserModel um) { 3 System.out.println("req headers="+req.getHeaders()+", reqBody="+req.getBody()); 4 5 um.setName(um.getName()+",server"); 6 List<UserModel> list = new ArrayList<UserModel>(); 7 list.add(um); 8 UserModel um2 = new UserModel(); 9 um2.setUuid("22"); 10 um2.setName("222"); 11 list.add(um2); 12 13 ResponseEntity<List<UserModel>> ret = new ResponseEntity<List<UserModel>>(list,HttpStatus.OK); 14 return ret; 15 }
1 @XmlRootElement(name = "testxml") 2 public class UserModel {
1 @RequestMapping(value = "/hello") 2 @ResponseBody 3 public UserModel handleRequest(HttpEntity<String> req, UserModel um) { 4 System.out.println("req headers="+req.getHeaders()+", reqBody="+req.getBody()); 5 6 um.setName(um.getName()+",server"); 7 8 PhoneNumberModel pnm = new PhoneNumberModel("123","321"); 9 PhoneNumberModel pnm2 = new PhoneNumberModel("2222","333"); 10 List<PhoneNumberModel> tempList = new ArrayList<PhoneNumberModel>(); 11 tempList.add(pnm2); 12 tempList.add(pnm); 13 14 um.setPm(tempList); 15 16 return um; 17 }
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> 3 <script language="javascript" src="/mvcexample/static/js/jquery-1.3.2.min.js"></script> 4 <script language="javascript"> 5 $().ready(function(){ 6 $.ajax({ 7 url:‘/mvcexample/hello‘, 8 type: ‘POST‘, 9 dataType: ‘xml‘, 10 data: {uuid:‘1‘,name:‘test‘}, 11 timeout: 1000, 12 error: function(){ alert(‘Error loading XML document‘); }, 13 success: function(xml){ 14 $(xml).find("testxml").children("pm").each(function(i){ 15 var uuid=$(this).children("areaCode").text(); 16 var name=$(this).children("phoneNumber").text(); 17 alert("uuid="+uuid+",name="+name); 18 19 }); } }); }); 20 </script>
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 2 <testxml> 3 <age>0</age> 4 <name>test,server</name> 5 <pm> 6 <areaCode>2222</areaCode> 7 <phoneNumber>333</phoneNumber> 8 </pm> 9 <pm> 10 <areaCode>123</areaCode> 11 <phoneNumber>321</phoneNumber> 12 </pm> 13 <uuid>1</uuid> 14 </testxml>
1 <!--1、检查扩展名(如my.pdf);2、检查Parameter(如my?format=pdf);3、检查Accept Header--> 2 <bean id= "contentNegotiationManager" class= "org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 3 <!-- 扩展名至mimeType的映射,即 /user.json => application/json --> 4 <property name= "favorPathExtension" value= "true" /> 5 <!-- 用于开启 /userinfo/123?format=json 的支持 --> 6 <property name= "favorParameter" value= "true" /> 7 <property name= "parameterName" value= "format"/> 8 <!-- 是否忽略Accept Header --> 9 <property name= "ignoreAcceptHeader" value= "false"/> 10 <property name= "mediaTypes"> <!--扩展名到MIME的映射;favorPathExtension, favorParameter是true时起作用 --> 11 <value> 12 ccjson=application/json 13 ccxml=application/xml 14 html=text/html 15 </value> 16 </property> 17 <!-- 默认的content type --> 18 <property name= "defaultContentType" value= "text/html" /> 19 </bean> 20 21 <!-- ========================= VIEW定义 ========================= --> 22 <!-- 内容协商视图解析器;根据客户端不同的请求决定不同的view进行响应 --> 23 <!-- 会自动根据解析的contentType来决定使用哪个视图解析器(默认使用整个web应用中的viewResolver) --> 24 <bean class= "org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order= "0"> 25 <!-- 内容协商管理器 用于决定media type --> 26 <property name= "contentNegotiationManager" ref= "contentNegotiationManager"/> 27 <!-- 默认视图 放在解析链最后 --> 28 <property name= "defaultViews"> 29 <list> 30 <bean class= "org.springframework.web.servlet.view.json.MappingJackson2JsonView"/> 31 <bean class= "org.springframework.web.servlet.view.xml.MarshallingView"> 32 <property name= "marshaller"> 33 <bean class= "org.springframework.oxm.jaxb.Jaxb2Marshaller"> 34 <property name= "packagesToScan" value= "cn.javass"></property> 35 </bean> 36 </property> 37 </bean> 38 </list> 39 </property> 40 </bean> 41 42 <!-- bean name view resolver--> 43 <bean class= "org.springframework.web.servlet.view.BeanNameViewResolver" p:order= "1"/> 44 <!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用 html)- --> 45 <bean id= "defaultViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver" p:order= "2"> 46 <property name= "viewClass" value= "org.springframework.web.servlet.view.JstlView"/> 47 <property name= "contentType" value= "text/html"/> 48 <property name= "prefix" value= "/WEB-INF/jsp/"/> 49 <property name= "suffix" value= ".jsp"/> 50 </bean> 51 <!-- 在mvc:annotation-driven里面配置使用内容协商--> 52 <mvc:annotation-driven 53 validator= "validator" 54 conversion-service= "conversionService" 55 content-negotiation-manager= "contentNegotiationManager" 56 > 57 </mvc:annotation-driven> 58
标签:
原文地址:http://www.cnblogs.com/lzjoy/p/4877440.html