0

I'm blocked trying to post back to Rest Controller using AngularJS.

What's the way to retrieve the MyCommand Class Object ? Do I have to convert it in some way back to an object ? I see it done successfully the way I'm trying in examples but it doesn't work for me... :(

@Entity
public final class MyCommand implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;


private String cmdScript;



// --------- getters and setters --------------

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}



public String getCmdScript() {
    return cmdScript;
}

public void setCmdScript(String cmdScript) {
    this.cmdScript = cmdScript;
}




}

var app = angular.module('myApp', [ 'restangular' ]);

var myData;


app.controller('commandController', function($scope, $http, $location,
    Restangular) {

$scope.success ={};
$scope.myData = {};

$scope.saveScript = function() {


var myData = $scope.myData;

    console.log(">>>>>>>>>>>>>>>>tl1Data angular.toJson: "
            + angular.toJson(tl1Data, 2));

    $http.post('/MyGen/rest/saveScript/', myData )
    .success(function(myData, status, headers, config) {
        $scope.success = ">>>>>>>>>>>>>>>>Save Script Success: "+$scope.success;
        console.log($scope.success);
    })
    .error(function(myData, status, headers, config) {
        $scope.success = ( "failure message: " + JSON.stringify({data: scrData}));
    });


}   


<form id = "commandForm" name="commandForm" th:object="${mycommand}" ng-controller="commandController" ng-submit="saveScript()">
    <div class="row">
        <div class="col-sm-12" style="background-color:#cccccc;"><textarea class="form-control" rows="6" id="cmd_script" ng-model="myData.cmdScript"></textarea></div>

    </div>



    @RequestMapping(value = "/saveScript/", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public void saveScript(@RequestBody MyCommand myData) {

        log.debug(">>>>>>>>>>>>>>>>>>>REST_CTR SAVE SCRIPT : "+myData.getCmdScript());

gives this error:

2016-02-20 22:35:07.214 WARN 6796 --- [omcat-http--108] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token at [Source: java.io.PushbackInputStream@78fa95e5; line: 1, column: 2] (through reference chain: com.mycompany.MyCommand["cmdScript"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token at [Source: java.io.PushbackInputStream@78fa95e5; line: 1, column: 2] (through reference chain: com.mycompany.MyCommand["cmdScript"])

My Console Log on the page shows...

tl1Data angular.toJson: {
"cmdScript": [
"Invalid COMMAND entry\r\nInvalid COMMAND entry"
 ],
 "jobname": "qwert"
 }"

Invalid COMMAND entry\r\nInvalid COMMAND entry >>>IS OK !! THAT'S WHAT I ENTERED AS the TEST. And expected to see in the RestController (haha)

Soooo what gives..???

JohnnyO
  • 527
  • 9
  • 21
  • `myData` is not the same as `$scope.myData` in controller. Inspect the actual request in browser dev tools network will likely see the request body is empty – charlietfl Feb 21 '16 at 04:40
  • The code is such a mess. Could you clear this up. First of all your RequestMapping is a void and returns nothing, what are you trying to do in the success handler? What is this th:object on form when doing restful requests. Then the payload variable `myData` doesn't even exist and is passed as undefined. Use proper variable. – Vaelyr Feb 21 '16 at 06:37

0 Answers0