@ResponseBody和@RequestBody区别


本文主要介绍通过ajax提交表单后,@ResponseBody和@RequestBody的使用。

一、概念

@RequestBody

作用: 

   

注解用于将Controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为JAVA类


使用时机:

A)POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);


B) PUT方式提交时, 根据request header Content-Type的值来判断:


  •     application/x-www-form-urlencoded, 必须;
  •     multipart/form-data, 不能处理;
  •     其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;

@ResponseBody

作用: 

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;


二、案例结构


三、关键代码


UserController:


    
    
  1. package com.edwin.user.controller;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import com.edwin.user.vo.UserVo;
  11. @Controller
  12. @RequestMapping( "user")
  13. public class UserController {
  14. private List<UserVo> userList = new ArrayList<UserVo>();
  15. public UserController() {
  16. for ( int i = 1; i <= 5; i++) {
  17. UserVo uv = new UserVo();
  18. uv.setUserID(i);
  19. uv.setUserName( "user"+i);
  20. uv.setGender(i% 2);
  21. uv.setPhone( "1380013800"+i);
  22. uv.setEmail( "email@12"+i+ ".com");
  23. uv.setBirthday( new Date( 90, 2, 23));
  24. uv.setPassword( "123456");
  25. userList.add(uv);
  26. }
  27. }
  28. @RequestMapping( "getUsers.do")
  29. @ResponseBody
  30. public List<UserVo> getUsers(){
  31. return userList;
  32. }
  33. /*
  34. 以POST提交方式,添加一个新用户
  35. * @param vo
  36. * @return
  37. */
  38. @RequestMapping(value= "addUser.do", method=RequestMethod.POST,consumes = "application/json")
  39. @ResponseBody
  40. public int addUser(@RequestBody UserVo vo){
  41. System.out.println(vo);
  42. userList.add(vo);
  43. return 1;
  44. }
  45. }


index.html


    
    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here </title>
  6. </head>
  7. <body>
  8. <a href="user/getUsers.do">查看所有用户 </a>
  9. <a href="insert.html">添加用户 </a>
  10. </body>
  11. </html>


insert.html


    
    
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Store </title>
  6. <script type="text/javascript" src="js/jquery-1.8.2.min.js"> </script>
  7. <script type="text/javascript">
  8. function addUser() {
  9. var name = $(<span class="hljs-string">"#userName"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> password = $( "#password").val();
  10. var email = $(<span class="hljs-string">"#email"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> phone = $( "#phone").val();
  11. var gender = $(<span class="hljs-string">"#gender"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> birthday = $( "#birthday").val();
  12. var sendInfo = {
  13. userName: name,
  14. password: password,
  15. email: email,
  16. phone: phone,
  17. gender: gender,
  18. birthday: birthday,
  19. };
  20. $.ajax({
  21. type: "POST",
  22. url: "user/addUser.do",
  23. contentType : 'application/json', //默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。
  24. dataType: "json", //预期服务器返回的数据类型
  25. success: function (msg) {
  26. if (msg) {
  27. alert(name + " was added in list !");
  28. } else {
  29. alert( "Cannot add to list !");
  30. }
  31. }, error: function(msg){
  32. alert( "failed");
  33. },
  34. data: JSON.stringify(sendInfo)
  35. });
  36. }
  37. </script>
  38. </head>
  39. <body>
  40. Post 提交
  41. <form action="" method="post">
  42. userName: <input type="text" id ="userName" value="david"/> <br>
  43. password: <input type="text" id ="password" value="111111"/> <br>
  44. email: <input type="text" id ="email" value="[email protected]"/> <br>
  45. phone: <input type="text" id ="phone" value="13800138002"/> <br>
  46. gender: <input type="text" id ="gender" value="1"> <br>
  47. birthday: <input type="text" id ="birthday" value="1990-06-30"> <br>
  48. <input type="button" value="OK" onclick="addUser()"/>
  49. </form>
  50. </body>
  51. </html>


四、效果图

4.1查看所有用户


4.2添加用户




本文主要介绍通过ajax提交表单后,@ResponseBody和@RequestBody的使用。

一、概念

@RequestBody

作用: 

   

注解用于将Controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为JAVA类


使用时机:

A)POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);


B) PUT方式提交时, 根据request header Content-Type的值来判断:


  •     application/x-www-form-urlencoded, 必须;
  •     multipart/form-data, 不能处理;
  •     其他格式, 必须;
