繁体   English   中英

Spring Boot @ManyToMany没有显示在JSON中

[英]Spring Boot @ManyToMany not showing in JSON

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

在此处输入图片说明

    //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> {

}

控制器:

@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 

在实体类中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM