1

Java - spring controller - Angularjs form submission

Problem is with "@RequestBody OfferForm data" when i submit form i get error "404 bad request" but when i replace OfferForm bean with String object it works fine and display form data in json format.

any help appreciated.

Following is my angularjs function code

$scope.submitOffer = function() {
                      alert('submitOffer')
                    $http({method: 'POST', url: '/offer/submitOffer', data: $scope.formData}).
                    success(function(data, status, headers, config) {
                        $scope.successMsg = data;
                    }).
                    error(function(data, status, headers, config) {
                        if(status == 400) {
                             $scope.errMessages = data;
                        } else {
                            alert('Unexpected server error.');
                        }

                    });         
                };

Following is my controller code

@RequestMapping(value="offer")
public class OfferController{
    @RequestMapping(value = "/submitOffer", method = RequestMethod.POST)
    @ResponseBody public ResponseEntity<?> postForm(@RequestBody OfferForm data) {

        System.out.println(data);

        return new ResponseEntity<String>("Offer Created", HttpStatus.OK);

    }
}

Following is my java bean

public class OfferForm {

    private String offerType;
    private String offerTitle;


    public String getOfferType() {
        return offerType;
    }
    public void setOfferType(String offerType) {
        this.offerType = offerType;
    }
    public String getOfferTitle() {
        return offerTitle;
    }
    public void setOfferTitle(String offerTitle) {
        this.offerTitle = offerTitle;
    }

}
3
  • Could you show us what is $scope.formData ? It should be an object with at most two properties of type string: offerType and offerTitle. I think you have more properties than expected, thus the bad request Commented Dec 30, 2014 at 10:57
  • yes you are correct. i added all properties and it stopped giving me bad req error. Commented Dec 30, 2014 at 14:29
  • if you write above comment as answer i will accept it as points. Commented Dec 30, 2014 at 14:32

1 Answer 1

1

Your $scope.formData object has more property than expected by the webservice.

You should provide to your webservice an object which has at most two properties of type string: offerType and offerTitle. I think you currently have more properties than expected or you don't have the correct type, thus the bad request exception.

You could do something like that in your javascript, given that the two properties are a string:

$scope.submitOffer = function () {
    $http({
        method: 'POST',
        url: '/offer/submitOffer',
        data: {
            offerType: $scope.formData.offerType,
            offerTitle: $scope.formData.offerTitle
        }
    }).
    success(function (data, status, headers, config) {
        $scope.successMsg = data;
    }).
    error(function (data, status, headers, config) {
        if (status == 400) {
            $scope.errMessages = data;
        } else {
            alert('Unexpected server error.');
        }

    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.