说明:request的body部分的数据编码格式由header部分的Content-Type指定;

@ResponseBody

作用: 

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;


二、案例结构


三、关键代码


UserController:


  
  
  1. package com.edwin.user.controller;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestBody;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import com.edwin.user.vo.UserVo;
  11. @Controller
  12. @RequestMapping( "user")
  13. public class UserController {
  14. private List<UserVo> userList = new ArrayList<UserVo>();
  15. public UserController() {
  16. for ( int i = 1; i <= 5; i++) {
  17. UserVo uv = new UserVo();
  18. uv.setUserID(i);
  19. uv.setUserName( "user"+i);
  20. uv.setGender(i% 2);
  21. uv.setPhone( "1380013800"+i);
  22. uv.setEmail( "email@12"+i+ ".com");
  23. uv.setBirthday( new Date( 90, 2, 23));
  24. uv.setPassword( "123456");
  25. userList.add(uv);
  26. }
  27. }
  28. @RequestMapping( "getUsers.do")
  29. @ResponseBody
  30. public List<UserVo> getUsers(){
  31. return userList;
  32. }
  33. /*
  34. 以POST提交方式,添加一个新用户
  35. * @param vo
  36. * @return
  37. */
  38. @RequestMapping(value= "addUser.do", method=RequestMethod.POST,consumes = "application/json")
  39. @ResponseBody
  40. public int addUser(@RequestBody UserVo vo){
  41. System.out.println(vo);
  42. userList.add(vo);
  43. return 1;
  44. }
  45. }


index.html


  
  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here </title>
  6. </head>
  7. <body>
  8. <a href="user/getUsers.do">查看所有用户 </a>
  9. <a href="insert.html">添加用户 </a>
  10. </body>
  11. </html>


insert.html


  
  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Store </title>
  6. <script type="text/javascript" src="js/jquery-1.8.2.min.js"> </script>
  7. <script type="text/javascript">
  8. function addUser() {
  9. var name = $(<span class="hljs-string">"#userName"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="11"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> password = $( "#password").val();
  10. var email = $(<span class="hljs-string">"#email"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="13"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> phone = $( "#phone").val();
  11. var gender = $(<span class="hljs-string">"#gender"</span>).val();</div></div></li><li><div class="hljs-ln-numbers"><div class="hljs-ln-line hljs-ln-n" data-line-number="15"></div></div><div class="hljs-ln-code"><div class="hljs-ln-line"> <span class="hljs-keyword">var</span> birthday = $( "#birthday").val();
  12. var sendInfo = {
  13. userName: name,
  14. password: password,
  15. email: email,
  16. phone: phone,
  17. gender: gender,
  18. birthday: birthday,
  19. };
  20. $.ajax({
  21. type: "POST",
  22. url: "user/addUser.do",
  23. contentType : 'application/json', //默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。
  24. dataType: "json", //预期服务器返回的数据类型
  25. success: function (msg) {
  26. if (msg) {
  27. alert(name + " was added in list !");
  28. } else {
  29. alert( "Cannot add to list !");
  30. }
  31. }, error: function(msg){
  32. alert( "failed");
  33. },
  34. data: JSON.stringify(sendInfo)
  35. });
  36. }
  37. </script>
  38. </head>
  39. <body>
  40. Post 提交
  41. <form action="" method="post">
  42. userName: <input type="text" id ="userName" value="david"/> <br>
  43. password: <input type="text" id ="password" value="111111"/> <br>
  44. email: <input type="text" id ="email" value="[email protected]"/> <br>
  45. phone: <input type="text" id ="phone" value="13800138002"/> <br>
  46. gender: <input type="text" id ="gender" value="1"> <br>
  47. birthday: <input type="text" id ="birthday" value="1990-06-30"> <br>
  48. <input type="button" value="OK" onclick="addUser()"/>
  49. </form>
  50. </body>
  51. </html>


四、效果图

4.1查看所有用户


4.2添加用户



猜你喜欢

转载自blog.csdn.net/majishushu/article/details/81094535