Define which method is not serialized

Asked

Viewed 172 times

1

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

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 answer:

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

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

Is it possible to define that this method is despised at the time of serialization? If so, how to do it?

  • @Brunocésar, sim, o Provider padrão. I made an issue in the question.

  • I added yes, but it didn’t work. It belongs to the javax.xml.bind.annotation.Xmltransient package?

  • I am using Jboss7. To be exact Jboss 7.1.

1 answer

1


Resteasy is the preview JAX-RS on Jboss AS. In version 7.1.1.Final of Jboss AS the version of Resteasy is the 2.3.2.Final (can be seen in modules\org\jboss\resteasy).

In his documentation it is said that the preview JSON default is Jettison. This means that it should react to the JAXB annotation @XmlTransient when configured in the entity accessor type, for example, @XmlAccessorType(XmlAccessType.FIELD), and not serialize such annotated methods/attributes in this way.

However the documentation seems to be wrong, as found in this JIRA, showing that it was a bug in the documentation and that the preview actually used is Jackson’s.

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

  • add the following dependency (here in the case of maven) 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 provided, since Jboss already has such dependence on its modules.

  • note down your method isEmpty() with @JsonIgnore, being like this:
@JsonIgnore
public boolean isEmpty() {
    return linhasNaoFavoritas.isEmpty() && linhasNaoFavoritas.isEmpty();
}

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

Another way is to create your own preview customized, as can be seen in this example. In this case you can use the serializer you want to deliver JSON, and finally rename your method isEmpty for a name that is not standard Java Beans, such as renaming it to something like hasFavorites.

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

  • Even adding the annotation the problem persists.

  • @Geisonsantos what is the full version of jboss? Can you provide a full example with the bug? It could be a gist same, because I tested this type in this version and it worked ok

  • Jboss-as-7.1.1.Final. No error occurs. The problem is that it serializes the method. Which results in the attribute empty in my json.

  • @Geisonsantos error was just the way to call the problem. As I said, here was OK with this approach, if it can generate a sample project for me to try to reproduce your problem... Vice is using default settings, right? If you have changed anything, also inform.

  • 1

    Bruno, I downloaded and tested the Github sample project. It really works perfectly. My problem must be something specific, something related to lib conflict. In fact, the problem no longer exists, because I removed the methods, they were not so important ;). I will signal your answer as correct. Thank you for your attention and congratulations for your dedication. Your participation is very important to Sopt.

Browser other questions tagged

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