Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java][Team 2 millions] Use of Entity in Rest controller #58

Closed
JulienPlanque opened this issue Apr 6, 2023 · 2 comments
Closed

[Java][Team 2 millions] Use of Entity in Rest controller #58

JulienPlanque opened this issue Apr 6, 2023 · 2 comments

Comments

@JulienPlanque
Copy link

Optimized API: Use of Entity in Rest controller

Platform

OS OS version Langage
- - Java

Main caracteristics

ID Title Category Sub-category
??? Use of Entity in Rest controller Environment Optimized API

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Using persistence Entity in the return of a Rest controler is not
energy efficient. This use induces a multiples unnecessary SQL calls, multiple unused fields serialization,
and thus unnecessary calculations by the CPU, RAM usage and network usage.

Rule complete description

Text

Using persistence Entity, class with @entity annotation of javax.persistence is not
energy efficient.
Prefer the usage of Data Transfert Object (DTO) which is more energy friendly. This reduces the amount of information returned so that it is more relevant and lighter.

<p>Using persistence Entity in the return of a Rest controler is not
energy efficient. This use induces a multiples unnecessary SQL calls, multiple unused fields serialization,
and thus unnecessary calculations by the CPU, RAM usage and network usage. </p>
<h2>Noncompliant</h2>
<pre>
	@RestController
    public class AvoidReturningSpringEntityInRestController {
        private final BookService bookService;

        public AvoidReturningSpringEntityInRestController(BookService bookstoreService) {
            this.bookService = bookstoreService;
        }

        @RequestMapping("books")
        public List<Book> findBooks() {
        return bookService.findAll();
        }
    }

    @Entity
    public class Books {
        // Attributes of books
    }
</pre>
<h2>Compliant Solution</h2>
<pre>
    @RestController
    public class AvoidReturningSpringEntityInRestController {
        private final BookService bookService;

        public AvoidReturningSpringEntityInRestController(BookService bookstoreService) {
            this.bookService = bookstoreService;
        }

        @RequestMapping("books")
        public List<BookDTO> findBooks() {
            return BookMapper.from(bookService.findAll());
        }
    }
</pre>

Implementation principle

  • Inspect the all classes with @RestController annotation
  • Inpect in all method the class in return, directly or in a collection
  • If this class has @entity annotation of javax.persistence, report the line
@JulienPlanque JulienPlanque changed the title [Java] Use Use of Entity in Rest controller [Java] Use of Entity in Rest controller Apr 6, 2023
@JulienPlanque
Copy link
Author

Build by Teams 2 millions

@JulienPlanque JulienPlanque changed the title [Java] Use of Entity in Rest controller [Java][Team 2 millions] Use of Entity in Rest controller Apr 6, 2023
@dedece35
Copy link
Member

Hi @MP-Aubay, @JulienPlanque

sorry but with implementation is that situation, we can't accept this implementation because :

  • no scientific paper to prove this rule
  • no measures to prove it's a "green" rule

We keep this proposition to measure it when we will have a tool to measure it.
(check RULES.md )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants