Best practice for maintaining method visibility using the Spring @Controller stereotype

My project, for current use, uses a private access modifier for the methods of MVC controllers:

@Controller public class HelloWorldController { @RequestMapping("/helloWorld") private ModelAndView helloWorld() { 

I have included PMD, and in his report a lot:

 /src/main/java/com/web/controller/SignalController.java:91: Avoid unused private methods such as 'handleNewRequest()'. 

Therefore, instead of disabling the useful PMD rule, I think that you can use public to change the visibility of control methods.

Are there any reasons to keep the controller methods private ?

+4
source share
1 answer

You shoot in the foot, making it private:

  • it is treated as unused PMD and IDE (and, transitively, all other private methods that it calls) too. This way, you or your colleagues may mistakenly remove private methods that are actually used.
  • It is much more difficult to unit - check them out.
  • This is unconventional, making your code weird for experienced Spring developers.
  • They are logically public because they are called by code outside the class and package.
+3
source

Source: https://habr.com/ru/post/1490735/


All Articles