【问题标题】:Why properties of the JSON object come at null at Angular request on the Spring Java side?为什么 JSON 对象的属性在 Spring Java 端的 Angular 请求中为 null?
【发布时间】: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]

【问题讨论】:

    标签: java json spring angular


    【解决方案1】:

    这可能是因为您没有将传入的 JSON 转换为实际对象的函数,或者您没有告诉 Spring 您希望它使用 JSON。

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

    我不太确定你是否需要这个dependancy,但以防万一。

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.8</version>
    </dependency>

    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);
    return this.http.post(this.url, policy, options)
      .map((res) => res.json() as Policy)
      .catch((err) => Observable.throw(console.error(err)));
    }

    【讨论】:

    • 它不应该工作,但我不太确定 Chrome Post 是如何工作的。但是,您可以尝试使用 Postman 之类的方法来拨打电话,看看是否也有效
    • 添加 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE) 没有帮助。在我看来,问题出在 Angular 方面,如何获取发送的内容?
    • @AlexNarinsky 可能是你写 angular 方法的方式,我在上面的帖子中添加了 addPolicyWithObservable 方法的修改版本,看看是否有效。
    【解决方案2】:

    尝试对AttributeDTO 使用@RequestBody 注释。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多