使用对象 class 作为 spring 引导 RestController 中输入的包装器
Using Object class as wrapper for input in spring boot RestController
我对 spring 引导有点陌生,我正在尝试设计对用户历史记录的搜索,它将提供 3 个属性来搜索用户历史记录 {userId, searchKey, SearchValue}。
搜索值数据类型可能因搜索而异。
E.g
- Userid=100, SearchKey=userAddress, searchValue='10 Downing Street'
- Userid=100, SearchKey=external, searchValue=true
- Userid=100, SearchKey=companyId, searchValue=25
我正在尝试设计一个休息端点,如下所示。此端点将与 React 前端集成。
@GetMapping(value = "/searchUserHistoryByKeyValue")
public ResponseEntity<Object> searchUserHistoryByKeyValue(
@RequestParam(value = "userId") int userId,
@RequestParam(value = "searchKey") String searchKey,
@RequestBody Object searchValue) {
List<org.json.simple.JSONObject> entities =
userHistoryService.searchUserHisotryByKeyValue(userId, searchKey, searchValue);
return new ResponseEntity<>(entities, HttpStatus.OK);
}
我已经在 userhistory 对象上实现了 dynamodb 搜索,它将输入作为通用 searchValue 对象作为搜索过滤器,如下所示。
Dynamo 数据库查询 - https://www.tutorialspoint.com/dynamodb/dynamodb_querying.htm
public List<JSONObject> searchUserHistoryByKeyValue(
int userId, String searchKey, Object searchValue) throws DataAccessException {
Table table = dynamoDB.getTable(userHistoryTable.getName());
Map<String, String> expressionAttributeNames =
DEFAULT_USER_FILTERS.stream()
.collect(
Collectors.toMap(attrib -> attrib, attrib -> attrib.substring(1), (a, b) -> b));
Optional<String> projectionExpression =
createProjectionExpression(
Collections.singletonList(searchKey), expressionAttributeNames);
Optional<String> filterProjectionExpression =
buildCustomProjectionExpression(
Collections.singletonList(searchKey), expressionAttributeNames);
QuerySpec querySpec =
new QuerySpec()
.withProjectionExpression(projectionExpression.orElse(StringUtils.EMPTY))
.withKeyConditionExpression("#userId = :userId")
.withFilterExpression(
String.format(
"%s = :searchValue",
filterProjectionExpression.orElseThrow(
() -> new IllegalArgumentException("Invalid Search Attributes"))))
.withNameMap(expressionAttributeNames)
.withValueMap(Map.of(":userId", userId, ":searchValue", searchValue))
.withScanIndexForward(false);
当我尝试使用 swagger 或 postman 测试此端点时,我无法传入
@RequestBody 对象搜索值。它只是显示为空括号 - {}
它还显示以下错误为 -
'TypeError: Failed to execute 'fetch' on 'Window': Request with
GET/HEAD method cannot have body. '
我无法完成这项工作?感谢您对此的见解。
它是 HTTP 协议。
您不能使用 Get 方法传递任何正文对象。您必须使用 Post 或 Put 方法才能在 HTTP 请求中使用正文。
@RequestBody
不是单个值,它适用于与 POST 或 PUT 一起使用的自定义对象,但在你的情况下,如果 @RequestParam
,你也可以 @RequestParam
采用布尔值 vlue 所需的属性,它告诉端点调用者如果将其设置为 False 则哪些参数是可选的,如果将其设置为 True
则哪些是必需的
我对 spring 引导有点陌生,我正在尝试设计对用户历史记录的搜索,它将提供 3 个属性来搜索用户历史记录 {userId, searchKey, SearchValue}。
搜索值数据类型可能因搜索而异。
E.g
- Userid=100, SearchKey=userAddress, searchValue='10 Downing Street'
- Userid=100, SearchKey=external, searchValue=true
- Userid=100, SearchKey=companyId, searchValue=25
我正在尝试设计一个休息端点,如下所示。此端点将与 React 前端集成。
@GetMapping(value = "/searchUserHistoryByKeyValue")
public ResponseEntity<Object> searchUserHistoryByKeyValue(
@RequestParam(value = "userId") int userId,
@RequestParam(value = "searchKey") String searchKey,
@RequestBody Object searchValue) {
List<org.json.simple.JSONObject> entities =
userHistoryService.searchUserHisotryByKeyValue(userId, searchKey, searchValue);
return new ResponseEntity<>(entities, HttpStatus.OK);
}
我已经在 userhistory 对象上实现了 dynamodb 搜索,它将输入作为通用 searchValue 对象作为搜索过滤器,如下所示。 Dynamo 数据库查询 - https://www.tutorialspoint.com/dynamodb/dynamodb_querying.htm
public List<JSONObject> searchUserHistoryByKeyValue(
int userId, String searchKey, Object searchValue) throws DataAccessException {
Table table = dynamoDB.getTable(userHistoryTable.getName());
Map<String, String> expressionAttributeNames =
DEFAULT_USER_FILTERS.stream()
.collect(
Collectors.toMap(attrib -> attrib, attrib -> attrib.substring(1), (a, b) -> b));
Optional<String> projectionExpression =
createProjectionExpression(
Collections.singletonList(searchKey), expressionAttributeNames);
Optional<String> filterProjectionExpression =
buildCustomProjectionExpression(
Collections.singletonList(searchKey), expressionAttributeNames);
QuerySpec querySpec =
new QuerySpec()
.withProjectionExpression(projectionExpression.orElse(StringUtils.EMPTY))
.withKeyConditionExpression("#userId = :userId")
.withFilterExpression(
String.format(
"%s = :searchValue",
filterProjectionExpression.orElseThrow(
() -> new IllegalArgumentException("Invalid Search Attributes"))))
.withNameMap(expressionAttributeNames)
.withValueMap(Map.of(":userId", userId, ":searchValue", searchValue))
.withScanIndexForward(false);
当我尝试使用 swagger 或 postman 测试此端点时,我无法传入 @RequestBody 对象搜索值。它只是显示为空括号 - {}
它还显示以下错误为 -
'TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. '
我无法完成这项工作?感谢您对此的见解。
它是 HTTP 协议。 您不能使用 Get 方法传递任何正文对象。您必须使用 Post 或 Put 方法才能在 HTTP 请求中使用正文。
@RequestBody
不是单个值,它适用于与 POST 或 PUT 一起使用的自定义对象,但在你的情况下,如果 @RequestParam
,你也可以 @RequestParam
采用布尔值 vlue 所需的属性,它告诉端点调用者如果将其设置为 False 则哪些参数是可选的,如果将其设置为 True