D
D
Dmitry Volokitin2021-04-27 17:54:03
Java
Dmitry Volokitin, 2021-04-27 17:54:03

Why does the server return null in Json instead of objects?

There is a database, an entity layer, a dto layer, and the Rest controller itself.
When I pull a Get request converted via mapStruct to return data to the client, I get null instead of data for all rows. But, if the conversion to dto is removed, everything works. How to cure it?

entity

package com.example.demo.model.entity;

import lombok.Data;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "client", schema = "schema_bank")
//@Data
public class ClientEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    String lastName;
    String firstName;
    String middleName;
    String number;
    String email;
    int passport;


    @OneToMany(mappedBy = "clientEntity", fetch = FetchType.LAZY)
    List<CreditEntity> creditEntityList;
    @OneToOne(cascade = CascadeType.ALL, optional = false)
            @JoinColumn(name = "offer_id")
    OfferEntity offerEntity;
    @ManyToOne(optional = false, cascade = CascadeType.ALL)
            @JoinColumn(name = "bank_id")
    BankEntity bankEntity;


DTO
package com.example.demo.model.DTO;

public class ClientDTO {
    Long id;
    String lastName;
    String firstName;
    String middleName;
    String number;

MAP
package com.example.demo.model.converter.interfacies;

import com.example.demo.model.DTO.ClientDTO;
import com.example.demo.model.entity.ClientEntity;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface ClientMap {

    ClientMap INSTANCE = Mappers.getMapper(ClientMap.class);

    ClientDTO toDto(ClientEntity clientEntity);
    ClientEntity toEntity(ClientDTO clientDTO);

}


service layer (implementation)
package com.example.demo.service.impl;

import com.example.demo.model.DTO.ClientDTO;
import com.example.demo.model.converter.interfacies.ClientMap;
import com.example.demo.model.entity.ClientEntity;
import com.example.demo.repo.ClientRepo;
import com.example.demo.service.interfacies.ClientService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClientServiceImpl implements ClientService {
    final ClientRepo repo;

    public ClientServiceImpl(ClientRepo repo) {
        this.repo = repo;
    }

    @Override
    public ClientDTO create(ClientDTO clientDTO) {
        ClientEntity create = repo.save(ClientMap.INSTANCE.toEntity(clientDTO));
        return ClientMap.INSTANCE.toDto(create);
    }

    @Override
    public ClientDTO edit(ClientDTO clientDTO) {
        ClientEntity edit = repo.save(ClientMap.INSTANCE.toEntity(clientDTO));
        return ClientMap.INSTANCE.toDto(edit);
    }

    @Override
    public void delete(Long id) {
    repo.deleteById(id);
    }

    @Override
    public List<ClientDTO> findAll() {

       return repo.findAll().stream()
                .map(ClientMap.INSTANCE::toDto)
                .collect(Collectors.toList());

    }
    @Override
    public List<ClientEntity> all() {

        return repo.findAll();

    }
}

controller
package com.example.demo.rest;

import com.example.demo.model.DTO.ClientDTO;
import com.example.demo.model.entity.ClientEntity;
import com.example.demo.service.impl.ClientServiceImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clients")
public class ClientController {
    final ClientServiceImpl service;

    public ClientController(ClientServiceImpl service) {
        this.service = service;
    }

    @PostMapping("/create")
    public ClientDTO create (@RequestBody ClientDTO clientDTO){
        return service.create(clientDTO);
    }
    @PostMapping("/edit")
    public ClientDTO edit (@RequestBody ClientDTO clientDTO){
        return service.edit(clientDTO);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){
        service.delete(id);
    }
    @GetMapping("/all")
    public List<ClientDTO> getAll(){
        return service.findAll();
    }
    @GetMapping("/alls")
    public List<ClientEntity> all(){
        return service.all();
    }
}


[
    {
        "id": null,
        "lastName": null,
        "firstName": null,
        "middleName": null,
        "number": null,
        "email": null,
        "passport": 0
    }
]


[
    {
        "id": 2,
        "lastName": "volokitin",
        "firstName": "dmitry",
        "middleName": "vitaljevich",
        "number": "1234567890",
        "email": "[email protected]",
        "passport": 123456,
        "creditEntityList": [],
        "offerEntity": null,
        "bankEntity": null
    }
]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Volokitin, 2021-04-27
@Dima_gogi_ya

Solution:
MapStract did not work with Lombok. Lombok deleted, everything worked)

O
Orkhan, 2021-04-27
Hasanly @azerphoenix

But, if the conversion to dto is removed, everything works. How to cure it?

It means that something is wrong at the conversion stage. Debug the project and see what's wrong

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question