Spring rest - custom header validation on method level [closed]

。_饼干妹妹 提交于 2020-01-16 00:39:32

问题


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:


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.



来源:https://stackoverflow.com/questions/20007682/spring-rest-custom-header-validation-on-method-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!