I have built a web application in Spring Boot that has a form and besides the values from the form that it inserts in the database it takes from the header the username. This is the code from the Controller:
@PostMapping("/BilantErr/")
public String postIndex(@RequestParam(name="cui") String cui, @RequestParam(name="an_raportare") String anRaportare, @RequestParam(name="perioada") String perioada,
@RequestHeader("iv-user") String operator,@RequestParam(name="motivatie") String motivatie, Model model) {
String page = "";
//String operator = "serban";
try {
if(repository.insert(cui,anRaportare,operator,motivatie,perioada) == true) {
page = "success";
} else {
page = "error";
}
}catch(Exception e) {Logger logger = LoggerFactory.getLogger(BilantErr.class);}
return page;
}
The error that I am getting is : Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'iv-user' for method parameter type String is not present]
What may be the problem ? There is an application already built in JSF that works and takes the user from the header and I have to replace it with a Spring app. What am I doing wrong ? Thanks
CodePudding user response:
The request header "iv-user" is required but does not seem to be present in the request you receive. You could fix your request or make the header optional: @RequestHeader(value = "iv-user", required = false)
CodePudding user response:
MissingRequestHeaderException
means the request doesn't contain a "iv-user" in header. You must have a look to your header first. You can read all headers of the request by following code snippet:
@GetMapping("/listHeaders")
public ResponseEntity<String> multiValue(
@RequestHeader MultiValueMap<String, String> headers) {
headers.forEach((key, value) -> {
LOG.info(String.format(
"Header '%s' = %s", key, value.stream().collect(Collectors.joining("|"))));
});
return new ResponseEntity<String>(
String.format("Listed %d headers", headers.size()), HttpStatus.OK);
}