When developing a spring project, the front and back ends need to return different data, how to deal with it

I am a java rookie, ask some rookie questions?

use spring-boot to develop projects, spring-data-jpa to access databases,

there are many fields in the database, but only some of them are used by the client, while the data needed by the background is more comprehensive, for example:

Field of entity:

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity
@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue
    private Long id;

    private String nickName;  // 

    private Integer status;  // 

    private String gender;  // 

    private String signature;  // 

    private Date updatedAt; // 

    private Date createdAt;  // 

    private Date loginAt; //  

    private String loginIp; //  IP
    
    private String adminNote; //  
}

this table is used by the: id, nickName, gender, signature client page. The other fields do not want to be seen by the user, but the administrator should see,

how should the entity class be designed? do you want to do two entity classes?

do I use graphql, to communicate between the front end and the back end? do the front and back ends generate different Schema

Feb.28,2021

1jsonxml
2json,
3,
:
     springbootJsonjacksonjson
public class User {                                                   
                                                                      
    public int id;                                                    
                                                                      
    public String name;                                               
    //
    @JsonIgnore                                                       
    public String pwd;                                                
                                                                      
    public int age;                                                   
                                                                      
    public User(int id , String name , String pwd , int age) {        
        this.id = id;                                                 
        this.name = name;                                             
        this.pwd = pwd;                                               
        this.age = age;                                               
    }               
  ...get\set                                                  

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("")
    public Object findUser()
    {
        return new User(1,"demo","demo",22);
    }
}

return the result

{
"id": 1,
"name": "demo",
"age": 22
}

if the data displayed in the foreground and background is different, it usually requires multiple entity.
your requirement is to wrap the data in the service layer with different entity and return it to the front end.
in addition, you can Baidu dto vo dao these entity differences, to help you understand how to send different entity


without spring mvc? Spring mvc can solve your doubts

Menu