'Restcontroller method naming convention spring boot

Are there conventions for naming methods in the Restful spring boot conrtoller layer ? I am in dilemma choosing the two name - getSomeData vs fetchSomeData. Is it OK to use HTTP Verbs (get,post,put) inside controller method names?



Solution 1:[1]

I think the most important thing is to be consistent in all your Controllers and to be explicit about what the method is supposed to be doing. It is completely ok to use HTTP verbs in the method names, especially in regards to GET. But when you have POSTs for example, that is usually a creation of a resource, so a method called createWhateverResource instead of postWhateverResource. The important thing is to be clear and let the name of the method be self-explanatory.

Solution 2:[2]

I checked a bit on the net. My conclusions:

  1. There are no official naming rules
  2. Official Spring Boot documentation uses short names: all(), one(), etc.
  3. Names for the URLs are most important, method names are secondary
  4. You never call these methods directly in code, they are only called by Spring framework.

A related note - for methods returning HTML (using Thymeleaf templates) I would probably call the methods by the page that they return: home(), orderDetails(), etc. Again for the same reason - we never call the methods directly. At the same time, it is very clear that @Controller and @RestController classes contain only methods returning HTTP responses to specific endpoints. Therefore, the verbs are probably not necessary.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 João Dias
Solution 2 Girts Strazdins