Response and Request format

0

We have an EndPoint in Rest for accessing data from a partner, also in Rest. (we use SpringBoot)

Their response is all in Portuguese. In accordance with our customer policy, all of our EndPoint code and interface must be in English.

I have the following scenario:

// Controller
@GetMapping("/getClinicalRecord")
public Dermatologist getFichaClinica(@RequestHeader(X_SECURITY_TOKEN) final String token) {

    Usuario usuario = getUsuario(token);
    final Dermatologist response = getFichaClinicaApiService().getFichaClinica(usuario);

    return response;
}

// Response
public class Dermatologist {

    @JsonProperty("temCancer")
    private Integer hasCancer;

    @JsonProperty("cancer")
    private String cancer;

    @JsonProperty("fezCirurgia")
    private Integer didSurgery;

    @JsonProperty("temTatuagem")
    private Integer hasTatto;
}

In the example above the controller returns the Dermatologist class, however I need the return to be in English. With variable names. However, it is being displayed according to the values reported in JsonProperty.

Is there any annotation that solves this?

I was suggested to create a class to perform the return, and copy the output of our partner in this new object, which would be:

public class Dermatologist {

    private Integer hasCancer;
    private String cancer;
    private Integer didSurgery;
    private Integer hasTatto;
}

Considering this approach, can I do the copy of the two objects using Reflection or something like that? Remember that there is only one example. Return objects would be identical, but have lists and other internal objects.

Thank you.

    
asked by anonymous 06.12.2017 / 15:48

1 answer

0

You can do this in some ways, including the one you mentioned.

Using a new class

The first option is to create a new class (a kind of object adapter ) with the same attributes with the desired names. And then use something similar to a copy constructor to facilitate the class conversion process. Example:

public static class DermatologistAdapter {

    @JsonProperty("hasCancer")
    private Integer hasCancer;

    @JsonProperty("cancer")
    private String cancer;

    @JsonProperty("didSurgery")
    private Integer didSurgery;

    @JsonProperty("hasTatto")
    private Integer hasTatto;

    public DermatologistAdapter(Dermatologist dermatologist) {
        this.hasCancer = dermatologist.hasCancer();
        this.cancer = dermatologist.cancer();
        this.didSurgery = dermatologist.didSurgery();
        this.hasTatto = dermatologist.hasTatto();
    }
}

And in your controller :

GetMapping("/getClinicalRecord")
public DermatologistAdapter getFichaClinica(@RequestHeader(X_SECURITY_TOKEN) final String token) {

    Usuario usuario = getUsuario(token);
    final Dermatologist response = getFichaClinicaApiService().getFichaClinica(usuario);

    return new DermatologistAdapter(response);
}

Using a Custom JsonSerializer

A second option is to create a JsonSerializer for the Dermatologist class, so that Jackson uses it instead of annotations. Example:

public final class DermatologistSerializer extends JsonSerializer<Dermatologist> {

    @Override
    public final void serialize(Dermatologist value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {

        gen.writeStartObject();
        gen.writeNumberField("hasCancer", value.hasCancer());
        gen.writeStringField("cancer", value.cancer());
        gen.writeNumberField("didSurgery", value.didSurgery());
        gen.writeNumberField("hasTatto", value.hasTatto());
        gen.writeEndObject();
    }
}

And then register it this way:

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
    Jackson2ObjectMapperBuilder j2omb = new Jackson2ObjectMapperBuilder();
    SimpleModule module = new SimpleModule();
    module.addSerializer(Dermatologist.class, new DermatologistSerializer());
    j2omb.modules(module);
    return j2omb;
}

Note: In this case the Controller method would be unchanged.

Using Mix-in Annotations

Finally, you could also use Mix-in Annotations . Example:

public abstract class DermatologistMixIn {
    @JsonProperty("") Integer hasCancer;
    @JsonProperty("") String cancer;
    @JsonProperty("") Integer didSurgery;
    @JsonProperty("") Integer hasTatto;
}

And then register it this way:

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
    Jackson2ObjectMapperBuilder j2omb = new Jackson2ObjectMapperBuilder();
    j2omb.mixIn(Dermatologist.class, DermatologistMixIn.class);
    return j2omb;
}

Note: In this case the method of your Controller would also be unchanged.

    
06.12.2017 / 17:54