Spring rest - custom header validation on method l

2019-08-11 08:17发布

I am working on spring based rest service and looking for the wy to validate custom request header for some methods and not the others. Could You advice what is the best way to achieve this please? Is it possible to achieve it e.g. via custom annotations?:

@RequestMapping(value = "/url", method = RequestMethod.GET)
@ResponseBody
@validateHeader(name=headerName)  <--??
public DTO method() {
        ...
        return DTO;
}

1条回答
欢心
2楼-- · 2019-08-11 08:40

It's a little unclear what you are asking. If you're looking for your handler method to only handle requests that contain a specific header, you can use the @RequestMapping for that as well.

RequestMapping has a headers attribute where you can specify

The headers of the mapped request, narrowing the primary mapping.

Using it like this

@RequestMapping(value = "/url", method = RequestMethod.GET, headers = {"my-header"})

You can also specify that each header have a specific value (from the javadoc)

@RequestMapping(value = "/something", headers = "content-type=text")

The javadoc is very descriptive, you can also specify if you want your handler to handle requests that don't contain a specific header. Go through the javadoc.

查看更多
登录 后发表回答