英文:
AngularJs $http Post Requst for JPA OneToOne mapping return String instead JSON data
问题
以下是翻译好的内容:
这里是AngularJs材料中的HTML:
<div layout="row">
<md-input-container class="md-block" flex="100">
<label>Select Country</label>
<md-select name="type" ng-model="newDatum.country">
<md-option ng-repeat="c in country" value="{{c}}">{{c.name}}</md-option>
</md-select>
</md-input-container>
</div>
<md-button style="width:100%" type="submit" ng-click="createDatum(newDatum)" class="md-raised md-primary">
<md-icon class="md-size-3x">arrow_forward</md-icon> 创建
</md-button>
这是Javascript:
$scope.createDatum = function(datum){
$http({
method : "POST",
url : "api/address",
data : angular.toJson(datum),
headers : {
"Content-Type" : "application/json"
}
}).then(function(response){
$mdDialog.show($mdDialog.alert()
.title('创建 \'' + datum.contactPersonName + '\' 成功!')
.textContent('您已成功创建此 \'' + datum.contactPersonName.toLowerCase() + '\' 配置!')
.ok('好的'));
$scope.showProgressBar = false;
$scope.getAllData();
}, function(reason){
$mdDialog.show($mdDialog.alert()
.title('创建 \'' + datum.contactPersonName + '\' 出错!')
.textContent('在创建此 \'' + datum.contactPersonName.toLowerCase() + '\' 配置时出错! ' + reason.data)
.ok('好的'));
$scope.showProgressBar = false;
$scope.error_message = reason.data;
});
}
Java实体类映射:
@OneToOne(cascade = CascadeType.MERGE)
@JoinTable(name = "address_country", joinColumns = @JoinColumn(name = "address_id"), inverseJoinColumns = @JoinColumn(name = "country_id"))
private Country country;
Java编译错误:
2020-05-02 15:39:53.691 WARN 5083 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : 已解析 [org.springframework.http.converter.HttpMessageNotReadableException: JSON解析错误:无法构造`com.zero.zerodefinition.country.Country`的实例(尽管至少存在一个创建者):没有用于从字符串值反序列化的String-argument构造函数/工厂方法('{"id":4,"name":"AMERICAN SAMOA","niceName":"American Samoa","countryCode":"AS","phoneCode":"1684","iso3":"ASM","numCode":"16"}');嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法构造`com.zero.zerodefinition.country.Country`的实例(尽管至少存在一个创建者):没有用于从字符串值反序列化的String-argument构造函数/工厂方法('{"id":4,"name":"AMERICAN SAMOA","niceName":"American Samoa","countryCode":"AS","phoneCode":"1684","iso3":"ASM","numCode":"16"}')
位于[源:(PushbackInputStream);行:1,列:227](通过引用链:com.zero.zerodefinition.address.Address["country"])]
浏览器错误:
{"timestamp":"2020-05-02T09:39:53.692+0000","status":400,"error":"错误的请求","message":"JSON解析错误:无法构造`com.zero.zerodefinition.country.Country`的实例(尽管至少存在一个创建者):没有用于从字符串值反序列化的String-argument构造函数/工厂方法('{\"id\":4,\"name\":\"AMERICAN SAMOA\",\"niceName\":\"American Samoa\",\"countryCode\":\"AS\",\"phoneCode\":\"1684\",\"iso3\":\"ASM\",\"numCode\":\"16\"}');嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法构造`com.zero.zerodefinition.country.Country`的实例(尽管至少存在一个创建者):没有用于从字符串值反序列化的String-argument构造函数/工厂方法('{\"id\":4,\"name\":\"AMERICAN SAMOA\",\"niceName\":\"American Samoa\",\"countryCode\":\"AS\",\"phoneCode\":\"1684\",\"iso3\":\"ASM\",\"numCode\":\"16\"}')\n位于[源:(PushbackInputStream);行:1,列:227](通过引用链:com.zero.zerodefinition.address.Address[\"country\"])","path":"/zerodefinition/api/address"}
如何解决这个问题!是否存在Hibernate映射的问题!为什么这不被转换为JSON而是字符串。
英文:
here is html in AngularJs materials
<div layout="row">
<md-input-container class="md-block" flex="100">
<label>Select Country</label>
<md-select name="type" ng-model="newDatum.country" >
<md-option ng-repeat="c in country" value="{{c}}">{{c.name}}</md-option>
</md-select>
</md-input-container>
</div>
<md-button style="width:100%" type="submit" ng-click="createDatum(newDatum)" class="md-raised md-primary">
<md-icon class="md-size-3x">arrow_forward</md-icon>&nbsp;&nbsp;&nbsp;Create
</md-button>
here is javascript
$scope.createDatum= function(datum){
$http({
method : "POST",
url : "api/address",
data : angular.toJson(datum),
headers : {
"Content-Type" : "application/json"
}
}).then(function(response){
$mdDialog.show($mdDialog.alert()
.title('Create \''+datum.contactPersonName+'\' Succesfull!')
.textContent('Here you have successfully create this \''+datum.contactPersonName.toLowerCase()+'\' configuration!')
.ok('Ok'));
$scocontactPersonNamehowProgressBar = false;
$scope.getAllData();
}, function(reason){
$mdDialog.show($mdDialog.alert()
.title('Create \''+datum.contactPersonName+'\' Error!')
.textContent('Here you have got error to create this \''+datum.contactPersonName.toLowerCase()+'\' configuration! '+reason.data)
.ok('Ok'));
$scope.showProgressBar = false;
$scope.error_message = reason.data;
});}
Java entity class mapping
@OneToOne(cascade = CascadeType.MERGE)
@JoinTable(name = "address_country", joinColumns = @JoinColumn(name = "address_id"), inverseJoinColumns = @JoinColumn(name = "country_id"))
private Country country;
Java Compiler error
2020-05-02 15:39:53.691 WARN 5083 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.zero.zerodefinition.country.Country` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":4,"name":"AMERICAN SAMOA","niceName":"American Samoa","countryCode":"AS","phoneCode":"1684","iso3":"ASM","numCode":"16"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.zero.zerodefinition.country.Country` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":4,"name":"AMERICAN SAMOA","niceName":"American Samoa","countryCode":"AS","phoneCode":"1684","iso3":"ASM","numCode":"16"}')
at [Source: (PushbackInputStream); line: 1, column: 227] (through reference chain: com.zero.zerodefinition.address.Address["country"])]
Browser error
{"timestamp":"2020-05-02T09:39:53.692+0000","status":400,"error":"Bad Request","message":"JSON parse error: Cannot construct instance of `com.zero.zerodefinition.country.Country` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{\"id\":4,\"name\":\"AMERICAN SAMOA\",\"niceName\":\"American Samoa\",\"countryCode\":\"AS\",\"phoneCode\":\"1684\",\"iso3\":\"ASM\",\"numCode\":\"16\"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.zero.zerodefinition.country.Country` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{\"id\":4,\"name\":\"AMERICAN SAMOA\",\"niceName\":\"American Samoa\",\"countryCode\":\"AS\",\"phoneCode\":\"1684\",\"iso3\":\"ASM\",\"numCode\":\"16\"}')\n at [Source: (PushbackInputStream); line: 1, column: 227] (through reference chain: com.zero.zerodefinition.address.Address[\"country\"])","path":"/zerodefinition/api/address"}
how to solve this.! Is there any problem of hibernate mapping! Why this is not converted to json insted string.
专注分享java语言的经验与见解,让所有开发者获益!
评论