java 在 Spring Boot 中通过 RESTful Web 服务生成 JSON?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37922624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Produce JSON by RESTful web service in Spring Boot?
提问by Adam D
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","[email protected]",1,"xx","Emb"],["Cin","mis","[email protected]",1,"yy","Ns"]]
Thanks!
谢谢!
回答by luizMello
You can use a constructor annotated with @JsonCreator in you entity:
您可以在实体中使用带有 @JsonCreator 注释的构造函数:
Ex
前任
...
import com.fasterxml.Hymanson.annotation.JsonCreator;
import com.fasterxml.Hymanson.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;
}
...
回答by Mickael
Could you please check you have the following dependency in your pom.xml
?
您能否检查一下您的pom.xml
?
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-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. Thanks to Spring's HTTP message converter support, you don't need to do this conversion manually. Because Hymanson 2is on the classpath, Spring's
MappingHymanson2HttpMessageConverter
is automatically chosen to convert the Greeting instance to JSON.
Greeting 对象必须转换为 JSON。由于 Spring 的 HTTP 消息转换器支持,您无需手动进行此转换。因为Hymanson 2在类路径上,所以
MappingHymanson2HttpMessageConverter
会自动选择Spring 来将 Greeting 实例转换为 JSON。
回答by Sumeet Tiwari
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