Boa tarde pessoal da Alura!
Estou fazendo um backend para estudo, a temática do meu projeto é ser um "ifood"... Enfim, estou desenvolvendo um fix para corrigir algumas coisas no método POST para criar um pedido e toda vez que mando uma requisição no postman estou recebendo essa exception:
"message": "JSON parse error: Cannot deserialize value of type `java.lang.Long` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.lang.Long` from Object value (token `JsonToken.START_OBJECT`)\n at [Source: (PushbackInputStream); line: 5, column: 6] (through reference chain: br.com.develfoodspringweb.develfoodspringweb.controller.form.RequestForm[\"platesId\"]->java.util.ArrayList[0])",
A minha classe RequestForm é o seguinte:
package br.com.develfoodspringweb.develfoodspringweb.controller.form;
import br.com.develfoodspringweb.develfoodspringweb.models.Plate;
import br.com.develfoodspringweb.develfoodspringweb.models.StatusRequest;
import br.com.develfoodspringweb.develfoodspringweb.models.User;
import br.com.develfoodspringweb.develfoodspringweb.models.Request;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* Created By Luis Gregorio
*
* Class created to collect information from the application and send it to the API
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RequestForm {
private Long id;
private StatusRequest status;
@NotNull
private String obs;
private User user;
private Long restaurantId;
private List<Long> platesId;
private List<Plate> plates;
private int quantity;
public Request convertToUserRequest (RequestForm requestForm){
return new Request(requestForm);
}
}
a minha classe RequestDto:
package br.com.develfoodspringweb.develfoodspringweb.controller.dto;
import br.com.develfoodspringweb.develfoodspringweb.models.Plate;
import br.com.develfoodspringweb.develfoodspringweb.models.Request;
import br.com.develfoodspringweb.develfoodspringweb.models.StatusRequest;
import br.com.develfoodspringweb.develfoodspringweb.models.User;
import com.fasterxml.jackson.annotation.JsonIgnore;
import br.com.develfoodspringweb.develfoodspringweb.models.StatusRequest;
import br.com.develfoodspringweb.develfoodspringweb.models.User;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
public class RequestDto {
private Long id;
private StatusRequest status;
private User user;
private String dateRequest;
private List<PlateDto> plateDtos;
private String obs;
private Double priceTotal;
@JsonIgnore
private List<Plate> plates;
public RequestDto(Request request){
this.id = request.getId();
this.status = request.getStatus();
this.dateRequest = request.getDateRequest();
this.user = request.getUser();
this.obs = request.getObs();
this.priceTotal = request.getPriceTotal();
this.plates = request.getPlateId();
this.converToListDto(request.getPlateId());
}
/**
* Function to convert PlateModel class, receveid when creating a request, to a list of DTO class which is called from the constructor above in this class
* @param plates
* @return
* @author: Thomas Benetti
*/
private void converToListDto(List<Plate> plates) {
if(this.plateDtos == null){
this.plateDtos = new ArrayList<>();
}
plates.stream().forEach(plate -> this.plateDtos.add(new PlateDto(plate)));
}
/**
* Function to convert Model class to a list of DTO class
* @param request
* @return
* @author: Thomas Benetti
*/
public static RequestDto convertToRequestDto (Request request){
return new RequestDto(request);
}
}