为什么我的json从控制器返回的字段为空?

时间:2019-01-19 14:23:54

标签: spring spring-boot

我正在IntelliJ中使用调试器,并且在返回结果之前,该数组非常好,如您所见here

但是由于某种原因,浏览器中的响应看起来像this

我不明白为什么这些字段不可见。

这是我的2个模型的样子:
市镇

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

    String name;
}

预测

@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;
}

这是我的控制器

@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;
    }
}

应用程序的其余部分(服务和持久层)非常标准。
这是什么原因呢?

2 个答案:

答案 0 :(得分:1)

您将需要模型的吸气剂和吸气剂。 Jackson模型将模型转换为JSON时需要它来访问其字段,这与将resultSet转换为模型时的JPA不同。这是代码:

预测

@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;
    }
}

市政

@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;
    }
}

答案 1 :(得分:1)

对于每个要公开的字段,您都需要getter和setter。 您可以从lombok项目中使用@Data来避免样板代码。 https://projectlombok.org/