www.state-it.info

Gran base de respuestas a las preguntas de los desarrolladores
Header decor

¿por Qué el campo no парсится y sigue siendo null?

SlandShow 2018-10-28 11:38:58

Tengo el REST, el controlador que saca los datos de la base (энтити) y lo envuelve en el DTO, que se serializan en JSON.

@GetMapping("/api/board/schedule/{id}")
    public ResponseEntity<?> getScheduleById(@PathVariable Long id) {
        ScheduleDTO schedule = scheduleService.getByIdScheduleDTO(id);

        LOGGER.info("RETRIEVED " + schedule + " SCHEDULER");
        return ResponseEntity.ok(schedule);
    }


Primero saco энтити:
INFO    2018-10-28 10:49:55,690 [http-nio-8080-exec-5] com.slandshow.service.Impl.ScheduleServiceImpl  - SELECTED BY ID = 147 SCHEDULE IS Schedule ( id: 147, 2018-10-30 20:04:51.0 → 2018-10-30 22:21:13.0, Yaztennaya → Laxta, train: Fenix )
INFO    2018-10-28 10:49:55,735 [http-nio-8080-exec-5] com.slandshow.controllers.BoardScheduleController  - RETRIEVED [email protected] SCHEDULER


A continuación, el controlador me devuelve envuelto JSON a través de DTO:
{"id":147,"stationDepartureName":"Yaztennaya","stationArrivalName":"Laxta","trainName":"Fenix","dateDeparture":"2018-10-30 20:04:51.0","dateArrival":"2018-10-30 22:21:13.0","price":0}


Entonces, quiero дессериализовать es el caso.

Esto se hace a través de Jackson:
private String getResultResponse(String URL) {
        WebResource webResource = client.resource(URL);
        return webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class).getEntity(String.class);
}
 
// ...
ObjectMapper objectMapper = new ObjectMapper();
String json = getResultResponse(Utils.URL_SCHEDULE_BY_ID + 147);
        try {
            System.out.println(("PARSED -> " + objectMapper.readValue(json, Schedule.class)));
        } catch (IOException e) {
            e.printStackTrace();
}


Así que, al parecer, todo ok Pero no, Jackson no дессериализует un campo trainName, no entiendo por qué... Todo el resto de los campos correctamente парсятся.

Aquí está mi DTO, que сериализую:
public class ScheduleDTO implements Serializable {

    private Long id;

    private String stationDepartureName;

    private String stationArrivalName;

    private String trainName;

    private String dateDeparture;

    private String dateArrival;

    private Integer price;

    public ScheduleDTO() {
    }

    public ScheduleDTO(String trainName) {
        this.trainName = trainName;
    }

    // Setters

    public void setId(Long id) {
        this.id = id;
    }

    public void setStationDepartureName(String stationDepartureName) {
        this.stationDepartureName = stationDepartureName;
    }

    public void setStationArrivalName(String stationArrivalName) {
        this.stationArrivalName = stationArrivalName;
    }

    public void setTrainName(String trainName) {
        this.trainName = trainName;
    }

    public void setDateDeparture(String dateDeparture) {
        this.dateDeparture = dateDeparture;
    }

    public void setDateArrival(String dateArrival) {
        this.dateArrival = dateArrival;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    // Getters

    public Long getId() {
        return id;
    }

    public String getStationDepartureName() {
        return stationDepartureName;
    }

    public String getStationArrivalName() {
        return stationArrivalName;
    }

    public String getDateDeparture() {
        return dateDeparture;
    }

    public String getDateArrival() {
        return dateArrival;
    }

    public Integer getPrice() {
        return price;
    }

    public String getTrainName() {
        return trainName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ScheduleDTO that = (ScheduleDTO) o;

        return ((ScheduleDTO) o).trainName.equals(this.getTrainName());
    }

    @Override
    public int hashCode() {
        return trainName.hashCode();
    }
}


Y he aquí el modelo de programación, a través de la que estoy tratando de analizar Jackson'ohm:
import java.io.Serializable;

public class Schedule implements Serializable {

    private Long id;

    private String dateDeparture;

    private String dateArrival;

    private String trainName;

    private String stationDepartureName;

    private String stationArrivalName;

    private Integer price;

    /* Getters & setters */
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public String getDateDeparture() { return dateDeparture; }
    public void setDateDeparture(String dateDeparture) { this.dateDeparture = dateDeparture; }

    public String getDateArrival() { return dateArrival; }
    public void setDateArrival(String dateArrival) { this.dateArrival = dateArrival; }

    public String getTrainName() { return trainName; }
    public void setTrainName(String train) { this.trainName = trainName; }

    public String getStationDepartureName() { return stationDepartureName; }
    public void setStationDepartureName(String stationDeparture) { this.stationDepartureName = stationDeparture; }

    public String getStationArrivalName() { return stationArrivalName; }
    public void setStationArrival(String stationArrival) { this.stationArrivalName = stationArrival; }

    public Integer getPrice() {
        return price;
    }
    public void setPrice(Integer price) {
        this.price = price;
    }
}


Lo que yo hago no es tan?

xez 2018-10-28 13:02:58

Esto ya no es gracioso. Comienza, por último, utilizar lombok. https://projectlombok.org/


xez 2018-10-28 13:03:51

Que contiene

String json = getResultResponse(Utils.URL_SCHEDULE_BY_ID + 147);
?


xez 2018-10-28 13:04:50

Jackson es realmente necesario? Spring mismo todo esto sabe.


Total answers: 1

Djaler 2018-10-28 13:06:54

public void setTrainName(String train) { this.trainName = trainName; }

Preguntas relacionadas:

Footer decor

© www.state-it.info | Base of answers to questions for developers and programmers.