我的问题:我不会返回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"]]
谢谢!
答案 0 :(得分:0)
您可以在实体中使用带@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;
}
...
答案 1 :(得分:-1)
请问您的pom.xml
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.6.3</version>
</dependency>
另外,您可以获得有关Spring引导如何在Spring引导网站上将Java对象处理为JSON的更多信息:https://spring.io/guides/gs/rest-service/
Greeting对象必须转换为JSON。感谢Spring的HTTP 消息转换器支持,您不需要执行此转换 手动。因为Jackson 2在类路径上,所以是Spring 系统会自动选择
MappingJackson2HttpMessageConverter
进行转换 JSON的Greeting实例。
答案 2 :(得分:-1)
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