'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","[email protected]",1,"xx","Emb"],["Cin","mis","[email protected]",1,"yy","Ns"]]

Thanks!



Solution 1:[1]

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;

    }

...

Solution 2:[2]

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.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 luizMello
Solution 2 Mickael
Solution 3 Sumeet Tiwari