简体   繁体   中英

Produce JSON by RESTful web service in Spring Boot?

My problem: I don't returns Json but an array.

So, I will wish Json return:

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:

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 ?

<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/

The Greeting object must be converted to JSON. Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually. Because Jackson 2 is on the classpath, Spring's MappingJackson2HttpMessageConverter is automatically chosen to convert the Greeting instance to 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

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.

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