Spring @RestController Get Request Content-Type to response json or html

不打扰是莪最后的温柔 提交于 2019-12-02 07:50:58
Nikolay Rusev

Content-Type is a request header and you can get with the following code:

    @RequestMapping("/display")
    public void display(@RequestHeader("Content-Type") String contentType)  {}

see spring's @RequestHeader docs

You don't need to do this manually. The thing that you need is Content negotiation. Which returns response type that will fit in your needs. See this post

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