gpt4 book ai didi

java - Spring Boot @ManyToMany 未显示在 JSON 中

转载 作者:行者123 更新时间:2023-12-02 11:17:45 25 4
gpt4 key购买 nike

我正在尝试从 User 实体获取所有信息,其中包括用户从 Shoes 实体获得的鞋子。当我尝试调用 findAll() 时,它仅从一个表获取信息,但忽略连接的表。请告诉我我做错了什么。

enter image description here

    //Shoes
@Entity
public class Shoes implements Serializable {
private Integer shoesId;
private Integer shoesCode;
private String shoesColor;
private String shoesSeason;
private String shoesType;
private String shoesPicture;
private String shoesShortDescription;
private String shoesFullDescription;
private String shoesPrice;

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "userShoes")
private Set<User> userSet;
//getters and setters


//User
@Entity
public class User implements Serializable {

private Integer userId;
private String userLogin;
private String userPassword;
private String userFirstName;
private String userLastName;
private String userEmail;
private String userBirthDay;
private String userFacebookId;
private String userGoogleId;
private String userAvatar;

@ManyToMany(targetEntity = Shoes.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "user_shoes",
joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "shoes_id")})
private Set<Shoes> userShoes;

//getters and setters

我正在使用 JpaRepository:

@Repository
@Transactional
public interface ShoesRepository extends JpaRepository<Shoes, Serializable> {

List<Shoes> findShoesByShoesColorAndShoesSeason(String shoesColor, String shoesSeason);
}
@Repository
@Transactional
public interface UserRepository extends JpaRepository<User, Serializable> {

}

Controller :

@RestController
@RequestMapping("/v1/items")
public class ShoesController {

private ShoesService shoesService;

@Autowired
public ShoesController(ShoesService shoesService) {
this.shoesService = shoesService;
}

@GetMapping("/shoes/test")
public String hello(){
return "HelloWorld";
}


@GetMapping(path = "shoes")
public ResponseEntity<Iterable<Shoes>> getAll(){
List<Shoes> allshoes = new ArrayList<>();
shoesService.findAll().iterator().forEachRemaining(allshoes::add);
if (allshoes.isEmpty()){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(allshoes, HttpStatus.OK);
} }

@RestController
@RequestMapping("v1/users")
public class UserController {

private UserService userService;

@Autowired
public UserController(UserService userService) {
this.userService = userService;
}

@GetMapping(path = "user")
public ResponseEntity<List<User>> getAll(){
List<User> allUsers = new ArrayList<>();
userService.findAll().iterator().forEachRemaining(allUsers::add);
if (allUsers.isEmpty()){
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(allUsers, HttpStatus.OK);
} }

我正在使用 Postman 来测试: http://localhost:8080/v1/users/user获取 JSON:

[
{
"userId": 1,
"userLogin": "fsdfsdf",
"userPassword": "sdfsdfsdf",
"userFirstName": "fsdfds",
"userLastName": "fsdfsd",
"userEmail": "dfsf",
"userBirthDay": "sdfsdfs",
"userFacebookId": "sdfdsfsd",
"userGoogleId": "fsdfsd",
"userAvatar": "sdsdf"
}
]

但没有鞋子属性。

http://localhost:8080/v1/items/shoes

[
{
"shoesId": 1,
"shoesCode": 23432234,
"shoesColor": "dsfsdf",
"shoesSeason": "fsdfs",
"shoesType": "fsdsdf",
"shoesPicture": "sdfsdf",
"shoesShortDescription": "dfsdfsd",
"shoesFullDescription": "sdfsdf",
"shoesPrice": "sdfsd"
}
]

但没有用户属性。

最佳答案

在主类中找到解决方案。我真的不知道为什么有人要添加注释:

@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan({"com.ua.demoClothes"})
@EntityScan("com.ua.demoClothes.entity")
@JsonAutoDetect

但是当它被删除时,它开始正常工作。只需使用

@SpringBootApplication

在主类

@JsonBackReference
@JsonManagedReference

在实体类中

关于java - Spring Boot @ManyToMany 未显示在 JSON 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com