Search code examples
ajaxangularjsspring-mvcspring-annotations

Sending data from AngularJS factory to a Spring Controller


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!!


Solution

  • 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'
        }