Response and Request format

Asked

Viewed 52 times

0

We have an Endpoint in Rest for access to a partner’s data, also in Rest. (We use Springboot)

Their answer is in Portuguese. According to our client’s policy, all 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 above example the controller returns the Dermatologist class, but I need the return to be in English. With the variable names. But 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, is there a way I can copy the two objects using Reflection or something like that? Remember that there is only one example. The return objects would be identical, but have lists and other internal objects.

Grateful.

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 conversion process of the classes. 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 his 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 class Dermatologist, for Jackson to use 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 that way:

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

Obs: in this case your method Controller 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 that way:

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

Note: in this case your Controller’s method would also be unchanged.

  • Fantastic. Thank you Felipão =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.