我无法从弹簧靴中控制器的标头中拿出一个值

发布于 2025-01-24 07:03:39 字数 1019 浏览 3 评论 0原文

我已经在Spring Boot中构建了一个具有表单的Web应用程序,除了从其从标头用户名中插入的表单中插入的表单中的值。这是控制器中的代码:

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

我遇到的错误是:已解决[org.springframework.web.bind.missingrequestheaderexception:不存在参数类型字符串的必需请求标题'iv-user'不存在]代码>

问题可能是什么?已经在JSF中构建了一个应用程序,可以使用户从标题中带走,我必须用Spring应用程序替换它。我在做什么错?谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多像笑话 2025-01-31 07:03:39

缺少requestheaderexception表示HTTP请求不包含“ IV-user”标头。您必须先看看您的请求。您可以通过以下代码段来阅读HTTP请求的所有标题:

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

MissingRequestHeaderException means the HTTP request doesn't contain a "iv-user" header. You must have a look to your request first. You can read all headers of the HTTP 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);
}
无法回应 2025-01-31 07:03:39

需要“ iv-user”请求标题,但似乎在收到的请求中似乎不存在。您可以修复您的请求或使标题可选:@requestheader(value =“ iv-user”,必需= false)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文