Define which method is not serialized

1

I have a rest service that returns the object of a class that contains the following structure as a response:

public class LocalizacaoLinhaWrapper implements
        Serializable {

    ...

    private List<LocalizacaoLinha> linhasFavoritas;

    private List<LocalizacaoLinha> linhasNaoFavoritas;

    public boolean isEmpty() {
        return linhasNaoFavoritas.isEmpty() && linhasNaoFavoritas.isEmpty();
    }

    //Getters e setters
}

I use the following command to return the response:

LocalizacaoLinhaWrapperDTO wrapper = service.minhaConsulta()//realizo a consulta    
Response.status(Status.OK).entity(localizacaoWrapper).build()

When I make the request to this service everything is serialized correctly, however I have an attacker called empty in my json , which was generated due to the existence of the LocalizacaoLinhaWrapper.isEmpty() method.

Is it possible to set this method to be neglected at the time of serialization? If yes, how to do it?

    
asked by anonymous 17.09.2015 / 00:12

1 answer

1

RestEasy is the provider JAX-RS in JBoss AS. In version 7.1.1.Final of JBoss AS the version of RestEasy is 2.3.2.Final (can be seen in modules\org\jboss\resteasy ).

In your documentation it is said that the < em> provider JSON standard is the Jettison. This means that it should react to the% JAXB annotation% when set to the accessor type entity, for example, @XmlTransient , and not serialize those methods / attributes annotated in this way.

However the documentation seems to be wrong, as found in this JIRA , showing that it was an bug in the documentation and that the provider actually used is that of Jackson.

Assuming you are using a dependency manager such as , you can do something like this:

  • add the following dependency (here in @XmlAccessorType(XmlAccessType.FIELD) ) to your project:
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>

    <!-- a versão é 1.9.2 por que é a encontrada nos modules do JBoss -->
    <version>1.9.2</version>
    <scope>provided</scope>
</dependency>

The scope will be maven , since JBoss already has such a dependency on its modules .

  • annotate your method provided with isEmpty() , thus:
@JsonIgnore
public boolean isEmpty() {
    return linhasNaoFavoritas.isEmpty() && linhasNaoFavoritas.isEmpty();
}

The above solution is for JBoss AS in the above quoted version, so it may not have the same behavior in other containers JEE.

Another way is to create your own custom provider , as can be seen this example . In this case, you can use the serializer you want to deliver JSON, and finally rename your @JsonIgnore method to a non-default Java Beans name, such as renaming it to something like isEmpty .

If you need to, in this repository you can find a complete example using the approach quoted above, with small differences, but exactly simulate your problem.

    
17.09.2015 / 02:29