Recipe Name:
Missing annotation in controller class
Description:
This class is missing a @Controller or @RestController annotation
Level:
error
Language:
  • java
Tags:
  • Spring
  • framework specific
  • web
  • Spring Web
  • quality
Documentation

Controllers and RestControllers should be marked using the respective Spring stereotypes annotations.

Before
public class MyController {

  @GetMapping("/hello/{name}")
  public String getHello(@PathVariable String name) {
    ...
  }
}
After
@RestController
public class MyController {

  @GetMapping("/hello/{name}")
  public String getHello(@PathVariable String name) {
    ...
  }
}
Recipe
id: scw:spring:web:restcontroller-annotated
version: 10
metadata:
  name: Missing annotation in controller class
  shortDescription: This class is missing a @Controller or @RestController annotation
  level: error
  language: java
  enabled: true
  descriptionFile: descriptions/Missing_annotation_in_controller_class.html
  tags: Spring;framework specific;web;Spring Web;quality
search:
  class:
    not:
      annotation:
        anyOf:
        - type: org.springframework.stereotype.Controller
        - type: org.springframework.web.bind.annotation.RestController
    member:
      method:
        anyOf:
        - annotation:
            type: org.springframework.web.bind.annotation.PostMapping
        - annotation:
            type: org.springframework.web.bind.annotation.GetMapping
        - annotation:
            type: org.springframework.web.bind.annotation.PutMapping
        - annotation:
            type: org.springframework.web.bind.annotation.DeleteMapping
    without:
      modifier: abstract
availableFixes:
- name: Add @RestController annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.web.bind.annotation.RestController'
      target: parentClass
- name: Add @Controller annotation
  actions:
  - addAnnotation:
      annotation: '@org.springframework.stereotype.Controller'
      target: parentClass