Home > Net >  how to read header using @Request header in spring-boot and passing the values to other funtions
how to read header using @Request header in spring-boot and passing the values to other funtions

Time:10-12

i want to read header using @Request herader. below is my controller class. I used the map for reading my header, now i want to check if my header contains the any of these three ids

external id or client id or user ID merchant Id

here is my controller class i was using contains method to check if header contains specific key or not, but it returns null, same with when i used get method to print the value of specific key.

@RestController
public class TransactionController {
    @Autowired
    TransactoinService transactionService;

    @PostMapping
    TransactionResponse  createTransaction(@RequestBody TransactionRequest transactionRequest){
        return transactionService.createTransaction(transactionRequest);
    }
    @GetMapping(value="/transactionDetails")
    TransactionResponse getTransactionDetailsByExternalId(@RequestHeader Map<String,String> header){
         System.out.println(header);
         return transactionService.getTransactionDetailsByExternalId(header.get("externalId"));
    }
}

when i run above code in postman this getmapping is not passing the value into the function, even though when i try to print the header its printing the value of externalId into header. here is the output of above code

{externalid=b5eccae2-b6c1-4d17-987e-1849542df2a3, user-agent=PostmanRuntime/7.29.2, accept=*/*, cache-control=no-cache, postman-token=07f367c4-e875-40ef-ab9b-5fd6d783d307, host=localhost:8080, accept-encoding=gzip, deflate, br, connection=keep-alive}
Hibernate: SELECT * FROM transaction_entity  WHERE transaction_entity.external_Id = ?

here it is image of postman status when i send the vaulues using headers

i'm haveing above issue earlier too, but then i tried getmapping with httpservletRequest, then i find my other code have no problem because everything is working fine. now i'm again trying to implement the getmapping with reqestheader becuase @request header is good to use when try to read header.

CodePudding user response:

For reading header values with @RequesHeader you have to provide header name as annotation value. For details you can look here

 @GetMapping(value="/transactionDetails")
  TransactionResponse getTransactionDetailsByExternalId(@RequestHeader("externalid") String externalid){
     System.out.println(externalid);
     return transactionService.getTransactionDetailsByExternalId(externalid);
    }

Or you can pass HttpEntity as method parameter and read headers value from it. More details can be found here

 @GetMapping(value="/transactionDetails")
  TransactionResponse getTransactionDetailsByExternalId(HttpEntity<byte[]> requestEntity){
     System.out.println(requestEntity.getHeaders());
     return transactionService.getTransactionDetailsByExternalId(requestEntity.getHeaders().getFirst("externalId"));
    }
  • Related