本来想学习一下@RequestBody这个注解,但发请求时出现跨域问题,所以就先来看一下跨域该怎么办。
CORS
首先因为最近在做一个前后端分离的项目,分开就意味着可能不在一个域中,所以不可避免的遇到CORS的问题。试过几个方法:
- Spring MVC 4.2.5以后新增的支持跨域的注解@CrossOrigin,如果是老项目的话升级spring库可能会有些兼容的问题,不知为什么这个注解没有升效;
- 用反向代理,这个一定好使的;
-
还有就是我现在使用的,手动增加一个Filter,在Response中增加对跨域的支持,这种方式对老浏览器可能会有问题。
CORSFilter.Java
public class CORSFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); chain.doFilter(req, res); } public void init(FilterConfig filterConfig) {} public void destroy() {} }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
web.xml
<filter>
<filter-name>cors</filter-name>
<filter-class>xxxx.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
如此即可解决跨域问题。
@RequestBody
下面说说@RequestBody
这个注解。因为一直觉得传简单对象直接不用注解也可以自动组装,就以为spring可以为我们组装所有类型的对象。但今天测试过,原来象Map,List或复合对象都必需使用这个注解才可以正确组装,以下是我的测试代码:
首先写个测试的Controller:
RequestBodyController.java
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.cunframework.core.common.controller.BaseController;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
class A{
private String name;
private String value;
public String getName() {
return name;
}
public String getValue() {
return value;
}
public void setName(String name) {
this.name = name;
}
public void setValue(String value) {
this.value = value;
}
}
class B{
private List<A> a;
private String count;
public List<A> getA() {
return a;
}
public String getCount() {
return count;
}
public void setA(List<A> a) {
this.a = a;
}
public void setCount(String count) {
this.count = count;
}
}
@Scope("prototype")
@Controller
@RequestMapping("/rb")
public class RequestBodyController extends BaseController{
@RequestMapping(value = "test1")
@ResponseBody
public Map test1(A a,HttpServletResponse response) {
try {
System.out.println(JSON.toJSONString(a));
return toSuccess();
} catch (Exception e) {
e.printStackTrace();
return toError();
}
}
@RequestMapping(value = "test2")
@ResponseBody
public Map test2(List<A> a,HttpServletResponse response) {
try {
System.out.println(JSON.toJSONString(a));
return toSuccess();
} catch (Exception e) {
e.printStackTrace();
return toError();
}
}
@RequestMapping(value = "test3")
@ResponseBody
public Map test3(@RequestBody List<A> a,HttpServletResponse response) {
try {
System.out.println(JSON.toJSONString(a));
return toSuccess();
} catch (Exception e) {
e.printStackTrace();
return toError();
}
}
@RequestMapping(value = "test4")
@ResponseBody
public Map test4(B b,HttpServletResponse response) {
try {
System.out.println(JSON.toJSONString(b));
return toSuccess();
} catch (Exception e) {
e.printStackTrace();
return toError();
}
}
@RequestMapping(value = "test5")
@ResponseBody
public Map test5(@RequestBody B b,HttpServletResponse response) {
try {
System.out.println(JSON.toJSONString(b));
return toSuccess();
} catch (Exception e) {
e.printStackTrace();
return toError();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
测试用的html文件:
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script> function ajax(url,data,contentType){ $.ajax({ type: "POST", contentType: contentType, url: url, data: data, success: function(data){ console.log(data); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Error Occured!"); } }); } function test1(){ var data = { name:123, value:456 }; ajax( "http://localhost:8080/orcs/rb/test1.do", data, "application/x-www-form-urlencoded" ) } function test2(){ var data = [ { name:123, value:456 }, { name:222, value:222 }, { name:333, value:444456 } ]; ajax( "http://localhost:8080/orcs/rb/test2.do", data, "application/x-www-form-urlencoded" ); } function test3(){ var data = [ { name:123, value:456 }, { name:222, value:222 }, { name:333, value:444456 } ]; ajax( "http://localhost:8080/orcs/rb/test3.do", JSON.stringify(data), "application/json" ); } function test4(){ var data = { a:[{ name:123, value:456 }, { name:222, value:222 }, { name:333, value:444456 }], count:222 } ajax( "http://localhost:8080/orcs/rb/test4.do", data, "application/x-www-form-urlencoded" ); } function test5(){ var data = { a:[{ name:123, value:456 }, { name:222, value:222 }, { name:333, value:444456 }], count:222 }; ajax( "http://localhost:8080/orcs/rb/test5.do", JSON.stringify(data), "application/json" ); } </script>
</head>
<body>
<button onclick="test1()">btn1</button>
<button onclick="test2()">btn2</button>
<button onclick="test3()">btn3</button>
<button onclick="test4()">btn4</button>
<button onclick="test5()">btn5</button>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
发现@RequestBody这个注解,使用时对ajax有些要求:
- 数据必需要用
JSON.stringify(data)
处理; contentType
类型必需为application/json
。
而不加这个注解组装简单对象时,则:
1. 数据直接用json不必处理;
2. contentType
类型默认为application/x-www-form-urlencoded
即可。
因为没有深入研究,有不对的地方请指出 :)