Should I use jpa object in rest request and / or response

I have a situation where I can send a JPA object as a vacation request and / or receive a JPA object as a vacation response

@RequestMapping(value = "/products", method = RequestMethod.POST)
public @ResponseBody ProductDetailsResponse createNewProduct(@RequestBody ProductDetails newProduct)
        throws Exception {

ProductDetails is the essence

@Entity
@Table(name = "product")
public class ProductDetails {

Should I use this or have some kind of conversion from objects to other types of objects

+4
source share
2 answers

There is no hard and fast rule, but it is not considered good practice (for very good reasons and its very stubborn look) to use JPA objects as DTO (data transfer objects).

In addition to DTOs, which are light versions of entities in terms of size, there are other advantages.

, , - , . , DTO, JSON .

, JSON ( ) DTO , , DB, - .

( DTO Entity ). API- mapper, .

, , DTO , .

SO , ,

DTO ?

DTO

REST API - DTO ?

DTO.

+5

, . (JSON XML).

, ( ) . . , .

(CRUD-), , .

, @JsonView.

( , ), DTO . , -API (DTO) () .

+3

Source: https://habr.com/ru/post/1680593/


All Articles