HttpServletRequest httpReq = (HttpServletRequest) request;
if (httpReq.getHeader("device").equals("web1")) {
chain.doFilter(request, response);
}
I want to know how can I check whether the key "device"
exists in the request header?
Note : Don't want to use getHeaderName
which returns an enumeration of all the header names this request contains and iterate through it.
getParameterMap().containsKey("device")
is not working here.
From the JavaDoc for HttpServletRequest.getHeader(String name)
:
If the request did not include a header of the specified name, this method returns
null
.
So a basic null check is sufficient:
boolean deviceHeaderExists = httpReq.getHeader("device") != null;
Reading between the lines a little, if you're looking to accept a mandatory device
header then you can code that up with annotations in your REST call. Example:
@GetMapping("/something")
public void doSomething(@RequestHeader("device") @NotNull String deviceName) {
// your logic here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With