Spring Boot DTO values are recevied null

Viewed 35

I send this data from Front with headers {'Accept': 'application/json', 'Content-Type': 'application/json'}

{
    "libelle": "Article AB",
    "dimension": {
        "id": 0,
        "code": "BIJOUTERIE",
        "label": "Bijouterie"
    },
    "categorie": {
        "id": 1,
        "code": "FEMMES",
        "label": "Femmes"
    },
    "famille": {
        "id": 0,
        "code": "BELDI",
        "label": "Beldi"
    },
    "type": {
        "id": 2,
        "code": "MDEMA",
        "label": "Mdema"
    },
    "picture": "text",
    "description": null,
    "prixVente": 5000,
    "poids": null,
    "status": null,
    "enabled": true,
    "barcode": "4351951317874",
    "annee": null,
    "boitier": null,
    "mouvement": null,
    "taille": null,
    "diametre": null
}

I receive it like this, only one value and others are null, nest values are null

libelle=Article AB,
description=null,
dimension=null,
enabled=true,
categorie=null,
famille=null,
type=null,
picture=null,
prixVente=0.0,
poids=0.0,
status=null,
barcode=null,
annee=0,
boitier=null,
mouvement=null,
taille=0,
diametre=0.0

this is my post function in front

 post(url: string, body: any, options?: any): Observable<any> {
        return this.http.post<any>(url, JSON.stringify(body), newHeaders(options));
    }

this is my Rest controller

import org.springframework.web.bind.annotation.ResponseBody;
    @RequestMapping(value = "/article", method = RequestMethod.POST)
    @PreAuthorize("hasRole('ROLE_CREATE_ARTICLE')")
    public ResponseEntity<Long> addArticle(@RequestBody ArticleDto article) throws Exception {
        
        article = articleService.createArticle(article);
        
        return new ResponseEntity<Long>(article.getId(), HttpStatus.CREATED);

    }

ArticleDto

package com.sighimo.dto;



import com.sighimo.common.AuditBaseDto;
import com.sighimo.common.Utils;
import com.sighimo.common.enumeration.Categorie;
import com.sighimo.common.enumeration.Dimension;
import com.sighimo.common.enumeration.Famille;
import com.sighimo.common.enumeration.Mouvement;
import com.sighimo.common.enumeration.Type;
import com.sighimo.models.Article;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
@Getter
@Setter
public class ArticleDto extends AuditBaseDto {

    /** Libelle */
    private String libelle;

    /** Description */
    private String description;

    /** Adresse */
    private Dimension dimension;
    
    private boolean enabled = true;
    //Enum
    private Categorie categorie;
    //Enum
    private Famille famille;
    //Enum
    private Type type;
    private String picture;
    private double prixVente;
    private double poids;
    private String status;
    private String barcode;
    private int annee;
    private String boitier;
    //Enum
    private Mouvement mouvement;
    // pierre;
    private int taille;
    private double diametre;

                        
    /**
     * Constructeur par défaut.
     */
    public ArticleDto() {
        super();
    }
    
    /**
     * Constructeur.
     * @param id clé primaire
     */
    public ArticleDto(Long id) {
        super(id);
    }
    
    public ArticleDto(Article article, boolean collections, int level) {
        super();
        convertToDto(this, article, collections, level);
    }

    public ArticleDto(Article article, boolean collections) {
        super();
        convertToDto(this, article, collections, 0);
    }
    
    public ArticleDto(Article article) {
        super();
        convertToDto(this, article, false, 0);
    }
    
    public Article convertIdToEntity(Article article, ArticleDto articleDto) {

        article.setId(articleDto.getId());

        return article;
    }
    
    public ArticleDto convertIdToDto(ArticleDto articleDto, Article article) {

        articleDto.setId(article.getId());

        return articleDto;
    }   
    
