为什么在Spring Java端的Angular请求中JSON对象的属性为null? [英] Why properties of the JSON object come at null at Angular request on the Spring Java side?

查看:156
本文介绍了为什么在Spring Java端的Angular请求中JSON对象的属性为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Angular应用程序和Chrome海报发送相同的发布请求.

I am sending the same Post request from the Angular Application and the Chrome poster.

在海报中,我指定了以下内容: 网址: http://localhost:8080/rentapp/policy

From the Poster I specify the following: URL: http://localhost:8080/rentapp/policy

标题: 内容类型application/json

Header: content-type application/json

内容:身体 { "title":禁止吸烟", "description":禁止吸烟" }

Content: body { "title": "NoSmoking", "description":"NoSmoking" }

在Spring Controller中,我有以下代码:

In the Spring Controller I have the following code:

@PostMapping
// @Secured("ROLE_ADMIN")
public ResponseEntity<?> create(@Valid AttributeDTO policy, BindingResult bindingResult) {
    logger.debug("CreatePolicy=" + policy);

AttributeDTO定义为:

AttributeDTO is defined as:

@Data
public class AttributeDTO {
private String title;
private String description;
}

我看到填充了发帖人请求AttributeDTO. 在Angular中,我有以下方法

I see that after the Poster request AttributeDTO is populated. In Angular I Have the following method

public addPolicyWithObservable(policy: Policy): Observable<Policy> {
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});
console.log("addPolicyWithObservable policy=" + policy + " url=" + this.url + " http=" + this.http);
const ret: Observable<Policy> = this.http.post(this.url, policy, options)
  .map(this.extractData)
  .catch(this.handleErrorObservable);
console.log ("addPolicyWithObservable ret=" + ret);
return ret;
}

private extractData(res: Response) {
const body = res.json();
return body.data || {};
}

private handleErrorObservable(error: Response | any) {
console.error(error.message || error);
return Observable.throw(error.message || error);
}

在策略"模型中:

export class Policy {
public id: number;
public title: string;
public description: string;

toString(): string {
return this.title + ': ' + this.description;
}
}

我看到在此请求之后,Java方面AttributeDTO的属性(title和description)为空.尽管事实是请求到达Java端,而在Angular端,发送之前的日志记录是正确的,即标题和描述已正确填充:

I see that after this request the properties of AttributeDTO (title and description ) are null on the Java side. That is despite the fact that the request comes to the Java side and on the Angular side the logging before sending is correct, i.e the title and description are properly populated:

addPolicyWithObservable policy =禁止吸烟:禁止吸烟 url =/api/policy http = [对象对象]

addPolicyWithObservable policy=NoSmoking: NoSmoking url=/api/policy http=[object Object]

createpolicy.service.ts:20 addPolicyWithObservable ret = [对象对象]

createpolicy.service.ts:20 addPolicyWithObservable ret=[object Object]

推荐答案

尝试对AttributeDTO使用@RequestBody批注.

public ResponseEntity<?> create(
    @RequestBody AttributeDTO policy,
    BindingResult bindingResult)
{
    logger.debug("CreatePolicy=" + policy);
    ...

这篇关于为什么在Spring Java端的Angular请求中JSON对象的属性为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