S
S
sland show2018-10-28 11:38:58
Java
sland show, 2018-10-28 11:38:58

Why is the field not parsed and remains null?

I have a REST controller that pulls data from a database (entity) and wraps it in a DTO that is serialized to 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);
    }


First, I pull out the entity:
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.02018-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


Then the controller itself returns the wrapped JSON to me via 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}


So, I want to deserialize this case.

This is done through 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();
}


So, everything seemed to be ok. But no, Jackson doesn't deserialize one field trainName, I don't understand why... All other fields are correctly parsed.

Here is my DTO that I am serializing:
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();
    }
}


And here is the Schedule model that I am trying to parse with Jackson:
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;
    }
}


What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Romanov, 2018-10-28
@SlandShow

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question