Hecklers in Development
Code, coffee, & camaraderie. Collection, unordered. ;)

Just a quick tip for the Spring fans out there…

 

I was leading a workshop yesterday and this question arose: Is it possible to filter requests by header content using Spring’s various request mapping (@RequestMapping, @GetMapping, @PostMapping, et al) annotations?

 

Not only is it possible, it’s easy, and the implementation is concise & clean! Let’s take a look.

 

The code

Here is a simple example that filters based upon the content-type of the header:

 

 

To test this, simply visit the Spring Initializr, create a simple Spring Boot app with a single dependency (Web), download & unzip it, and add the code above using your favorite IDE. Then build & run it (of course)! 😉

 

The output

Using httpie (which is quickly becoming my favorite CLI HTTP client), we can verify our results:

 

Request content type of text/plain

 

Request content type of application/json

 

NOTE: The above examples use httpie “shorthand”, which assumes localhost when the base URL is omitted.

 

That’s all for now, but stay tuned (or follow me on Twitter) for more quick (or more lengthy) Spring tips!

 

Cheers,
Mark

 

Click here for additional information

 

Share

Related Posts:


Tags: , , , , , , , ,

Powered by Wordpress
Theme © 2005 - 2009 FrederikM.de, heavily modified by Mark Heckler
BlueMod is a modification of the blueblog_DE Theme by Oliver Wunder