如何使用angularjs发布项目对象?
我的简单java类:
@Entity
public class ProductionOrder implements Serializable {
@Enumerated(EnumType.STRING)
public Status status;
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "ITEM_ID")
private Item item;
private int plannedQty;
private Date plannedDate;
private String description;
//getters setters enum
我的角度控制器:
'use strict';
App.controller('PoController', ['$scope', 'PoService', function($scope, PoService) {
var vm = this;
vm.order = {id: null, status: null, item:null , plannedQty: null, plannedDate: '', description:''};
vm.orders = [];
vm.fetchAllOrders = function () {
PoService.fetchAllOrders()
.then(
function (d) {
vm.orders = d;
},
function (errResp) {
console.error('Error while fetching production orders!');
}
);
};
vm.createOrder = function (item) {
PoService.createOrder(item)
.then(
vm.fetchAllOrders,
function (errResponse) {
console.error('Error while creating production order');
}
);
};
vm.fetchAllOrders();
vm.submit = function () {
if (vm.order.id === null) {
console.log('Saving new order', vm.order);
vm.createOrder(vm.order);
} else {
console.log('Sth wrong with adding new order');
}
vm.reset();
};
vm.reset = function () {
vm.order = {id: null, status:null, item: null, plannedQty: null, plannedDate: '', description:''};
$scope.myForm.$setPristine();
};
}]);
这是我的角色服务:
'use strict';
App.factory('PoService', ['$http', '$q', function ($http, $q) {
return {
fetchAllOrders: function () {
return $http.get('http://localhost:8080/api/po/all')
.then(
function (response) {
return response.data;
},
function (errResponse) {
console.error('Error while fetching production orders');
return $q.reject(errResponse);
}
);
},
createOrder: function (item) {
return $http.post('http://localhost:8080/api/po/add', item)
.then(
function (res) {
return res.data;
},
function (errRes) {
console.error('Error while adding production order');
return $q.reject(errRes);
}
);
}
};
}]);
在html中我有简单的形式:
<div class="ui main text container", ng-controller="PoController as ctrl">
<form class="ui form" ng-submit="ctrl.submit()" name="myForm">
<h4 class="ui dividing header">Production order information</h4>
<div class="field">
<label> </label>
<div class="two fields">
<div class="field">
<input type="text" ng-model="ctrl.order.id" placeholder="Production order id">
</div>
<div class="field">
<input type="text" ng-model="ctrl.order.item" placeholder="Item id">
</div>
</div>
</div>
<h4 class="ui dividing header">Status:</h4>
<select class="ui dropdown" ng-model="ctrl.order.status">
<option value="CREATED">CREATED</option>
<option value="PLANNED">PLANNED</option>
<option value="DONE">DONE</option>
</select>
<h4 class="ui dividing header">Planning information</h4>
<div class="field">
<label> </label>
<div class="two fields">
<div class="field">
<input type="text" ng-model="ctrl.order.plannedQty" placeholder="Quantity">
</div>
<div class="field">
<input type="text" ng-model="ctrl.order.plannedDate" placeholder="Enter date RRRR-MM-DD">
</div>
</div>
</div>
</br>
<div class="field">
<label>Description</label>
<textarea ng-model="ctrl.order.descritpion"></textarea>
</div>
<div class="asbutton" id="button">
<input type="submit" value="Save" class="ui primary button" ng-disabled="myForm.$invalid">
<button class="ui button" type="button" ng-click="ctrl.reset()" ng-disabled="myForm.$pristine">
Reset
</button>
</div>
</form>
</div>
当我键入例如:1。这意味着我想用1 id的项目创建实现。在终端我看到错误:
2016-06-12 16:06:13.906 WARN 10057 --- [nio-8080-exec-3] .wsmsDefaultHandlerExceptionResolver:无法读取HTTP消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取document:无法从String值(&#39; 2&#39;)实例化类型[simple type,class com.prod.domain.Item]的值。没有单字符串构造函数/工厂方法 在[来源:java.io.PushbackInputStream@2b8fb852; line:1,column:25](通过引用链:com.prod.domain.ProductionOrder [&#34; item&#34;]);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从String值(&#39; 2&#39;)实例化类型[simple type,class com.prod.domain.Item]的值。没有单字符串构造函数/工厂方法 在[来源:java.io.PushbackInputStream@2b8fb852; line:1,column:25](通过引用链:com.prod.domain.ProductionOrder [&#34; item&#34;])
我怎样才能发布与此类似的嵌套json:
item: {id: 6}