【问题标题】:Spring Boot @ManyToMany not showing in JSONSpring Boot @ManyToMany 未在 JSON 中显示
【发布时间】:2018-05-03 18:37:59
【问题描述】:

我正在尝试从User 实体获取所有信息,其中包括用户从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"
    }
]

但没有用户属性。

【问题讨论】:

  • 您是否在 userShoes 上定义了公共 getter?如果不是,您应该添加它或强制杰克逊使用字段(例如,通过@JsonProperty 或通过@JsonAutoDetect 更改自动检测策略)
  • 附带说明:userService.findAll().iterator().forEachRemaining(allUsers::add) 效率不高,因为它可能会强制ArrayList 调整多次大小,无论如何你为什么不只是返回 userService.findAll()?
  • 您的数据库中的节目和用户之间是否存在关联?

标签: java rest spring-boot spring-data-jpa many-to-many


【解决方案1】:

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

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

但是当它被移除时,它开始正常工作。只需要

@SpringBootApplication

在主类中

@JsonBackReference
@JsonManagedReference 

在实体类中

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2020-07-20
    • 2019-06-16
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多