如何格式化rest api生成的json输出

时间:2015-02-03 15:24:57

标签: java json rest spring-boot

我正在进行春季启动,这是一个休息应用程序。每当我打电话给" localhost:8080 / projects"时,我会得到下面给出的json数据。

{
uid: "166"
    code: "99991"
        name: "iPOC AG Mednet - BIOC"
            protocol: "iPOC AG Mednet - BIOC"
                client: "BioClinica"
                    description: "AG Mednet - BIOC PoC "
                        platform: "BioPACS"
                            subjectConfiguration: {
    displayInitials: true
    displayDOB: true
    displayGender: true
}
uploadConfiguration: {
    hasAttachments: true
    hasNonDicom: false
    autoInserted: true
}}

但我需要以下面给出的格式:

{
"_embedded" : {
"projects" : [ {
    "uid" : "1",
    "code" : "123",
    "name" : "Project 1",
    "protocol" : "Protocol 1",
    "client" : "Client",
    "description" : "",
    "platform": "BioPACS",
    "subjectConfiguration" : {
    "displayInitials" : true,
    "displayDOB" : true,
    "displayGender" : true
},
"uploadConfiguration" : {
    hasAttachments: true,
    hasNonDicom: true,
    isAutoInserted: false,
},
"_links" : {
    "self" : {
    "href" : "http://localhost:8080/projects/123"
}
}
}]}}

我的"项目"分类如下。

public class Project {

private String uid;
private String code;
private String name;
private String protocol;
private String client;
private String description;
private String platform;
private SubjectConfiguration subjectConfiguration;
private UploadConfiguration uploadConfiguration;

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getProtocol() {
    return protocol;
}

public void setProtocol(String protocol) {
    this.protocol = protocol;
}

public String getClient() {
    return client;
}

public void setClient(String client) {
    this.client = client;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getPlatform() {
    return platform;
}

public void setPlatform(String platform) {
    this.platform = platform;
}

public SubjectConfiguration getSubjectConfiguration() {
    return subjectConfiguration;
}

public void setSubjectConfiguration(
        SubjectConfiguration subjectConfiguration) {
    this.subjectConfiguration = subjectConfiguration;
}

public UploadConfiguration getUploadConfiguration() {
    return uploadConfiguration;
}

public void setUploadConfiguration(UploadConfiguration uploadConfiguration) {
    this.uploadConfiguration = uploadConfiguration;
}

@Override
public String toString() {
    return "'uid':'" + uid + "', " + "'code':'" + code + "', "
            + "'name':'" + name + "', " + "'protocol':'" + protocol + "', "
            + "'client':'" + "', " + "'description':'" + description + "', "
            + "'platform':'" + platform + "', " + "'subjectConfiguration':'"
            + subjectConfiguration + "', " + "'uploadConfiguration':'"
            + uploadConfiguration + "'" ;
}}

返回List的方法如下:

@RequestMapping(value = "/projects", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Project> getProject() {
    GetAuthorizedWebSendTransferProjects request = new GetAuthorizedWebSendTransferProjects();
    GetAuthorizedWebSendTransferProjectsResponse response = gw.getResponse(request);
    JAXBElement<ArrayOfProjectContainer> arr = response.getGetAuthorizedWebSendTransferProjectsResult();
    ArrayOfProjectContainer arr1 = arr.getValue();
    List<ProjectContainer> arr2 = arr1.getProjectContainer();
    List<Project> projects = getPopulatedProjectList(arr2);
    return projects;
}

我是否需要添加任何库来实现此目的,或者我应该修改Project类的toString()方法。现在响应头有&#34; Content-Type:application / json&#34;但我需要的标题是&#34; Content-Type:application / hal + json&#34;。任何人都可以帮我解决这个问题。先谢谢你。

1 个答案:

答案 0 :(得分:3)

我建议使用Jackson转换您从调用&#34; localhost:8080 / projects&#34;到JsonNode对象。然后,您可以根据需要操作JsonNode对象,并使用该数据填充Project类。