我试图将一个对象列表返回到/data
端点。下面是其余控制器的代码:
@RestController
public class ApiController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "<h1>Welcome to Spring REST API</h1>";
}
@RequestMapping(value = "/data", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody List<UserData> data() {
return new UserService().getData();
}
}
服务代码:
public class UserService {
private List<UserData> data;
public UserService() {
this.data = new ArrayList<UserData>();
this.data.add(new UserData(1, "Leanne Graham", "bret", "[email protected]", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "[email protected]", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "[email protected]", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "[email protected]", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "[email protected]", "Keebler LLC",
"demarco.info", "1-344-736-8031 x564"));
}
/**
* @return the data
*/
public List<UserData> getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(List<UserData> data) {
this.data = data;
}
}
用户数据代码:
public class UserData {
int id;
String name, username, email, company, website, phone;
public UserData(int id, String name, String username, String email, String company, String website, String phone) {
this.id = id;
this.name = name;
this.username = username;
this.email = email;
this.company = company;
this.website = website;
this.phone = phone;
}
}
UserData类用于表示要在列表中传递的各个数据。
抛出以下错误和异常:
servlet [dispatcherServlet]的Servlet.service()与path []的上下文引发了异常[请求处理失败;嵌套异常是org.springframework.http.converter.HttpMessageConversionException:类型定义错误:[simple type,class com.example.springrest.UserData];嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException:找不到类com.example.springrest.UserData的序列化程序,并且没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:java .util.ArrayList [0])]有根本原因
试试这个
控制器代码
@RestController
public class ApiController {
@AutoWired
UserService userservice;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home() {
return "<h1>Welcome to Spring REST API</h1>";
}
@RequestMapping(value = "/data", method = RequestMethod.GET, produces = produces = MediaType.APPLICATION_JSON_VALUE)
public List<UserData> data() {
return new userservice.getData();
}
}
服务代码
@Service
public class UserService {
private List<UserData> data;
public UserService() {
this.data = new ArrayList<UserData>();
this.data.add(new UserData(1, "Leanne Graham", "bret", "[email protected]", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "[email protected]", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "[email protected]", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "[email protected]", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "[email protected]", "Keebler LLC",
"demarco.info", "1-344-736-8031 x564"));
}
/**
* @return the data
*/
public List<UserData> getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(List<UserData> data) {
this.data = data;
}
}