Values do not serialize in return in REST with Jersey

Asked

Viewed 117 times

1

Good morning, you guys, I’m new here and beginner also with Java and I’m having a problem with the serialization of an object and no longer know what to do.

I am giving, at least trying, to maintain a code here in the company, I have a class below and some attributes are not being serialized, as the id, I tried to create other attributes like the idTipoSituacao, codigoIdentificador and testeCodigoIdentificado but they also do not return in the REST API, I tried to change the value of an attribute that was already being sent/Serialized as the codSituacao for the value of the id I need to pick up the Angular that is on Frontend and it was quiet. So I don’t know if any place has any mapping for this class, and if you have already looked everywhere and did not find, if you have any suggestions where to look thank you. the annotations that are in the attribute I don’t understand them very well yet but for what I’ve been researching they are not the ones that ignore an attribute in the serialization, I tried to leave without annotation in the attribute too and nothing changes, the attribute still not appearing.

@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 Neat the code at this point above when the return of the EJB comes with the object the values are all right there. something is happening from this Return there until it reaches the screen. Both when it returns XML and JSON t the same way.

From what I saw in POM.XML this using the following Jersey dependency

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

Other than that I didn’t 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

2 answers

0


In order for a class attribute to be serialized, it must have the annotation @Jsonproperty OR have the access function get, example getId.

OBS:

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

I hope I made it clear.

0

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

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

Thank you

Browser other questions tagged

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