【发布时间】:2018-03-05 13:26:42
【问题描述】:
我正在从 Angular 应用程序和 Chrome 海报发送相同的 Post 请求。
从海报中我指定以下内容: 网址:http://localhost:8080/rentapp/policy
标题: 内容类型应用程序/json
内容:正文 { "title": "禁止吸烟", “描述”:“禁止吸烟” }
在 Spring 控制器中,我有以下代码:
@PostMapping
// @Secured("ROLE_ADMIN")
public ResponseEntity<?> create(@Valid AttributeDTO policy, BindingResult bindingResult) {
logger.debug("CreatePolicy=" + policy);
AttributeDTO 定义为:
@Data
public class AttributeDTO {
private String title;
private String description;
}
我看到在海报请求 AttributeDTO 被填充之后。 在Angular中我有以下方法
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;
}
}
我看到在这个请求之后 AttributeDTO 的属性(标题和描述)在 Java 端是空的。尽管请求来自 Java 端,而在 Angular 端,发送前的日志记录是正确的,即标题和描述已正确填充:
addPolicyWithObservable policy=NoSmoking: NoSmoking url=/api/policy http=[object Object]
createpolicy.service.ts:20 addPolicyWithObservable ret=[object Object]
【问题讨论】: