如何创建一个REST API,该API接受请求的嵌套API

时间:2019-09-23 04:48:22

标签: json spring rest api spring-boot

我正在创建春季靴以及运动衫中的rest api。我需要根据请求传递嵌套的JSON结构。我不知道该怎么做。

嵌套结构在下面,

    {
"student": "",
"groupId": "a1",
"standard": "Fifth",
"isPassed": true,
"section": "A",
"data": {
    "name": "Abcd",
    "age": "11"
},
"additional": {
    "Personal": {
        "1": {
            "address": {
                "Home": [
                    "xys"
                ],
                "Permanent": [
                    "xyz"
                ],
                "Language": [
                    "English",
                    "French"
                ]
            },
            "street": "5",
            "Mother": null,
            "Father": null
        }
    }
    "state": "xyz",
    "Sibblings": true
}

}

我希望其余的电话接受此结构。

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的操作,首先创建请求dto,它将json映射到DTO

import java.util.List;


public class RequestDTO {
  private String student;
  private String groupId;
  private String standard;
  private Boolean isPassed;
  private String section;
  private UserData data;
  private Additional additional;


  public String getStudent() {
    return student;
  }

  public void setStudent(String student) {
    this.student = student;
  }

  public String getGroupId() {
    return groupId;
  }

  public void setGroupId(String groupId) {
    this.groupId = groupId;
  }

  public String getStandard() {
    return standard;
  }

  public void setStandard(String standard) {
    this.standard = standard;
  }

  public Boolean getPassed() {
    return isPassed;
  }

  public void setPassed(Boolean passed) {
    isPassed = passed;
  }

  public String getSection() {
    return section;
  }

  public void setSection(String section) {
    this.section = section;
  }

  public UserData getData() {
    return data;
  }

  public void setData(UserData data) {
    this.data = data;
  }

  public Additional getAdditional() {
    return additional;
  }

  public void setAdditional(Additional additional) {
    this.additional = additional;
  }
}

class UserData {
  private String name;
  private Integer age;

  public String getName() {
    return name;
  }

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

  private Personal personal;
  private String state;
  private Boolean Sibblings;

  public Personal getPersonal() {
    return personal;
  }

  public void setPersonal(Personal personal) {
    this.personal = personal;
  }
}

class Personal{

  private Address address;

  public Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }
}

class Address{

  private List<String> Home;
  private List<String> Permanent;
  private List<Language> Language;

  public List<String> getHome() {
    return Home;
  }

  public void setHome(List<String> home) {
    Home = home;
  }
}

enum Language{
  English,French;
}

第二个创建将接受此请求的API处理程序

@RestController
@RequestMapping("/api")
public class AdminController {
    @RequestMapping(value = "/test", method = RequestMethod.POST)
      public RequestDTO postData(@RequestBody RequestDTO requestDTO) {
        logger.info("Body---->", requestDTO);
        return requestDTO;
      }
}

您可以通过下面的卷曲进行测试

curl -X POST \
  http://localhost:<PORT>/<context>/api/test \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 7b66a9cf-8b69-4555-9bb2-1c186bff368d' \
  -H 'cache-control: no-cache' \
  -d '{
    "student": "",
    "groupId": "a1",
    "standard": "Fifth",
    "isPassed": true,
    "section": "A",
    "data": {
        "name": "Abcd",
        "age": "11"
    },
    "additional": {
        "Personal": {
            "address": {
                "Home": [
                    "xys"
                ],
                "Permanent": [
                    "xyz"
                ],
                "Language": [
                    "English",
                    "French"
                ]
            },
            "street": "5",
            "Mother": null,
            "Father": null
        },
        "state": "xyz",
        "Sibblings": true
    }
}'

我建议您通过官方docs进行深入了解

答案 1 :(得分:0)

class Data
{
    private String name;
    private Integer age;
}

class Student{

private String student;
private String groupId;
private String standard;
private Boolean isPassed;
private String section;
private Data data;
private Additional additional;
}

这里是一些入门。进一步的嵌套必须以类似的方式完成