英文:
Wrapping json response in dynamic envelop - Rest api
问题
我有一个简单的 Spring Boot 应用程序,其中有一个 Rest API。我想通过调用客户端 Rest Web 服务来发送学生数据(以 JSON 格式)。但我想用 "DATA" 包装这个学生信息,应该像下面这样是动态的。
```java
if (dynamic == true)
// 将学生信息包装在 "DATA" 中并将学生信息发送给客户端。
else
// 不要包装学生数据,将学生信息发送给客户端。
当前学生的 JSON 格式。
{
"id": "1-abcd",
"name": "stack",
"address": "Pune"
}
期望的 JSON 格式。
{
"data": {
"id": "1-abcd",
"name": "stack",
"address": "Pune"
}
}
学生类如下所示:
class Student {
String id;
String name;
String address;
}
<details>
<summary>英文:</summary>
I have a simple spring-boot app with one Rest API. I want to send Student data (in JSON format)by consuming client rest web service.
But I want to envelop this student info with "DATA" and that should be dynamic like below.
```java
if(dynamic == true)
// envelop the student information in "DATA" and send student info to client.
else
// Do not envelop student data and send student info to client.
Current JSON format of the student.
{
"id": "1-abcd",
"name": "stack",
"address": "Pune"
}
Expected json format.
{
"data": {
"id": "1-abcd",
"name": "stack",
"address": "Pune"
}
}
Student class is as below:
Class Student{
String id;
String name;
String address;
}
答案1
得分: 1
将学生对象放入一个映射中,如果是动态的则返回映射,否则返回学生对象。
public ResponseEntity<?> getStudent(...) {
...
if (dynamic) {
return ResponseEntity.status(HttpStatus.OK)
.body(Collections.singletonMap("data", studentObj));
} else {
return ResponseEntity.status(HttpStatus.OK).body(studentObj);
}
}
英文:
Wrap the student object in a map if it's dynamic and return else return student object
public ResponseEntity<?> getStudent(...){
...
if(dynamic){
return ResponseEntity.status(HttpStatus.OK)
.body(Collections.singletonMap("data", studentObj));
} else {
return ResponseEntity.status(HttpStatus.OK).body(studentObj);
}
}
答案2
得分: 1
你可以有一个名为 Envelope
的类,类似于:
public class Envelope {
Student data;
public Student getData() {
return data;
}
public void setData(Student data) {
this.data = data;
}
public Envelope(Student data) {
super();
this.data = data;
}
}
在你的 if-else
逻辑中:
// 使用 Jackson 库:import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
Student student = new Student("1-abcd", "stack", "Pune");
if (dynamic) {
Envelope envelope = new Envelope(student);
return mapper.writeValueAsString(envelope);
}
return mapper.writeValueAsString(student);
输出 <br>
封装:
{
"data": {
"id": "1-abcd",
"name": "stack",
"address": "pune"
}
}
非封装:
{
"id": "1-abcd",
"name": "stack",
"address": "pune"
}
英文:
You could have an Envelope
class like:
public class Envelope {
Student data;
public Student getData() {
return data;
}
public void setData(Student data) {
this.data = data;
}
public Envelope(Student data) {
super();
this.data = data;
}
}
And your logic in the if-else
:
// using Jackson library: import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
Student student = new Student("1-abcd", "stack", "Pune");
if (dynamic) {
Envelope envelope = new Envelope(s);
return mapper.writeValueAsString(envelope)
}
return mapper.writeValueAsString(student)
Output <br>
Wrapped:
{
"data": {
"id": "1-abcd",
"name": "stack",
"address": "pune"
}
}
Unwrapped:
{
"id": "1-abcd",
"name": "stack",
"address": "pune"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论