Values do not serialize on return in REST with Jersey

1

Good morning everyone, I'm new here and I'm new to Java too and I'm having a problem with the serialization of an object and I do not know what else to do.

I'm giving, at least trying to maintain a code here in the company, I have a class below and some attributes are not being serialized, like the id, I tried to create other attributes like idTipoSituacao , codigoIdentificador I am trying to change the value of an attribute that was already being sent / serialized as testeCodigoIdentificado by the value of the id that I need to get in the angle that is in the FrontEnd and it was quiet. I do not know if some place has some mapping for this class, and if it has already looked everywhere and I did not find it, if you have any suggestion of where to look for thank you. the annotations that are in the attribute I do not understand them yet very well but from what I have been researching they are not they that ignore an attribute in the serialization, I tried to leave without annotation in the attribute also and nothing changes, the attribute does not appear.

@JsonRootName("tsdSituacaoDTO")
@JsonInclude
@XmlRootElement(name = "tsdSituacaoDTO")
public class TsdTipoSituacaoDTO extends SisprogModelDTO implements InterfaceDTO<TsdTipoSituacao> {

@Id
@SerializedName(value = "id")
private Integer id;

@JsonProperty("idTipoSituacao")
private Integer idTipoSituacao;

@JsonProperty("codigoIdentificado")
private Integer codigoIdentificador;

@SerializedName(value = "testeCodigoIdentificado")
private Integer testeCodigoIdentificado;

@Size(max = 20)
@JsonProperty("codSituacao")
private String codSituacao;

@Size(max = 50)
@JsonProperty("descricao")
private String descricao;

@JsonProperty("bloquear")
private Boolean bloquear;

@Size(max = 20)
@JsonProperty("corSituacao")
private String corSituacao;

@JsonProperty("caracteristica")
private Short caracteristica;

@JsonProperty("modSituacao")
private Integer modSituacao;

@JsonProperty("tipoControle")
private Integer tipoControle;

@JsonProperty("tsdTipoSituacaoMotivoList")
private List<TsdTipoSituacaoMotivoDTO> tsdTipoSituacaoMotivoList;

The bairo is the Resource

@Stateless
@Path("tsd/cadastros/tsddto")
public class TsdDTOResource {

// Verificar codigo do tsd
private final static String COD_MENU = "105001";
private final static String ENTITY = "Tsd";

@Context
private MessageBodyWriter<Tsd> context;
private TsdFacadeLocal tsdFacade;
private Gson gson;
private Type typeMap;

public TsdDTOResource() {
    try {
        gson = new Gson();
        typeMap = new TypeToken<Map<String, Object>>() {
        }.getType();
        tsdFacade = (TsdFacadeLocal) ServiceLocator.buscarEJB(ENTITY);
    } catch (NamingException ex) {
        Logger.getLogger(TsdDTOResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new GenericException("Erro ao inicializar o TSD!", ex);
    }
}


@GET
//@SecurityCheck(CodMenu = COD_MENU, Entity = ENTITY, Operacao = SisgerOperacaoEnum.CONSULTAR) <-- Comentei pra poder testar sem entrar em todo sistema
//@Interceptors(SecurityCheckInterceptor.class) <-- Comentei pra poder testar sem entrar em todo sistema
@Path("find/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public TsdDTO find(@PathParam("id") Integer id) {
    TsdDTO tsd = tsdFacade.findDTO(id);
    return tsd;
}

Detail, when I debug the code at this point above when the EJB return comes with the object the values are all there right. something is happening from that return there until you get to the screen. Both when returning XML and JSON it is the same way.

What I saw in POM.XML is using the following Jersey dependency

<dependency>
   <groupId>org.glassfish.jersey.ext</groupId>
   <artifactId>jersey-bean-validation</artifactId>
   <version>2.26</version>
 </dependency>

Otherwise I did not find any @Provider annotated class that was searched that could be intercepted by Jersey to map the conversion.

Below is the result that is being generated when the resource is consumed

<tsdTipoSituacao>
  <bloquear>false</bloquear>
  <caracteristica>11</caracteristica>
  <codSituacao>24</codSituacao>
  <corSituacao/>
  <descricao>Tipo Situação Teste 2 Sisprog</descricao>
  <modSituacao>1</modSituacao>
</tsdTipoSituacao>

Thank you

    
asked by anonymous 23.02.2018 / 14:20

2 answers

0

In order for the attribute of a class to be serialized, it must have the @JsonProperty OU get access function, example getId .

OBS:

  • When the name of the attribute to be serialized differs from the class attribute access function, you can use the annotation next to get . Example:
  • @JsonProperty("identificador")
    public Integer getId(){
        return id;
    }
    
  • When the access function has the same name as the attribute to be serialized, it is not necessary to report the annotation (neither the attribute nor the get ). Example:
  • @JsonProperty("id") //ANOTAÇÃO DESNECESSÁRIA
    public Integer getId(){
        return id;
    }
    

    I hope to have clarified.

        
    26.02.2018 / 22:13
    0

    I was able to get the attribute serialized. I believe it was the lack of the setId () method, had not realized it was out.

    I only need to understand now if I really need this @JsonProperty annotation with the attribute name, because without it it is not appearing in the result.

    Thank you

        
    23.02.2018 / 15:51