我正在尝试将对象列表返回到/data
端点。
下面是rest控制器的代码:
@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;
}
}
UserData代码:
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() []引发异常[请求处理失败;嵌套异常为 org.springframework.http.converter.HttpMessageConversionException: 类型定义错误:[简单类型,类 com.example.springrest.UserData];嵌套异常为 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:否 找到类com.example.springrest.UserData的序列化器,并且没有 发现的用于创建BeanSerializer的属性(为避免出现异常, 禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用 链:具有根本原因的java.util.ArrayList [0])]
答案 0 :(得分:1)
尝试
控制器代码
@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;
}
}