    public Article convertToEntity(Article article, ArticleDto articleDto) {

        if (article != null) {
            article = convertIdToEntity(article,articleDto);
            article.setLibelle(articleDto.getLibelle());
            article.setDescription(articleDto.getDescription());
            article.setPicture(articleDto.getPicture());
            article.setStatus(articleDto.getStatus());
            article.setPrixVente(articleDto.getPrixVente());
            article.setBarcode(articleDto.getBarcode());
            //Bijouterie
            if(articleDto.getCategorie() != null) {
                article.setCategorie(articleDto.getCategorie().name());
            }
            if(articleDto.getType() != null) {
                article.setType(articleDto.getType().name());
            }
            if(articleDto.getMouvement() != null) {
                article.setMouvement(articleDto.getMouvement().name());
            }
            if(articleDto.getDimension() != null) {
                article.setDimension(articleDto.getDimension().name());
            }
            if(articleDto.getFamille() != null) {
                article.setFamille(articleDto.getFamille().name());
            }
            article.setPoids(articleDto.getPoids());
            article.setTaille(articleDto.getTaille());
            //Horlogerie
            article.setAnnee(articleDto.getAnnee());
            article.setBoitier(articleDto.getBoitier());
            article.setDiametre(articleDto.getDiametre());
            article.setEnabled(articleDto.isEnabled());
            
            
        }
        
        return article;
    }   

    public ArticleDto mappedCustomDto( ArticleDto articleDto, String[] includes, String[] excludes) {

        if (Utils.isNoEmpty(includes) || Utils.isNoEmpty(excludes)) {
            if (Utils.isNoEmpty(includes)) {
                Utils.copyIncludesProperties(articleDto, this, includes);
            }
            if (Utils.isNoEmpty(excludes)) {
                Utils.copyExcludesProperties(articleDto, this, excludes);
            }

            this.id = articleDto.getId();
            
            return this;
        }

        return articleDto;
    }
    
    public ArticleDto convertToDto(ArticleDto articleDto, Article article, boolean collections, int level) {
        
        level++;
        if (articleDto != null && level <= maxLevel) {

                articleDto = convertIdToDto(articleDto, article);
                articleDto.setLibelle(article.getLibelle());
                articleDto.setDescription(article.getDescription());
                articleDto.setPicture(article.getPicture());
                articleDto.setStatus(article.getStatus());
                articleDto.setPrixVente(article.getPrixVente());
                articleDto.setBarcode(article.getBarcode());
                articleDto.setEnabled(article.isEnabled());
                articleDto.setPoids(article.getPoids());
                //Bijouterie
                articleDto.setDimension(Dimension.forValue(article.getDimension()));
                articleDto.setCategorie(Categorie.forValue(article.getCategorie()));
                articleDto.setFamille(Famille.forValue(article.getFamille()));
                articleDto.setType(Type.forValue(article.getType()));
                articleDto.setTaille(article.getTaille());
                //Horlogerie
                articleDto.setMouvement(Mouvement.forValue(article.getMouvement()));
                articleDto.setAnnee(article.getAnnee());
                articleDto.setBoitier(article.getBoitier());
                articleDto.setDiametre(article.getDiametre());
                
                articleDto.setCreatedBy(article.getCreatedBy());
                articleDto.setCreatedOn(Utils.dateTimeToString(article.getCreatedOn()));
                articleDto.setUpdatedBy(article.getUpdatedBy());
                articleDto.setUpdatedOn(Utils.dateTimeToString(article.getUpdatedOn()));
                
            
                if (collections) {
                }               
            
        }
        
        return articleDto;
    }   
    
}

I tried to add @JsonProperty, @Builder but same problem

EDIT : using Objectmapper I'm getting this error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "label" (class com.sighimo.dto.ArticleDto), not marked as ignorable (18 known properties: "barcode", "enabled", "libelle", "dimension", "taille", "famille", "status", "diametre", "categorie", "poids", "type", "id", "description", "picture", "prixVente", "annee", "boitier", "mouvement"])

1 Answers

Add @AllArgConstructor ,@NoArgsConstructor and @RequiredArgConstructor to the ArticleDTO class and remove the manually added constructors. That annotations automatically create constructors for you. Then add above annotations to all other classses such as Dimension which have refetences inside articleDTO. This should work.

Related