RequestHeader

RequestHeader注解是帮助开发者方便获取客户端的Header值的。

示例

在不使用RequestHeader注解的情况下,我们是这样获取Header值的:

@RestController
class UserController {

    @GetMapping(path = "/get")
    String info(HttpRequest request) {
        String header = request.getHeader("location");
        ...
    }
}

在使用了RequestHeader注解时:

@RestController
class UserController {

    @GetMapping(path = "/get")
    String info(@RequestHeader("location") String location) {
        ...
    }
}

RequestHeader注解的参数默认是必须的,如果此参数为空,则会抛出HeaderMissingException异常,异常处理请参考ExceptionResolver

如果开发者想让RequestHeader注解的参数为非必填参数:

@RestController
class UserController {

    @GetMapping(path = "/get")
    String info(@RequestHeader(name = "account", required = false) String header) {
        ...
    }
}

results matching ""

    No results matching ""