简体   繁体   English

角度模型数据到Spring Pojo类的映射不起作用

[英]mapping of angular model data to spring pojo class not working

I'm trying to map angular model class to my spring model class. 我正在尝试将角度模型类映射到我的弹簧模型类。 But, in spring model class it displays null for all the entities. 但是,在spring模型类中,所有实体均显示为null。 Below is my code which i've used to do the same, but it fails to map angular model data to spring pojo class. 下面是我曾经做过的代码,但是无法将角度模型数据映射到spring pojo类。

HeaderMappingDetails.java HeaderMappingDetails.java

public class HeaderMappingDetails{
    String Impressions;
    String Clicks;
    String Revenue;
    //getters and setters
}

headermappingdetails.ts headermappingdetails.ts

export class HeaderMappingDetails{
    public Impressions:string;
    public Clicks:string;
    public Revenue:string
}

add-client.component.ts 附加client.component.ts

saveHeaders(){
   this.clientService.saveHeaders(this.headerMap).subscribe(
       res=>{
           console.log(res);       
       },error=>{
           console.log(error);
       }
   );     
}

client.service.ts client.service.ts

saveHeaders(mapHeaders:HeaderMappingDetails){

    let url = this.serverPath+'/user/saveHeaders';
    let tokenHeader = new Headers({
    'Content-Type' : 'application/json',
    'x-auth-token' : localStorage.getItem('xAuthToken')
 });
    console.log(JSON.stringify(mapHeaders));
    return this.http.post(
        url,JSON.stringify(mapHeaders),
        {headers :   tokenHeader}
    );

}

addClient.html addClient.html

<form [hidden]="!showMapper" (ngSubmit)="saveHeaders()">
    <mat-form-field class="full-width">
        <mat-select placeholder="Map Impression As" [(ngModel)]="headerMap.Impression" id="Impression" name="Impression">
                <mat-option *ngFor="let option of mapper" [value]="option">{{option}}</mat-option>
            </mat-select>
    </mat-form-field>
    <mat-form-field class="full-width">
        <mat-select placeholder="Map Clicks As" [(ngModel)]="headerMap.Clicks" id="Clicks" name="Clicks">
            <mat-option *ngFor="let option of mapper"  [value]="option">{{option}}</mat-option>
        </mat-select>
    </mat-form-field>
    <mat-form-field class="full-width">
        <mat-select placeholder="Map Revenue As" [(ngModel)]="headerMap.Revenue" id="Revenue" name="Revenue">
            <mat-option *ngFor="let option of mapper"  [value]="option">{{option}}</mat-option>
        </mat-select>
    </mat-form-field>
    <button mat-raised-button type="submit" class="mat-primary">Save Mapped Headers</button>
</form>

HeaderMappingController.java HeaderMappingController.java

@PostMapping(value = "/saveHeaders")
public HeaderMappingDetails saveHeaders(@RequestBody HeaderMappingDetails headerMappingDetails) throws Exception {
    Integer clientId = Integer.parseInt(jedis.get("emailClientId"));
    boolean isSuccess = headerMappingService.saveHeaders(headerMappingDetails, clientId);
    return headerMappingDetails;
}

This is the response printed by angular in my console 这是我的控制台中用angular打印的响应

{"Impression":"Campaign","Clicks":"Budget","Revenue":"Budget"} { “印象”: “运动”, “点击次数”: “预算”, “收入”: “预算”}

first off, you should name your variables according to java language standards. 首先,您应该根据Java语言标准命名变量。 In your case, make them start with a small letter (only classes should have capital first latters). 在您的情况下,使它们以小写字母开头(仅类应具有大写优先的后者)。 Also, make all variables private: 另外,将所有变量设为私有:

public class HeaderMappingDetails{
    private String impressions;
    private String clicks;
    private String revenue;
    // getters and setters
}

Your angular code looks ok to me, i use it a bit differently: 您的角度代码对我来说还可以,我使用它的方式有所不同:

saveHeaders(mapHeaders:HeaderMappingDetails){
    let url = this.serverPath+'/user/saveHeaders';
    const headers: HttpHeaders = new HttpHeaders();
    headers.set("content-type", "application/json");
    headers.set("x-auth-token", localStorage.getItem('xAuthToken'));
    const options = {
      headers: headers
    };
    console.dir(mapHeaders);
    return this.http.post(url, mapHeaders, options);
}

You don't need to stringify the object. 您无需对对象进行字符串化。

In your case, i also would recommend using an interface instead of an object for your typescript object: 在您的情况下,我还建议您使用接口代替Typescript对象的对象:

export interface IHeaderMappingDetails {
    impressions: string;
    clicks: string;
    revenue: string;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM