0

I'm working on a rest web service based on spring boot ,I'm using lombok for generating getter and setter but when I called my web service I get my object with fields with default values null this my code :

My entity :

@Data
@Entity
@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Projet implements Serializable {
    /**
     *  Serial UID
     */
    private static final long serialVersionUID = -7268509549664338191L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long codeProjet ;
    private String description;
    private String responsable ;
    private Double budget ;
    private Date dateDebut;
    private Date dateFin ;
    @OneToMany(mappedBy="codeQuestonnaire")
    @JsonIgnore
    private List<Questionnaire> questionnaires ;

}

This my rest service :

@RestController
public class ProjectResource {

    @Autowired
    private ProjetRepository projetRepository ;

    @GetMapping("/projet/{id}")
    public ResponseEntity<Projet> getProjet(@PathVariable("id") long id){
        Projet projet = projetRepository.getOne(id);
        if(projet!=null) {
            return new ResponseEntity<Projet>(projet,HttpStatus.OK);
        }else {
            return new ResponseEntity<Projet>(HttpStatus.NOT_FOUND);
        }

    }
}

In my configuration file I added this line :

spring.jackson.serialization.fail-on-empty-beans=false

When I called my service I get this response :

{"codeProjet":null,"description":null,"responsable":null,"budget":null,"dateDebut":null,"dateFin":null,"handler":{},"hibernateLazyInitializer":{}}

If I added getters and setters to my class it works correctly and I get json response with values Thanks in advance for any help

e2rabi
  • 4,728
  • 9
  • 42
  • 69

3 Answers3

1

I think you hit the same problem as here No serializer found for class org.hibernate.proxy.pojo.javassist.Javassist?.

Instead of configuring spring.jackson.serialization.fail-on-empty-beans=false, annotate your entity with @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}).

K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95
1

Just replace getOne method to findById (or findOne for Spring Boot 1.5+).

Method getOne return the reference to the entity (its proxy), not the entity.

More info see here.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
0

Solved The problem is that I used HikariCp the default connection pool without adding the required configuration in the properties file.

e2rabi
  • 4,728
  • 9
  • 42
  • 69