Spring 弹簧靴有问题+;h2和x2B;jpa项目
这是我的Spring 弹簧靴有问题+;h2和x2B;jpa项目,spring,spring-boot,jpa,h2,Spring,Spring Boot,Jpa,H2,这是我的用户课程: @Data @Entity public class User { @Id @GeneratedValue Long userID; String eMail; String phoneNumber; String displayName; //File displayPicture; String firstName; String middleName; String lastName; Array
用户
课程:
@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() {
}
}
@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
,但我想返回除它之外的所有内容
如何在没有其他类的情况下完成此操作?添加到字段定义中。
或者,您可以在类级别上使用
我注意到您尝试过给字段private access修饰符。这不会将字段隐藏到JSON序列化程序中,因为您的类用Lombok注释,当在没有@Getter/@Setter和重写访问权限的情况下使用时,它会将对生成方法的访问设置为public。添加到字段定义中。
或者,您可以在类级别上使用
我注意到您尝试过给字段private access修饰符。这不会将字段隐藏到JSON序列化程序中,因为您的类用Lombok注释,当在没有@Getter/@Setter和重写访问权限的情况下使用时,它会将对生成方法的访问设置为public。或者您可以将另一个对象返回到浏览器
@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();
})
}
}
@Builder
@资料
公共类用户响应{
私人字符串电子邮件;
私有字符串电话号码;
私有字符串显示名;
//因为我懒惰,所以省略了其余部分
}
@RestController
公共类用户控制器{
私有最终用户存储库;
@自动连线
公共用户控制器(用户存储库){
this.repository=存储库;
}
@GetMapping(“/user/{id}”)
public UserResponse one(@PathVariable Long id){
最终可选用户=repository.findById(id).orelsetrow(()->newusernotfoundexception(id));
返回user.map(userEntity->{
返回UserResponse.builder()
.eMail(userEntity.getEMail())
.phoneNumber(userEntity.getphoneNumber())
//因为我懒惰,所以省略了其余部分
.build();
})
}
}
或者您可以将另一个对象返回到浏览器
@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();
})
}
}
@Builder
@资料
公共类用户响应{
私人字符串电子邮件;
私有字符串电话号码;
私有字符串显示名;
//因为我懒惰,所以省略了其余部分
}
@RestController
公共类用户控制器{
私有最终用户存储库;
@自动连线
公共用户控制器(用户存储库){
this.repository=存储库;
}
@GetMapping(“/user/{id}”)
public UserResponse one(@PathVariable Long id){
最终可选用户=repository.findById(id).orelsetrow(()->newusernotfoundexception(id));
返回user.map(userEntity->{
返回UserResponse.builder()
.eMail(userEntity.getEMail())
.phoneNumber(userEntity.getphoneNumber())
//因为我懒惰,所以省略了其余部分
.build();
})
}
}
一种常见的做法是,不要直接将实体返回给客户端,而是使用特定的响应对象,这样数据库就有了自己的API,客户端就会暴露出另一个API。您能解释一下吗?在调用数据库时,您可以使用@Entity
注释类。然后将所有对象从该类移到另一个类,然后将该对象返回到浏览器(客户端)。通过这种方式,您可以在不影响浏览器对象的情况下向数据库中添加内容,也可以在不影响数据库的情况下向浏览器对象中添加内容。概念很清楚,我想,一个示例或参考将非常有帮助。发布演示概念的答案一种常见做法是不将实体直接返回给客户端,而是使用特定的响应对象,这样数据库就有了自己的API,客户端就会暴露出另一个API。您能解释一下吗?您在调用数据库时使用了@Entity
注释类。然后将所有对象从该类移到另一个类,然后将该对象返回到浏览器(客户端)。通过这种方式,您可以在不影响浏览器对象的情况下向数据库中添加内容,也可以在不影响数据库的情况下向浏览器对象中添加内容。概念很清楚,我想,一个示例或参考将非常有帮助。发布演示概念的答案