Answer the question
In order to leave comments, you need to log in
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;
package com.example.demo.model.DTO;
public class ClientDTO {
Long id;
String lastName;
String firstName;
String middleName;
String number;
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);
}
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();
}
}
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
Solution:
MapStract did not work with Lombok. Lombok deleted, everything worked)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question