You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Optimized API: Use of Entity in Rest controller
Platform
Main caracteristics
Severity / Remediation Cost
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.
Implementation principle
The text was updated successfully, but these errors were encountered: