0

I have a spring controller which should recieve data in the sessionAttribute from an angularjs factory.

My Spring Controller is :

@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 factory:

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; 
});

Whenever I am invoking the factory via angularjs controller to recieve "dataObject", it says "bad request 400", Though the "dataObject" has valid json data in it. I want my spring controller to receive this data. Please help, stuck for two days now :( Thanks in advance!!

GeekStyle
  • 13
  • 1
  • 9

2 Answers2

1

If you're sending JSON as a POST payload, you should be using @RequestBody instead of @RequestParam.

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
0

Thy this i modified your 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(@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";
    }

Another this be sure to send json data from AngularJS factory. For instance :

headers: {
        'Content-type': 'application/json'
    }
Pracede
  • 4,226
  • 16
  • 65
  • 110
  • Thanks a lot!! it worked finally :-) Just a small question what is the difference between requestparam and requestbody. I was using requestparam with YUI & ajax to send data earlier and it worked fine but when I started using angularjs instead it didn't work and have to use requestbody now. What's the difference? – GeekStyle Dec 05 '14 at 10:29
  • RequestBody : means map JSON data to Java Object With Spring MVC. RequestParam : map the param ( in your example dataObject) to the Controller param. I think in YUI & ajax you were passing data as parameter and not as content request – Pracede Dec 05 '14 at 12:27
  • Thanks for bringing it to my notice. I was sending the same JSON data before but was passing it as a parameter which is now treated as JSON data now. – GeekStyle Dec 05 '14 at 12:45