【问题标题】:Produce JSON by RESTful web service in Spring Boot?在 Spring Boot 中通过 RESTful Web 服务生成 JSON?
【发布时间】:2016-10-21 17:09:50
【问题描述】:

我的问题:我不返回 Json,而是返回一个数组。

所以,我希望 Json 回归:

我的仓库界面:

public interface SuiRepository extends JpaRepository<Folder, Integer>{
@Query("...")
    public List<Folder> data();
}

我的方法:

@Override
    public List<Folder> getFolder(){
        List<Folder> s = folderRepository.data();

        return s;

    }

我的休息服务:

@RequestMapping(value="/folders", method=RequestMethod.GET, produces="application/json", consumes="application/json")
    @ResponseBody
    public  List<Folder> getFolders() {
        return iUd.getFolders();
    }

我的文件夹类

Entity
     public class Folder implements Serializable{
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int idFolder; 
        private String comments;
        @ManyToOne
        @JoinColumn(name="IdFile")
        private File file;
        @ManyToOne
        @JoinColumn(name="username")
        private User user;

     **Getters&Setters...**

}  

当前回报:

[["Ban","dee","dede@gmail.com",1,"xx","Emb"],["Cin","mis","sse@gmail.com",1,"yy","Ns"]]

谢谢!

【问题讨论】:

  • 你的文件夹类是什么样子的?
  • 您收到的是JSON,您发布的内容是100% 有效的JSON!你想要什么? Object 的数组,而不是 ArrayArray?根据您的代码和注释,看起来您甚至没有看过文档一次,您的注释根据它们所注释的方法毫无意义。
  • 你得到答案了吗?

标签: java json rest spring-boot


【解决方案1】:

您可以在您的实体中使用带有 @JsonCreator 注释的构造函数:

...
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.persistence.*;


@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    String name;
    String phone;
    String password;

    @JsonCreator
    public User(@JsonProperty("name") String name,
                @JsonProperty("phone") String phone) {

        this.name = name;
        this.phone = phone;

    }

...

【讨论】:

    【解决方案2】:
    Try this one in controller :
    
    @RequestMapping(value="/folders", method= RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Folder> getFolders()
    {
      HttpStatus httpStatus = HttpStatus.OK;
      List<Folder> listFol=iUd.getFolders();
      return new ResponseEntity<HawkeyesResponse>(listFol,httpStatus);
    }
    

    在类级别添加此注释:

    @RestController
    

    【讨论】:

      【解决方案3】:

      您能否检查一下您的pom.xml 中是否存在以下依赖项?

      <dependency>
          <groupId>com.fasterxml.jackson.dataformat</groupId>
          <artifactId>jackson-dataformat-xml</artifactId>
          <version>2.6.3</version>
      </dependency>
      

      您还可以在 Spring Boot 网站上了解有关 Spring Boot 如何将 Java 对象处理为 JSON 的更多信息:https://spring.io/guides/gs/rest-service/

      Greeting 对象必须转换为 JSON。感谢 Spring 的 HTTP 消息转换器支持,你不需要做这个转换 手动。因为Jackson 2 在类路径中,所以 Spring 的 自动选择MappingJackson2HttpMessageConverter 进行转换 到 JSON 的 Greeting 实例。

      【讨论】:

      • 我放了这个依赖,但是还是不行:(
      • 你是从哪个 spring boot 项目开始的?您能在您的请求中提供您的pom.xml 吗?此外,您应该查看以下 Spring Boot 指南:spring.io/guides/gs/accessing-data-rest
      • 为什么你认为XML 数据格式提供者与JSON 有任何关系?
      猜你喜欢
      • 2018-10-25
      • 1970-01-01
      • 2012-11-15
      • 2016-03-24
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多