0

After upgrading from rc-1 to rc-3 the JSON.stringfy() method is returning values with \ at start and end of each value:

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

How should I fix that?

code snippet:

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);
            }
        )
    );
}

My Java RESTful services can't parse the output:

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: java.io.ByteArrayInputStream@79f844cf; line: 1, column: 1]

class Cliente:

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

}

class 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;

    ...
}

data before stringfy:

{"perfil":"CLIENTE","pessoa":{"tipo":"FISICA","sexo":"MASCULINO","nome":"Marcos Kichel","cpf":"911.111.064-36","rg":"1234"},"dependentes":[],"email":"[email protected]"}
9
  • please post a snippet of your code Commented Jun 28, 2016 at 18:35
  • done @Apostolos :) Commented Jun 28, 2016 at 18:38
  • It's just escaping the quotes. Is it actually causing problems? Commented Jun 28, 2016 at 18:39
  • Yes, my Java RESTful services can't parse the JSON output. Commented Jun 28, 2016 at 18:41
  • cannot reproduce it actually Commented Jun 28, 2016 at 18:49

2 Answers 2

1

It seems that you are stringifying something that was already stringified. Take out the JSON.stringify() and you should be good to go.

Sign up to request clarification or add additional context in comments.

Comments

0

The issue here actually not caused by escaped quotes, but the wrong data type of perfil field. In your java code it's expected to be an object, while in json you pass the String value "CLIENTE".

So either make sure you send an object within "perfil" field or change the type of perfil field from PerfilUsuario to String

1 Comment

Actually, it is an enum, as you can notice on the @Enumerated annotation above the field. Enums are parsed as String by the Jackson Body Writer, it is not the problem, but thanks for your help anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.