将 JSON 响应包装在动态包裹中 – REST API

huangapple go评论82阅读模式
英文:

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 &quot;DATA&quot; and that should be dynamic like below.

```java
if(dynamic == true)
    // envelop the student information in &quot;DATA&quot; and send student info to client.
else 
   // Do not envelop student data and send student info to client.

Current JSON format of the student.

{
   &quot;id&quot;: &quot;1-abcd&quot;,
   &quot;name&quot;: &quot;stack&quot;,
   &quot;address&quot;: &quot;Pune&quot;
}

Expected json format.

{
   &quot;data&quot;: {
         &quot;id&quot;: &quot;1-abcd&quot;,
         &quot;name&quot;: &quot;stack&quot;,
         &quot;address&quot;: &quot;Pune&quot;
	 }
}

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&lt;?&gt; getStudent(...){
    ...
    if(dynamic){
       return ResponseEntity.status(HttpStatus.OK)
                            .body(Collections.singletonMap(&quot;data&quot;, 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(&quot;1-abcd&quot;, &quot;stack&quot;, &quot;Pune&quot;);

  if (dynamic) {
      Envelope envelope = new Envelope(s);
      return mapper.writeValueAsString(envelope)
  }

  return mapper.writeValueAsString(student)

Output <br>
Wrapped:

{
    &quot;data&quot;: {
        &quot;id&quot;: &quot;1-abcd&quot;,
        &quot;name&quot;: &quot;stack&quot;,
        &quot;address&quot;: &quot;pune&quot;
    }
}

Unwrapped:

{
    &quot;id&quot;: &quot;1-abcd&quot;,
    &quot;name&quot;: &quot;stack&quot;,
    &quot;address&quot;: &quot;pune&quot;
}

huangapple
  • 本文由 发表于 2020年8月18日 12:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63461905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定