响应返回
Spring Boot 请求响应
重定向与转发
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void method(HttpServletResponse httpServletResponse) {
httpServletResponse.setHeader("Location", projectUrl);
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
return new ModelAndView("redirect:" + projectUrl);
}
Jsonp
对于 Jsonp 风格的请求,Spring 在返回时需要加以修改已支持前端的跨域调用,在后端处理方式如下:
protected String responseHandler(JSONObject rtn,
HttpServletRequest request) {
if(request.getParameter("callback") != null){
//将数据填充到callback,并回调
StringBuffer buf = new StringBuffer();
buf.append(request.getParameter("callback"));
buf.append("(");
buf.append(rtn.toJSONString());
buf.append(");");
return buf.toString();
}
// TODO Auto-generated method stub
return rtn.toJSONString();
}
前端请求方式:
jsonp: function (path, params, callback) {
//构造出URL
var url = 'http://localhost:8080/' + path + '?requestData=' + JSON.stringify(params) + '&callback=JSON_CALLBACK';
console.log(url);
//发起网络请求
$http({
method: 'JSONP',
url: url
}).success(function (data, status, headers, config) {
console.log(data);
}).error(errorCallBack);
}