Skip to content

Commit

Permalink
Create rule S6837: Superfluous "@responsebody" annotations should be …
Browse files Browse the repository at this point in the history
…removed (#3366)

You can preview this rule
[here](https://sonarsource.github.io/rspec/#/rspec/S6837/java) (updated
a few minutes after each push).

## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)

---------

Co-authored-by: johann-beleites-sonarsource <[email protected]>
Co-authored-by: Johann Beleites <[email protected]>
Co-authored-by: Johann Beleites <[email protected]>
  • Loading branch information
4 people authored Oct 31, 2023
1 parent 6325626 commit ff6e324
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rules/S6837/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "Superfluous \"@ResponseBody\" annotations should be removed",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"spring"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6837",
"sqKey": "S6837",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
}
}
70 changes: 70 additions & 0 deletions rules/S6837/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
== Why is this an issue?

The Spring framework's `@RestController` annotation is equivalent to using the `@Controller` and `@ResponseBody` annotations together.
As such, it is redundant to add a `@ResponseBody` annotation when the class is already annotated with `@RestController`.

== How to fix it

Remove the `@ResponseBody` annotation from the class or method.

=== Code examples

==== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
@RestController
public class MyController {
@ResponseBody // Noncompliant, the @RestController annotation already implies @ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
----

==== Compliant solution

[source,java,diff-id=1,diff-type=compliant]
----
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
----

==== Noncompliant code example

[source,java,diff-id=2,diff-type=noncompliant]
----
@RestController
@ResponseBody // Noncompliant, the @RestController annotation already implies @ResponseBody
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
----

==== Compliant solution

[source,java,diff-id=2,diff-type=compliant]
----
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}
}
----

== Resources
=== Articles & blog posts
* Spring Guides - https://spring.io/guides/gs/rest-service/[Building a RESTful Web Service]
* Baeldung - https://www.baeldung.com/spring-controller-vs-restcontroller[The Spring @Controller and @RestController Annotations]
* Baeldung - https://www.baeldung.com/spring-request-response-body[Spring's RequestBody and ResponseBody Annotations]
2 changes: 2 additions & 0 deletions rules/S6837/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit ff6e324

Please sign in to comment.