2016-06-28 97 views
0

解析生成JSON從RC-1升級到RC-3的JSON.stringfy()方法後,在各值的開始和結束時返回與\值:爪哇不能通過JSON.stringfy

{ 
    \"perfil\":\"CLIENTE\", ... 
} 

我應該如何修復?

代碼片段:

post(url, data) { 
    console.log(JSON.stringify(data)); 
    return Observable.create(observer => 
     this.http.post(this.restConfig.baseUrl + url, JSON.stringify(data), { 
      headers: this.getDefaultHeaders() 
     }).subscribe(
      data => this.next(observer, data) 
      , err => { 
       console.log(err); 
       if (err.status === 401) { 
        this.redirectAuth(); 
       } 
       observer.error(err); 
      } 
     ) 
    ); 
} 

我的Java RESTful服務無法解析輸出:

Unexpected token (VALUE_STRING), expected FIELD_NAME: missing property 'perfil' that is to contain type id (for class br.com.inbit.medipop.model.entities.impl.Cliente) at [Source: [email protected]; line: 1, column: 1] 

類Cliente:

@Table 
@Entity 
@DiscriminatorValue("CLIENTE") 
public class Cliente extends Usuario { 

} 

類Usuario:

@Table 
@Entity 
@Inheritance(strategy = InheritanceType.JOINED) 
@DiscriminatorColumn(name = "perfil", discriminatorType = DiscriminatorType.STRING) 
@JsonIgnoreProperties(ignoreUnknown = true) 
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "perfil") 
@JsonSubTypes({ 
    @JsonSubTypes.Type(value = Administrador.class, name = "ADMIN"), 
    @JsonSubTypes.Type(value = Colaborador.class, name = "COLABORADOR"), 
    @JsonSubTypes.Type(value = Parceiro.class, name = "PARCEIRO"), 
    @JsonSubTypes.Type(value = Cliente.class, name = "CLIENTE"), 
    @JsonSubTypes.Type(value = Dependente.class, name = "DEPENDENTE") 
}) 
public abstract class Usuario { 

    @NotNull 
    @Enumerated(EnumType.STRING) 
    @Column(insertable = false, updatable = false) 
    protected PerfilUsuario perfil; 

    ... 
} 

數據:

{"perfil":"CLIENTE","pessoa":{"tipo":"FISICA","sexo":"MASCULINO","nome":"Marcos Kichel","cpf":"911.111.064-36","rg":"1234"},"dependentes":[],"email":"[email protected]"} 
+0

請發佈您的代碼片段 – Apostolos

+0

done @Apostolos :) –

+0

這只是逃避報價。它實際上是造成問題嗎? – jonrsharpe

回答

1

看來你是字符串化的東西,已經被字符串化。拿出JSON.stringify(),你應該很好去。

0

這裏居然沒有逃脫引號引起的問題,但perfil字段的數據類型錯誤。在你的java代碼中,它應該是一個對象,而在json中,你傳遞的是字符串值"CLIENTE"

因此,無論確保您在「PERFIL」字段發送對象或PerfilUsuario改變perfil字段的類型爲String

+0

實際上,它是一個'enum',你可以在字段上方的'@ Enumerated'註釋中注意到。枚舉由'Jackson Body Writer'解析爲'String',這不是問題,但是感謝您的幫助 –