Sensei Logo Sensei Hub

Recipe - Avoid mapping to multiple HTTP request methods

Description:
Map to one HTTP request method for best practices
Level:
info
Language:
  • java
Tags:
  • Spring
  • framework specific
  • web
  • Spring Web
  • quality
Documentation

It's considered best practices to map one HTTP method per method. This improves readability and could make refactoring easier.

Before
@RequestMapping(path = "/doSomething", method = { RequestMapping.GET, RequestMapping.POST })
After
@GetMapping(path = "/doSomething")
References
Recipe
id: scw:spring:web:multiple-http-mappings
version: 10
metadata:
  name: Avoid mapping to multiple HTTP request methods
  shortDescription: Map to one HTTP request method for best practices
  level: info
  language: java
  enabled: true
  descriptionFile: descriptions/AvoidmappingtomultipleHTTPrequestmethods.html
  tags: Spring;framework specific;web;Spring Web;quality
search:
  annotation:
    type: org.springframework.web.bind.annotation.RequestMapping
    parameters:
    - name: method
      value:
        value:
          stringified:
            contains: '{'
availableFixes:
- name: Set annotation to DeleteMapping
  actions:
  - changeAnnotation:
      removeParameter:
      - withName: method
  - changeAnnotation:
      type: org.springframework.web.bind.annotation.DeleteMapping
- name: Set annotation to GetMapping
  actions:
  - changeAnnotation:
      removeParameter:
      - withName: method
  - changeAnnotation:
      type: org.springframework.web.bind.annotation.GetMapping
- name: Set annotation to PostMapping
  actions:
  - changeAnnotation:
      removeParameter:
      - withName: method
  - changeAnnotation:
      type: org.springframework.web.bind.annotation.PostMapping
- name: Set annotation to PatchMapping
  actions:
  - changeAnnotation:
      removeParameter:
      - withName: method
  - changeAnnotation:
      type: org.springframework.web.bind.annotation.PatchMapping
- name: Set annotation to PutMapping
  actions:
  - changeAnnotation:
      removeParameter:
      - withName: method
  - changeAnnotation:
      type: org.springframework.web.bind.annotation.PutMapping
- name: Set method parameter to HEAD
  actions:
  - changeAnnotation:
      addParameter:
      - name: method
        value: org.springframework.web.bind.annotation.RequestMethod.HEAD
      removeParameter:
      - withName: method
- name: Set method parameter to OPTIONS
  actions:
  - changeAnnotation:
      addParameter:
      - name: method
        value: org.springframework.web.bind.annotation.RequestMethod.OPTIONS
      removeParameter:
      - withName: method
- name: Set method parameter to TRACE
  actions:
  - changeAnnotation:
      addParameter:
      - name: method
        value: org.springframework.web.bind.annotation.RequestMethod.TRACE
      removeParameter:
      - withName: method