Problem z dodaniem obiektu JSON

0

Cześć, jestem w trakcie pisania aplikacji i znalazłem się w kropce. Sprawa jest o tyle dziwniejsza, że dla dodania innego obiektu wszystko działa. W ensie, że jak dodaję obiekt EngineExemplar który jest napisany tak samo jak GearBoxExemplar to wszystko działa, natomiast dla tego drugiego w Postmanie wyrzuca mi taki błąd:

{
    "timestamp": "2022-09-30T05:33:22.890+0000",
    "status": 400,
    "error": "Bad Request",
    "errors": [
        {
            "codes": [
                "typeMismatch.gearBoxExemplarDto.available",
                "typeMismatch.available",
                "typeMismatch.boolean",
                "typeMismatch"
            ],
            "arguments": [
                {
                    "codes": [
                        "gearBoxExemplarDto.available",
                        "available"
                    ],
                    "arguments": null,
                    "defaultMessage": "available",
                    "code": "available"
                }
            ],
            "defaultMessage": "Failed to convert value of type 'null' to required type 'boolean'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [@com.fasterxml.jackson.annotation.JsonProperty boolean] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type",
            "objectName": "gearBoxExemplarDto",
            "field": "available",
            "rejectedValue": null,
            "bindingFailure": true,
            "code": "typeMismatch"
        }
    ],
    "message": "Validation failed for object='gearBoxExemplarDto'. Error count: 1",
    "path": "/gearBoxesExemplars/addGearBoxExemplar"
}

Natomiast encje, service dto, mapper i contoller wyglądają tak:
Encja:

import com.sun.istack.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity(name = "GEARBOXES_EXEMPLARS")
public class GearBoxExemplar {

    @Id
    @GeneratedValue
    @NotNull
    @Column(name = "GEARBOX_EXEMPLAR_ID", unique = true)
    private Long id;

    @NotNull
    @Column(name = "SERIAL_NUMBER")
    private String serialNumber;

    @NotNull
    @Column(name = "IS_AVAILABLE")
    private boolean available;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "GEARBOXES_ID")
    private GearBox gearbox;

    public GearBoxExemplar(Long id, String serialNumber, boolean available) {
        this.id = id;
        this.serialNumber = serialNumber;
        this.available = available;
    }
}

Service:

import com.restap.carshop.domain.GearBoxExemplar;
import com.restap.carshop.exception.GearBoxExemplarException;
import com.restap.carshop.repository.GearBoxesExemplarsRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GearBoxExemplarService {

    private GearBoxesExemplarsRepository gearBoxesExemplarsRepository;

    @Autowired
    public GearBoxExemplarService(GearBoxesExemplarsRepository gearBoxesExemplarsRepository) {
        this.gearBoxesExemplarsRepository = gearBoxesExemplarsRepository;
    }

    public GearBoxExemplar addGearBoxExemplar(final GearBoxExemplar gearBoxExemplar) {
        return gearBoxesExemplarsRepository.save(gearBoxExemplar);
    }

    public List<GearBoxExemplar> findAllGearBoxesExemplars() {
        return gearBoxesExemplarsRepository.findAll();
    }

    public GearBoxExemplar findGearBoxExemplarById(final Long id) throws GearBoxExemplarException {
        return gearBoxesExemplarsRepository.findById(id).orElseThrow(GearBoxExemplarException::new);
    }

    public void deleteGearBoxExemplar(final Long id) throws GearBoxExemplarException {
        if (gearBoxesExemplarsRepository.findById(id).isPresent()) {
            gearBoxesExemplarsRepository.deleteById(id);
        } else {
            throw new GearBoxExemplarException();
        }
    }

    public GearBoxExemplar findGearBoxExemplarBySerialNumber(final String serialNumber) throws GearBoxExemplarException {
        return gearBoxesExemplarsRepository.findBySerialNumber(serialNumber).orElseThrow(GearBoxExemplarException::new);
    }

    public GearBoxExemplar findEngineExemplarByAvailable(final boolean available) throws GearBoxExemplarException {
        return gearBoxesExemplarsRepository.findByAvailable(available).orElseThrow(GearBoxExemplarException::new);
    }
}

Dto:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class GearBoxExemplarDto {

    @JsonProperty("id")
    private Long id;

    @JsonProperty("serialNumber")
    private String serialNumber;

    @JsonProperty("available")
    private boolean available;
    //private Boolean available;

    @JsonProperty("gearBoxId")
    private Long gearBoxId;
}

Mapper:

