spring boot + h2 + jpa 项目有问题
problem with spring boot + h2 + jpa project
这是我的 User
class :
@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
class :
@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
也出现了,但我想 return 除它以外的所有内容。
如果没有另一个 class,如何做到这一点?
在字段定义中添加 @JsonIgnore。
或者,您可以在 class 级别上使用 @JsonIgnoreProperties。
我注意到您尝试为该字段提供私有访问修饰符。这不会将字段隐藏到 JSON 序列化程序,因为您的 class 是用 Lombok 的 @Data 注释的,当不使用 @Getter/@Setter 时使用覆盖访问权限,将对生成方法的访问权限设置为 public.
或者您可以向浏览器 return 发送另一个对象
@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();
})
}
}
这是我的 User
class :
@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
class :
@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
也出现了,但我想 return 除它以外的所有内容。
如果没有另一个 class,如何做到这一点?
在字段定义中添加 @JsonIgnore。 或者,您可以在 class 级别上使用 @JsonIgnoreProperties。
我注意到您尝试为该字段提供私有访问修饰符。这不会将字段隐藏到 JSON 序列化程序,因为您的 class 是用 Lombok 的 @Data 注释的,当不使用 @Getter/@Setter 时使用覆盖访问权限,将对生成方法的访问权限设置为 public.
或者您可以向浏览器 return 发送另一个对象
@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();
})
}
}