如何使用Angularjs将Form类对象发布回Spring RestController

时间:2016-02-21 04:33:42

标签: angularjs json post spring-restcontroller

我被阻止尝试使用AngularJS回发给Rest Controller。

检索MyCommand类对象的方法是什么?我是否必须以某种方式将其转换回对象?我看到它成功地完成了我在示例中尝试的方式,但它对我不起作用...... :(

@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());

给出了这个错误:

2016-02-20 22:35:07.214 WARN 6796 --- [omcat-http - 108] .wsmsDefaultHandlerExceptionResolver:无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法从START_ARRAY标记中反序列化java.lang.String的实例  在[来源:java.io.PushbackInputStream@78fa95e5; line:1,column:2](通过引用链:com.mycompany.MyCommand [“cmdScript”]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY令牌中反序列化java.lang.String的实例  在[来源:java.io.PushbackInputStream@78fa95e5; line:1,column:2](通过引用链:com.mycompany.MyCommand [“cmdScript”])

我的控制台登录页面显示...

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

无效的COMMAND条目\ r \ n无效的命令条目&gt;&gt;&gt; IS OK !!那是我作为测试输入的内容。并期望在RestController中看到(哈哈)

Soooo是什么赋予.. ???

0 个答案:

没有答案