【问题标题】:problem with spring boot + h2 + jpa projectspring boot + h2 + jpa项目的问题
【发布时间】:2020-01-04 12:40:26
【问题描述】:

这是我的User 课程:

@Data
@Entity
public class User {

    @Id @GeneratedValue Long userID;
    String eMail;
    String phoneNumber;
    String displayName;
    //File displayPicture;
    String firstName;
    String middleName;
    String lastName;
    ArrayList<ClassRoom>adminOf=new ArrayList<>();
    ArrayList<ClassRoom>memberOf=new ArrayList<>();
    @NotNull
    int rating;
    boolean isTeacher;
    ArrayList<Assignment>assignmentsToSubmit=new ArrayList<>();
    ArrayList<Assignment>assignmentsSubmitted=new ArrayList<>();

    @OneToOne(fetch = FetchType.LAZY,targetEntity = LoginCredential.class)
    @JoinColumn(name = "userID",referencedColumnName = "userID")
    private LoginCredential loginCredential;

    User() {
    }
}

这里是控制器,UserController 类:

@RestController
class UserController {

    private final UserRepository repository;
    UserController(UserRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/user/{id}")
    User one(@PathVariable Long id) {
        return repository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
    }
}

我通过浏览器访问了http://localhost:8080/user/1。这是输出:

这里loginCredential也出现了,但我想退回除它以外的所有东西。

如果没有其他类,如何做到这一点?

【问题讨论】:

  • 一种常见的做法是不将您的实体直接返回给客户端,而是使用特定的响应对象,以便数据库拥有其 API,而客户端会暴露另一个 API。
  • 你能解释一下吗?
  • 调用数据库时使用@Entity注解类。然后,您将所有内容从该类移至另一个类,并将该对象返回给浏览器(客户端)。这样,您可以在不影响浏览器对象的情况下向数据库添加内容,或者您​​可以在不影响数据库的情况下向浏览器对象添加内容。
  • 我想概念很清楚,一个例子或参考会很有帮助
  • 发布了演示概念的答案

标签: spring spring-boot jpa h2


【解决方案1】:

将@JsonIgnore 添加到字段定义中。 或者,您可以在班级级别使用@JsonIgnoreProperties。

我注意到您尝试为字段提供私有访问修饰符。这不会将字段隐藏到 JSON 序列化程序,因为您的类使用 Lombok 的 @Data 进行注释,当在没有 @Getter/@Setter 且具有覆盖访问权限的情况下使用时,将对生成的方法的访问权限设置为公共。

【讨论】:

    【解决方案2】:

    或者您可以将另一个对象返回给浏览器

    @Builder
    @Data
    public class UserResponse {
    
        private String eMail;
        private String phoneNumber;
        private String displayName;
    
        // omitted the rest because im lazy
    
    }
    
    
    @RestController
    public class UserController {
    
        private final UserRepository repository;
    
        @Autowire
        public UserController(UserRepository repository) {
            this.repository = repository;
        }
    
        @GetMapping("/user/{id}")
        public UserResponse one(@PathVariable Long id) {
            final Optional<UserEntity> user = repository.findById(id).orElseThrow(() -> new UserNotFoundException(id));
            return user.map(userEntity -> {
                return UserResponse.builder()
                    .eMail(userEntity.getEMail())
                    .phoneNumber(userEntity.getphoneNumber())
    
                    // omitted the rest because im lazy
    
                    .build();
            })
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2017-06-16
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2017-02-20
      • 2014-08-07
      相关资源
      最近更新 更多