如何通过 ResponseEntity 发送数据库 ID?
How to send a database ID via a ResponseEntity?
我正在为我的后端使用 Spring 启动,它将 ResponseEntity 主体中的 JSON 发送到 Vue.js 前端。
JSON 也位于 MySQL 数据库中,并从中获得一个 ID。此 ID 在生成后提供给 JSON,因此我无法更改我的 JSON 并将我的 ID 包含在其中。
因此,我需要通过其他方式将此文档 ID 包含在 ResponseEntity 中(用于通过不同的 REST 端点向文档提供下载-link)。
这就是我现在 return 我的 ResponseEntity 的方式:
public static ResponseEntity<String> getOutput(DocumentFile outputFile, String format)
throws IOException {
Path path = Paths.get(outputFile.getAbsolutePath());
String content = new String(Files.readAllBytes(path));
int documentID = outputFile.getId() + 1;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(getMediaTypeFromFormat(format));
ResponseEntity<String> responseString = ResponseEntity.ok().headers(headers).body(content);
return responseString;
}
documentID
是我要与 JSON 一起发送的 ID。
我能否以某种方式将此 ID 包含在页眉、正文或其他地方?我怎样才能做到这一点?
能否分享一下您的 JSON object 的结构?
有多种方法可以解决这个问题。
a) return a header中的ID值,如:
public static ResponseEntity<String> getOutput(DocumentFile outputFile, String format)
throws IOException {
Path path = Paths.get(outputFile.getAbsolutePath());
String content = new String(Files.readAllBytes(path));
int documentID = outputFile.getId() + 1;
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "X-Custom-ID");
headers.add("X-Custom-ID", String.valueOf(documentID));
headers.setContentType(getMediaTypeFromFormat(format));
ResponseEntity<String> responseString = ResponseEntity.ok().headers(headers).body(content);
return responseString;
}
b) 创建自定义 return object,例如:
class CustomResponse {
String id; // would be the id
String value; // would be the json
}
c)
This ID is given to the JSON after it was generated, so I cannot
change my JSON and include my ID in it.
是的,你可以。将您的 JSON 映射到域 object(使用 ObjectMapper),并向其添加 ID。
我正在为我的后端使用 Spring 启动,它将 ResponseEntity 主体中的 JSON 发送到 Vue.js 前端。
JSON 也位于 MySQL 数据库中,并从中获得一个 ID。此 ID 在生成后提供给 JSON,因此我无法更改我的 JSON 并将我的 ID 包含在其中。
因此,我需要通过其他方式将此文档 ID 包含在 ResponseEntity 中(用于通过不同的 REST 端点向文档提供下载-link)。
这就是我现在 return 我的 ResponseEntity 的方式:
public static ResponseEntity<String> getOutput(DocumentFile outputFile, String format)
throws IOException {
Path path = Paths.get(outputFile.getAbsolutePath());
String content = new String(Files.readAllBytes(path));
int documentID = outputFile.getId() + 1;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(getMediaTypeFromFormat(format));
ResponseEntity<String> responseString = ResponseEntity.ok().headers(headers).body(content);
return responseString;
}
documentID
是我要与 JSON 一起发送的 ID。
我能否以某种方式将此 ID 包含在页眉、正文或其他地方?我怎样才能做到这一点?
能否分享一下您的 JSON object 的结构?
有多种方法可以解决这个问题。
a) return a header中的ID值,如:
public static ResponseEntity<String> getOutput(DocumentFile outputFile, String format)
throws IOException {
Path path = Paths.get(outputFile.getAbsolutePath());
String content = new String(Files.readAllBytes(path));
int documentID = outputFile.getId() + 1;
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "X-Custom-ID");
headers.add("X-Custom-ID", String.valueOf(documentID));
headers.setContentType(getMediaTypeFromFormat(format));
ResponseEntity<String> responseString = ResponseEntity.ok().headers(headers).body(content);
return responseString;
}
b) 创建自定义 return object,例如:
class CustomResponse {
String id; // would be the id
String value; // would be the json
}
c)
This ID is given to the JSON after it was generated, so I cannot change my JSON and include my ID in it.
是的,你可以。将您的 JSON 映射到域 object(使用 ObjectMapper),并向其添加 ID。