-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6837: Superfluous "@responsebody" annotations should be …
…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
1 parent
6325626
commit ff6e324
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |