Java Spring Boot@ManyToMany未在JSON中显示
我正在尝试从Java Spring Boot@ManyToMany未在JSON中显示,java,rest,spring-boot,spring-data-jpa,many-to-many,Java,Rest,Spring Boot,Spring Data Jpa,Many To Many,我正在尝试从用户实体获取所有信息,其中包括用户从鞋子实体获得的鞋子。当我尝试调用findAll()时,它只从一个表中获取信息,而忽略连接的表。请告诉我我做错了什么 但是没有鞋的财产 但没有用户属性。在主类中找到解决方案。 我真的不知道为什么有人会添加注释: @Import(RepositoryRestMvcConfiguration.class) @ComponentScan({"com.ua.demoClothes"}) @EntityScan("com.ua.demoClothes.en
用户
实体获取所有信息,其中包括用户从鞋子
实体获得的鞋子。当我尝试调用findAll()
时,它只从一个表中获取信息,而忽略连接的表。请告诉我我做错了什么
但是没有鞋的财产
但没有用户属性。在主类中找到解决方案。 我真的不知道为什么有人会添加注释:
@Import(RepositoryRestMvcConfiguration.class)
@ComponentScan({"com.ua.demoClothes"})
@EntityScan("com.ua.demoClothes.entity")
@JsonAutoDetect
但当它被移除时,它开始正常工作。刚刚
@springbootplication
主课
及
在entities类中,是否在userShoes上定义了公共getter?如果不是,您应该添加它或强制jackson使用字段(例如,通过
@JsonProperty
或通过@jsonautodect
更改自动检测策略),并作为旁注:userService.findAll().iterator().forEachRemaining(allUsers::add)
不是很有效,因为它可能会强制ArrayList
调整多个时间的大小,以及您不只是返回userService的原因。findAll()?有问题:您的数据库中的节目和用户之间有关联吗?
@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> {
}
@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);
} }
[
{
"userId": 1,
"userLogin": "fsdfsdf",
"userPassword": "sdfsdfsdf",
"userFirstName": "fsdfds",
"userLastName": "fsdfsd",
"userEmail": "dfsf",
"userBirthDay": "sdfsdfs",
"userFacebookId": "sdfdsfsd",
"userGoogleId": "fsdfsd",
"userAvatar": "sdsdf"
}
]
[
{
"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
@JsonBackReference
@JsonManagedReference