Home > Enterprise >  Using Object class as wrapper for input in spring boot RestController
Using Object class as wrapper for input in spring boot RestController

Time:03-10

I am bit new to spring boot and I am trying to design a search on user history which will provide 3 attributes to search user history {userId, searchKey, SearchValue}.

The search value datatype may differ based on search.

E.g

  1. Userid=100, SearchKey=userAddress, searchValue='10 Downing Street'
  2. Userid=100, SearchKey=external, searchValue=true
  3. Userid=100, SearchKey=companyId, searchValue=25

I am trying to design a rest endpoint as below. This endpoint will integrate with react front end.

@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);
}

I have implemented a dynamodb search on userhistory object which takes input as generic searchValue object as search filter as below. Dynamo DB Querying - enter image description here

Also it shows below error as -

'TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body. '

I am not able to make this work? Appreciate your insights on this.

CodePudding user response:

It's HTTP protocol. You cannot pass any body object with the Get method. You have to use Post or Put method for using a body in HTTP request.

CodePudding user response:

@RequestBody not for single value it is intended for your custom object that is used with POST or PUT but in you case you can @RequestParam also if @RequestParam take attribute required with boolean vlue which tell your endpoint caller which params is optional if you set it False and which is required if you set it True

  • Related