import com.restap.carshop.domain.GearBoxExemplar;
import com.restap.carshop.dto.GearBoxExemplarDto;
import com.restap.carshop.exception.GearBoxException;
import com.restap.carshop.service.GearBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class GearBoxExemplarMapper {

    @Autowired
    private GearBoxService gearBoxService;

    public GearBoxExemplar mapToGearBoxExemplar(final GearBoxExemplarDto gearBoxExemplarDto) throws  GearBoxException {
        GearBoxExemplar gearBoxExemplar = new GearBoxExemplar(gearBoxExemplarDto.getId(),
                gearBoxExemplarDto.getSerialNumber(),
                gearBoxExemplarDto.isAvailable());
                //gearBoxExemplarDto.getAvailable());
        gearBoxExemplar.setGearbox(gearBoxService.getGearBoxById(gearBoxExemplarDto.getGearBoxId()));
        return gearBoxExemplar;
    }

    public GearBoxExemplarDto mapToGearBoxExemplarDto(final GearBoxExemplar gearBoxExemplar) {
        return new GearBoxExemplarDto(gearBoxExemplar.getId(),
                gearBoxExemplar.getSerialNumber(),
                gearBoxExemplar.isAvailable(),
                gearBoxExemplar.getGearbox().getId());
    }

    public List<GearBoxExemplarDto> mapToGearBoxesExemplarsDtoList(final List<GearBoxExemplar> gearBoxesExemplars) {
        return gearBoxesExemplars.stream().map(this::mapToGearBoxExemplarDto).collect(Collectors.toList());
    }
}

Controller:

import com.restap.carshop.domain.GearBoxExemplar;
import com.restap.carshop.dto.GearBoxExemplarDto;
import com.restap.carshop.exception.GearBoxException;
import com.restap.carshop.exception.GearBoxExemplarException;
import com.restap.carshop.mapper.GearBoxExemplarMapper;
import com.restap.carshop.service.GearBoxExemplarService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping(value = "/gearBoxesExemplars")
@RequiredArgsConstructor
public class GearBoxExemplarController {

    private final GearBoxExemplarMapper gearBoxExemplarMapper;
    private final GearBoxExemplarService gearBoxExemplarService;

    @RequestMapping(method = RequestMethod.POST, value = "addGearBoxExemplar", consumes = MediaType.APPLICATION_JSON_VALUE)
    public void addGearBoxExemplar(final GearBoxExemplarDto gearBoxExemplarDto) throws GearBoxException {
        GearBoxExemplar gearBoxExemplar = gearBoxExemplarMapper.mapToGearBoxExemplar(gearBoxExemplarDto);
        gearBoxExemplarService.addGearBoxExemplar(gearBoxExemplar);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "deleteGearBoxExemplar")
    public void deleteGearBoxExemplar(final Long id) throws GearBoxExemplarException {
        gearBoxExemplarService.deleteGearBoxExemplar(id);
    }

    @RequestMapping(method = RequestMethod.GET, value = "getGearBoxesExemplars")
    public List<GearBoxExemplarDto> getGearBoxesExemplars() {
        List<GearBoxExemplar> gearBoxExemplars = gearBoxExemplarService.findAllGearBoxesExemplars();
        return gearBoxExemplarMapper.mapToGearBoxesExemplarsDtoList(gearBoxExemplars);
    }

    @RequestMapping(method = RequestMethod.GET, value = "getGearBoxExemplarById")
    public GearBoxExemplarDto getGearBoxExemplarById(final Long id) throws GearBoxExemplarException {
        return gearBoxExemplarMapper.mapToGearBoxExemplarDto(
                gearBoxExemplarService.findGearBoxExemplarById(id));
    }

    @RequestMapping(method = RequestMethod.GET, value = "getGearBoxExemplarBySerialNumber")
    public GearBoxExemplarDto getGearBoxExemplarBySerialNumber(final String serialNumber) throws GearBoxExemplarException {
        return gearBoxExemplarMapper.mapToGearBoxExemplarDto(
                gearBoxExemplarService.findGearBoxExemplarBySerialNumber(serialNumber));
    }

    @RequestMapping(method = RequestMethod.GET, value = "getGearBoxExemplarByAvailable")
    public GearBoxExemplarDto getGearBoxExemplarByAvailable(final boolean available) throws GearBoxExemplarException {
        return gearBoxExemplarMapper.mapToGearBoxExemplarDto(
                gearBoxExemplarService.findEngineExemplarByAvailable(available));
    }
}

Porównywałem te klasy już z 10 razy z klasami dla EngineExemplar i wszystko jest napisane tak samo, a mimo to nie działa. Ktoś wie o co może chodzić?

2

blad Failed to convert value of type 'null' to required type 'boolean' w obiekcie gearBoxExemplarDtow polu available

nie mozna przekonwertowac nulla na boola

0
marcyse napisał(a):

blad Failed to convert value of type 'null' to required type 'boolean' w obiekcie gearBoxExemplarDtow polu available

nie mozna przekonwertowac nulla na boola

Ale właśnie problem w tym, że nie konwertuje. W postmnie body u mnie wygląda tak:Błąd.jpg

0

zdebuguj to, obstawiam, ze problem mozesz miec w mapperze (nie zaglebialem sie zbytnio w kod)

0

Wiecie coooo .....

<złośliwość>
Nie dalej jak w tych dniach importowałem bardziej złożone JSON-y, nie tylko że nie w Springu, ale wręcz w C#. Jak leciał wyjątek, to miał oszałamiające 3 ramki na Stacktrace

Jak wszedłem w ten post, spadłem z krzesła na ilość linii jaką spring-korpo-programista musi popełnić w tym celu. To naprawdę musi być tyle ?
</złośliwość>

marcyse napisał(a):

zdebuguj to ... (nie zaglebialem sie zbytnio w kod)

Ja też

1 użytkowników online, w tym zalogowanych: 0, gości: 1