AngularJs $http Post请求用于JPA的一对一映射,返回字符串而不是JSON数据。

huangapple 未分类评论51阅读模式
英文:

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>&nbsp;&nbsp;&nbsp;创建
</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

	&lt;div layout=&quot;row&quot;&gt;
		&lt;md-input-container class=&quot;md-block&quot; flex=&quot;100&quot;&gt;
        &lt;label&gt;Select Country&lt;/label&gt;
			&lt;md-select name=&quot;type&quot; ng-model=&quot;newDatum.country&quot; &gt;
				&lt;md-option ng-repeat=&quot;c in country&quot; value=&quot;{{c}}&quot;&gt;{{c.name}}&lt;/md-option&gt;
			&lt;/md-select&gt;
      &lt;/md-input-container&gt;
	&lt;/div&gt;
	&lt;md-button style=&quot;width:100%&quot; type=&quot;submit&quot; ng-click=&quot;createDatum(newDatum)&quot; class=&quot;md-raised md-primary&quot;&gt;
		&lt;md-icon class=&quot;md-size-3x&quot;&gt;arrow_forward&lt;/md-icon&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Create
	&lt;/md-button&gt;

here is javascript

    $scope.createDatum= function(datum){
			$http({
			method : &quot;POST&quot;,
			url : &quot;api/address&quot;,
			data : angular.toJson(datum),
			headers : {
				&quot;Content-Type&quot; : &quot;application/json&quot;
			}
		}).then(function(response){
			$mdDialog.show($mdDialog.alert()
				.title(&#39;Create \&#39;&#39;+datum.contactPersonName+&#39;\&#39; Succesfull!&#39;)
				.textContent(&#39;Here you have successfully create this \&#39;&#39;+datum.contactPersonName.toLowerCase()+&#39;\&#39; configuration!&#39;)
				.ok(&#39;Ok&#39;));
			$scocontactPersonNamehowProgressBar = false;
			$scope.getAllData();
		}, function(reason){
			$mdDialog.show($mdDialog.alert()
				.title(&#39;Create \&#39;&#39;+datum.contactPersonName+&#39;\&#39; Error!&#39;)
				.textContent(&#39;Here you have got error to create this \&#39;&#39;+datum.contactPersonName.toLowerCase()+&#39;\&#39; configuration! &#39;+reason.data)
				.ok(&#39;Ok&#39;));
			$scope.showProgressBar = false;
			$scope.error_message = reason.data;
		});}

Java entity class mapping

@OneToOne(cascade = CascadeType.MERGE)
@JoinTable(name = &quot;address_country&quot;, joinColumns = @JoinColumn(name = &quot;address_id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;country_id&quot;))
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 (&#39;{&quot;id&quot;:4,&quot;name&quot;:&quot;AMERICAN SAMOA&quot;,&quot;niceName&quot;:&quot;American Samoa&quot;,&quot;countryCode&quot;:&quot;AS&quot;,&quot;phoneCode&quot;:&quot;1684&quot;,&quot;iso3&quot;:&quot;ASM&quot;,&quot;numCode&quot;:&quot;16&quot;}&#39;); 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 (&#39;{&quot;id&quot;:4,&quot;name&quot;:&quot;AMERICAN SAMOA&quot;,&quot;niceName&quot;:&quot;American Samoa&quot;,&quot;countryCode&quot;:&quot;AS&quot;,&quot;phoneCode&quot;:&quot;1684&quot;,&quot;iso3&quot;:&quot;ASM&quot;,&quot;numCode&quot;:&quot;16&quot;}&#39;)
at [Source: (PushbackInputStream); line: 1, column: 227] (through reference chain: com.zero.zerodefinition.address.Address[&quot;country&quot;])]

Browser error

{&quot;timestamp&quot;:&quot;2020-05-02T09:39:53.692+0000&quot;,&quot;status&quot;:400,&quot;error&quot;:&quot;Bad Request&quot;,&quot;message&quot;:&quot;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 (&#39;{\&quot;id\&quot;:4,\&quot;name\&quot;:\&quot;AMERICAN SAMOA\&quot;,\&quot;niceName\&quot;:\&quot;American Samoa\&quot;,\&quot;countryCode\&quot;:\&quot;AS\&quot;,\&quot;phoneCode\&quot;:\&quot;1684\&quot;,\&quot;iso3\&quot;:\&quot;ASM\&quot;,\&quot;numCode\&quot;:\&quot;16\&quot;}&#39;); 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 (&#39;{\&quot;id\&quot;:4,\&quot;name\&quot;:\&quot;AMERICAN SAMOA\&quot;,\&quot;niceName\&quot;:\&quot;American Samoa\&quot;,\&quot;countryCode\&quot;:\&quot;AS\&quot;,\&quot;phoneCode\&quot;:\&quot;1684\&quot;,\&quot;iso3\&quot;:\&quot;ASM\&quot;,\&quot;numCode\&quot;:\&quot;16\&quot;}&#39;)\n at [Source: (PushbackInputStream); line: 1, column: 227] (through reference chain: com.zero.zerodefinition.address.Address[\&quot;country\&quot;])&quot;,&quot;path&quot;:&quot;/zerodefinition/api/address&quot;}

how to solve this.! Is there any problem of hibernate mapping! Why this is not converted to json insted string.

huangapple
  • 本文由 发表于 2020年5月2日 18:25:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61557804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定