在Spring应用程序中使用@RequestHeader时处理异常 [英] Handle Exceptions when using @RequestHeader in spring application

查看:1110
本文介绍了在Spring应用程序中使用@RequestHeader时处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用Spring框架的Java代码:

Here is my Java code that uses the Spring Framework:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String SampleFunction(@RequestHeader("Authorization") String details)
{
    System.out.println("Authorization details recieved");

}

我正在尝试访问授权标头。我想通过将丢失的 Authorization 标头重定向到400错误的请求页面来处理。我该怎么做?

I am trying to access Authorization header. I want to handle the missing Authorization header by redirecting it to a 400 Bad Request page. How can I do this?

推荐答案

默认情况下,标头是必需的。因此,如果缺少它,您将获得一个例外。
但是,请参见下面的代码。现在,如果缺少该字符串,则细节字符串将为空。

By default the header is required. So if it is missing, you will get an exception. However, see code below. Now, if it is missing, the details string will be null.

@RequestHeader(required = false, value = "Authorization") String details

这篇关于在Spring应用程序中使用@RequestHeader时处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