我有一个弹簧控制器,它应该从angularjs工厂接收sessionAttribute中的数据。
我的Spring Controller是:
@Controller
@SessionAttributes("dataObject")
public class ScreenDesignerController extends BaseController {
/**
* Injected screen designer service class.
*/
@Autowired
private ScreenDesigner screendiService;
@RequestMapping(value = "FormBuilder", method = RequestMethod.POST)
public final String knowDetails(
@ModelAttribute("globalUser") User globalUser,
@RequestParam(value = "dataObject") String myJsonStr,
BindingResult result, RedirectAttributes redirectAttributes,
final Model model
) throws Exception {
try {
logger.info("this is json array: " + myJsonStr);
screendiService.addData(myJsonStr);
} catch (Exception e) {
logger.info("inside customiseForm POST catch");
}
return "ScreenDesigner/FormBuilder";
}
Angular工厂:
indApp.factory('sendJsonDataService', function ($http, $rootScope, superCache) {
var sendjsondataservice = {
sendJsonData: function () {
//dataObject = JSON.stringify(superCache.get('super-cache'));
alert(JSON.stringify(dataObject));
res = $http.post('FormBuilder', dataObject);
res.success(function(data, status, headers, config) {
alert("Your Screen has been saved successfully into the database!");
});
res.error(function(data, status, headers, config) {
alert( "failure message: " + JSON.stringify({data: data}));
});
}
};
return sendjsondataservice;
});
每当我通过angularjs控制器调用工厂来接收" dataObject"时,它会说'"错误的请求400",尽管" dataObject"有有效的json数据。 我希望我的弹簧控制器能够接收这些数据。 请帮助,卡住两天吧:( 在此先感谢!!
答案 0 :(得分:1)
如果您要将JSON作为POST有效内容发送,则应使用@RequestBody
而不是@RequestParam
。
答案 1 :(得分:0)
你知道我修改了你的控制器:
@Controller
@SessionAttributes("dataObject")
public class ScreenDesignerController extends BaseController {
/**
* Injected screen designer service class.
*/
@Autowired
private ScreenDesigner screendiService;
@RequestMapping(value = "FormBuilder", method = RequestMethod.POST)
public final String knowDetails(@RequestBody String myJsonStr,@ModelAttribute("globalUser") User globalUser,
BindingResult result, RedirectAttributes redirectAttributes,
final Model model
) throws Exception {
try {
logger.info("this is json array: " + myJsonStr);
screendiService.addData(myJsonStr);
} catch (Exception e) {
logger.info("inside customiseForm POST catch");
}
return "ScreenDesigner/FormBuilder";
}
另一个确保从AngularJS工厂发送json数据。例如:
headers: {
'Content-type': 'application/json'
}