Spring- 返回对象列表的请求方法显示错误
Spring- Request method returning a list of objects is showing error
我正在尝试 return 一个对象列表到 /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", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "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;
}
}
UserDataclass用于表示要在列表中传递的单个数据。
抛出以下错误和异常:
Servlet.service() for servlet [dispatcherServlet] in context with path
[] threw exception [Request processing failed; nested exception is
org.springframework.http.converter.HttpMessageConversionException:
Type definition error: [simple type, class
com.example.springrest.UserData]; nested exception is
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No
serializer found for class com.example.springrest.UserData and no
properties discovered to create BeanSerializer (to avoid exception,
disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference
chain: java.util.ArrayList[0])] with root cause
试试这个
控制器代码
@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", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "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;
}
}
我正在尝试 return 一个对象列表到 /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", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "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;
}
}
UserDataclass用于表示要在列表中传递的单个数据。
抛出以下错误和异常:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.example.springrest.UserData]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.example.springrest.UserData and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])] with root cause
试试这个
控制器代码
@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", "Sincere@april.biz", "Romaguera-Crona", "hildegard.org",
"1-770-736-8031 x56442"));
this.data.add(new UserData(2, "Ervin Howell", "Antonette", "Shanna@melissa.tv", "Deckow-Crist", "anastasia.net",
"010-692-6593 x09125"));
this.data.add(new UserData(3, "Clementine Bauch", "Samantha", "Nathan@yesenia.net", "Romaguera-Jacobson",
"ramiro.info", "1-463-123-4447"));
this.data.add(new UserData(4, "Patricia Lebsack", "Karianne", "Julianne.OConner@kory.org", "Robel-Corkery",
"kale.biz", "1-770-736-8037"));
this.data.add(new UserData(5, "Chelsey Dietrich", "Kamren", "Lucio_Hettinger@annie.ca", "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;
}
}