2

I am using the debugger in IntelliJ and right before the point of returning the result, the array is perfectly fine, as you can see here

But for some reason, the response in the browser looks like this

I don't understand why the fields are invisible.

This is what my 2 models look like:
Municipality:

@Entity
public class Municipality {
    @Id
    @JsonIgnore
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    String name;
}

Prediction

@Entity
public class Prediction {
    @Id
    @JsonIgnore
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    LocalDateTime tsPredictionMade;
    LocalDateTime tsPredictionFor;
    float pm10;
    float pm25;

    @ManyToOne
    @OnDelete(action = OnDeleteAction.CASCADE)
    Municipality municipality;
}

And this is my controller:

@RestController
@RequestMapping("/predict")
public class PredictionController {

    private MunicipalityService municipalityService;
    private PredictionService predictionService;

    @Autowired
    public PredictionController(MunicipalityService municipalityService, PredictionService predictionService) {
        this.municipalityService = municipalityService;
        this.predictionService = predictionService;
    }

    @GetMapping
    public List<Municipality> getPredictions(){
        List<Municipality> result = municipalityService.getPredictions();
        return result;
    }

    @GetMapping("/{municipality}")
    public List<Prediction> getPredictionsForMunicipality(@PathVariable("municipality") String name){
        List<Prediction> result = predictionService.getPredictions(name);
        return result;
    }
}

The rest of the app (service and persistence layer) is pretty standard.
What is the reason for this?

2 Answers 2

1

You will need the getters and setters for your models. The Jackson library needs it for accessing its fields when converting the models into JSON, differently from JPA when converting the resultSet into models. Here is the code:

Prediction

@Entity
public class Municipality {
    @Id
    @JsonIgnore
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    String name;

    public Long getId() {
        return id;
    }

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

    public LocalDateTime getTsPredictionMade() {
        return tsPredictionMade;
    }

    public void setTsPredictionMade(LocalDateTime tsPredictionMade) {
        this.tsPredictionMade = tsPredictionMade;
    }

    public LocalDateTime getTsPredictionFor() {
        return tsPredictionFor;
    }

    public void setTsPredictionFor(LocalDateTime tsPredictionFor) {
        this.tsPredictionFor = tsPredictionFor;
    }

    public float getPm10() {
        return pm10;
    }

    public void setPm10(float pm10) {
        this.pm10 = pm10;
    }

    public float getPm25() {
        return pm25;
    }

    public void setPm25(float pm25) {
        this.pm25 = pm25;
    }

    public Municipality getMunicipality() {
        return municipality;
    }

    public void setMunicipality(Municipality municipality) {
        this.municipality = municipality;
    }
}

Municipality

@Entity
public class Municipality {
    @Id
    @JsonIgnore
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need getters and setter for each field that you want to expose. You can use @Data from lombok project to avoid boilerplate code. https://projectlombok.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.