Sensei Logo Sensei Hub

Recipe - Non-annotated controller public method

Description:
Public methods within a controller class should be treated as request handlers, therefore should be annotated as such. If you really believe this method should not be exposed as a Rest endpoint, please consider extracting it into an external Component class and call it from inside this controller.
Level:
warning
Language:
  • java
Tags:
  • Spring
  • framework specific
  • web
  • Spring Web
  • quality
Documentation

Public methods within a controller class should be treated as request handlers, therefore should be annotated as such. If you really believe this method should not be exposed as a Rest endpoint, please consider extracting it into an external Component class and call it from inside this controller.

Before
@RestController
public class MyController {

  public String home() {
    return "home";
  }
}
After
@RestController
public class MyController {

  @GetMapping
  public String home() {
    return "home";
  }
}
References
Recipe
id: scw:spring:web:controller-public-method
version: 10
metadata:
  name: Non-annotated controller public method
  shortDescription: Public methods within a controller class should be treated as request handlers, therefore should be annotated as such. If you really believe this method should not be exposed as a Rest endpoint, please consider extracting it into an external Component class and call it from inside this controller.
  level: warning
  language: java
  enabled: true
  descriptionFile: descriptions/Non-annotated_controller_public_method.html
  tags: Spring;framework specific;web;Spring Web;quality
search:
  method:
    not:
      annotation:
        type:
          reference:
            matches: org\.springframework\.web\.bind\.annotation..*
          checkInheritance: true
    in:
      typeDeclaration:
        annotation:
          type: org.springframework.web.bind.annotation.RestController
    modifier: public
    constructor: false
availableFixes:
- name: Add @GetMapping annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.GetMapping'
- name: Add @PostMapping annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.PostMapping'
- name: Add @PutMapping annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.PutMapping'
- name: Add @DeleteMapping annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.DeleteMapping'
- name: Add @PatchMapping annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.PatchMapping'