简体   繁体   中英

Response of REST API with the logs generated during execution of the Request

I am writing some REST apis with swagger-ui . Now during the execution process of this apis I am performing some operation, Which I need to send as a response of the API. Consider the following response as an example:

{
  "Database": [
    "Table 1 created",
    "data 1 inserted",
    "Data 3 insertion failed"
  ],
   "Kafka": [
    "Topic 1 created",
    "Topic 3 deleted",
    "Topic 4  rebalanced"
  ]
}

So is there any framework for this, or I need to manually create the JSON object and send it as a response.

I suppose you are using Spring MVC?

First: create a class for api response.

public class Data {
  private List<String> database = new ArrayList();
  private List<String> kafka = new ArrayList();

  public List<String> getDatabase() {
    return database;
  }

  public void setDatabase(List<String> database) {
    this.database = database;
  }

  public List<String> getKafka() {
    return kafka;
  }

  public void setKafka(List<String> kafka) {
    this.kafka = kafka;
  }
}

Second: use @ResponseBody annotation against the controller's method. This will make spring understand that method return value should be bound to the web response body.

@ResponseBody
public Data apiMethod() {
  return new Data();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM