简体   繁体   English

如何在@RestController响应中打印List()?

[英]How to print List() in @RestController Response?

I've a controller in a Spring App with this Code: 我在Spring App中有一个使用以下代码的控制器:

@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: 我试图用以下代码返回List<Project>以及我新创建的Object ResponseObject

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} . 但是在所有情况下,我的JSON Reponse都看起来像: {"data":[{}],"error":null}

Projects output is : [Project{id=1, name='Digital project'}] Projects输出为: [Project{id=1, name='Digital project'}]

Can anybody tell me why my List is not included in the JSON? 谁能告诉我为什么我的列表未包含在JSON中? Thanks! 谢谢!

protected Object data;

改成

protected List<Object> data;

You need to return a ResponseEntity like : 您需要返回一个ResponseEntity,如:

return new ResponseEntity<List<Project>>(projects, HttpStatus.OK);

see http://docs.spring.io/autorepo/docs/spring/4.0.2.RELEASE/javadoc-api/org/springframework/http/ResponseEntity.html 参见http://docs.spring.io/autorepo/docs/spring/4.0.2.RELEASE/javadoc-api/org/springframework/http/ResponseEntity.html

Provided you have Jackson in the classpath, which if you're using Spring boot you do everything will be de-serialized correctly. 如果您在类路径中有Jackson,那么如果您使用的是Spring boot,则可以正确地反序列化所有操作。 You do not need the ResponseObject class. 您不需要ResponseObject类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AJAX 从 restcontroller 打印数据? - How to print data from restcontroller using AJAX? 如何使用 Spring Boot @RestController 流式传输分块响应 - How To Stream Chunked Response With Spring Boot @RestController 如何强制RestController对CSV文件进行响应 - How to force the response of RestController to csv file 如何返回列表的json对象 <POJO> 在RestController中 - how to return json objects for list<POJO> in RestController 如何将 RabbitMQ 生产者的响应返回给 RestController? - How can I return response from RabbitMQ producer to RestController? 如何将@RestController中的请求体转换为抽象值列表? - How do I convert request body in a @RestController to a List of abstract values? Spring @RestController响应已经提交 - Spring @RestController response is already committed Spring Boot 中@RestController 的通用响应? - Generic response for @RestController in Spring Boot? 在 spring restcontroller 响应中返回 ByteArrayResource - return ByteArrayResource in spring restcontroller response 下载文件时如何在Springboot RestController响应中同时返回JSON响应和文件字节数组 - How to return both JSON response and Byte Array of file in Springboot RestController Response while downloading a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM