I've a controller in a Spring App with this Code:
@RequestMapping("/")
@ResponseBody
public ResponseObject index(){
System.out.println(this.sf);
Session session = sf.openSession();
List<Project> projects = session.createQuery("from Project").list();
session.close();
System.out.println(projects);
return new ResponseObject(projects);
}
I've tried to return List<Project>
and also my new created Object ResponseObject
with this code:
public class ResponseObject {
protected Object data;
protected Object error;
public ResponseObject(Object data) {
this.data = data;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Object getError() {
return error;
}
public void setError(Object error) {
this.error = error;
}
}
But in all cases my JSON Reponse looks like: {"data":[{}],"error":null}
.
Projects
output is : [Project{id=1, name='Digital project'}]
Can anybody tell me why my List is not included in the JSON? Thanks!
protected Object data;
改成
protected List<Object> data;
You need to return a ResponseEntity like :
return new ResponseEntity<List<Project>>(projects, HttpStatus.OK);
Provided you have Jackson in the classpath, which if you're using Spring boot you do everything will be de-serialized correctly. You do not need the ResponseObject class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.