简体   繁体   English

在 Spring Boot 中通过 RESTful Web 服务生成 JSON?

[英]Produce JSON by RESTful web service in Spring Boot?

My problem: I don't returns Json but an array.我的问题:我不返回 Json 而是一个数组。

So, I will wish Json return:所以,我希望 Json 回归:

My repository interface:我的存储库界面:

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

My method:我的方法:

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

        return s;

    }

My Rest Service:我的休息服务:

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

My Folder class我的文件夹类

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...**

}  

The current return:目前的回报:

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

Thanks!谢谢!

You can use a constructor annotated with @JsonCreator in you entity:您可以在实体中使用带有 @JsonCreator 注释的构造函数:

Ex

...
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;

    }

...

Could you please check you have the following dependency in your pom.xml ?您能否检查一下您的pom.xml是否有以下依赖项?

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

Also you can have more information about how Spring boot handles Java object to JSON on spring boot website : https://spring.io/guides/gs/rest-service/您还可以在 Spring Boot 网站上了解有关 Spring boot 如何将 Java 对象处理为 JSON 的更多信息: https : //spring.io/guides/gs/rest-service/

The Greeting object must be converted to JSON. Greeting 对象必须转换为 JSON。 Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually.由于 Spring 的 HTTP 消息转换器支持,您无需手动进行此转换。 Because Jackson 2 is on the classpath, Spring's MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to JSON.因为Jackson 2在类路径上, MappingJackson2HttpMessageConverter会自动选择 Spring 的MappingJackson2HttpMessageConverter将 Greeting 实例转换为 JSON。

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);
}

In class level add this annotation :在类级别添加此注释:

@RestController

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM