1

I want that all my rest services has as an input parameter HttpServletRequest httpRequest that I need later for some loggin purposes. This parameter sometimes is forgotten to be added and some methods are not logged. As are all rest services, and I am using Spring, all of them has some very specific annotations. I was thinking on using checkstyles to force the parameter to be present.

A little more of explanation of want I want to achieve. I am developing some rest servicies, and I am interested on logging some header that are sent to the rest services with some extra information. For this purpose, I have added HttpServletRequest request to each rest services as follows:

@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public Status get(HttpServletRequest request, @PathVariable("id") Integer id) {
       ....
}

This paremeter is correctly retrieved and I can read the headers correctly (everything automated using AspectJ). My problem now is that is for a new rest service, I forgot to add the parameter, no logs will be shown. As the parameter is optional (you can or cannot add to the rest service without any error) and all logging is automated by AspectJ, is possible that I can forget it for future rest services and no notice the miss until late.

The scope is to ensure that always is present in all my rest services. My first thought was using checkstyle as I am already using for other different purposes.

Is it possible using checkstyle or any similar tool to force that a parameter is present on any method that has an annotation? If not, there is any other different way to achive my objective?

King Midas
  • 1,442
  • 4
  • 29
  • 50
  • This seems like a job for a unit test. – VGR Oct 18 '19 at 11:38
  • This does not directly answer your question, but from your description it seems like you want to do some operation for all requests received. For that use case, when using Spring, the best is to define a Spring Filter, that will define in one single place an operation to be performed for all requests, without the need to put anything specific to that operation in the Controllers. – KevinLH Oct 18 '19 at 11:39
  • Interesting. Could you provide me more information as an answer that I can check and accept if needed? – King Midas Oct 18 '19 at 11:44
  • @KingMidas I thought your question was clear until I read your comment on an answer you would accept. Can you update your question to clarify whether you are looking for a generic solution for some arbitrary method, or are you only concerned about your specific use case? – skomisa Oct 18 '19 at 15:04
  • @skomisa I have edited the question to provide extra information. Now I guess that is easier to understand my objective. – King Midas Oct 21 '19 at 09:04
  • @KingMidas Possible duplicate of [What is the best way to check “method call” match format using checkstyle?](https://stackoverflow.com/q/43735093/2985643), though no solution is offered. – skomisa Oct 21 '19 at 20:29
  • @skomisa Not exactly the same, due to I do not want to force the number of parameters, but can be possible that an answer to this question provides also a solution to me. – King Midas Oct 22 '19 at 09:14

0 Answers0