I am building a Spring Boot application that utilizes the com.explore.explore.entities.Spot class. but getting this error
Type definition error: [simple type, class com.explore.explore.entities.Spot]] with root cause
I have checked the Spot class and its dependencies, but I can't seem to find the issue. How can I fix this error and properly serialize the Spot object to JSON?
I have already tried the following steps:
MyController.java
package com.explore.explore.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.explore.explore.entities.Spot;
import com.explore.explore.services.SpotService;
@RestController
public class MyController {
@Autowired
public SpotService SpotService;
@GetMapping("/hello")
public String home() {
return "hello";
}
// get spots
@GetMapping("/spots")
// public List<Integer> getSpots() {
// List<Integer> list;
// list = new ArrayList<>();
// list.add(3);
// list.add(5);
// return this.SpotService.getSpots();
// }
public List<Spot> getSpots() {
List<Spot> list;
list = new ArrayList<>();
list.add(new Spot(1, "38 Block", "Near 38"));
list.add(new Spot(0, "UNIMAll", "Near Me"));
return list;
}
}
Spot.java
package com.explore.explore.entities;
import org.springframework.http.converter.HttpMessageConversionException;
public class Spot {
private long id;
private String name;
private String location;
public Spot(long id, String name, String location) {
super();
this.id = id;
this.name = name;
this.location = location;
}
public Spot() {
super();
}
}
I needed to add annotations for serialization
@JsonAutoDetect
, @JsonProperty
, and @JsonInclude
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Spot {
@JsonProperty("id")
private long id;
@JsonProperty("name")
private String name;
@JsonProperty("location")
private String location;
public Spot(long id, String name, String location) {
super();
this.id = id;
this.name = name;
this.location = location;
}
public Spot() {
super();
}
// getters and setters here
}