让Jackson在Spring Boot中忽略控制器级别的某些字段

时间:2018-10-13 21:10:33

标签: spring-boot spring-restcontroller

我有以下课程:

@Entity
@Table(name = "Positions")
@NamedQuery(name = "Position.findAll", query = "SELECT p FROM Position p")
    public class Position implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Long id;

private String title;

private String description;

@ElementCollection
@CollectionTable(name = "qualifications", joinColumns = @JoinColumn(name = "position_id"))
@Column(name = "required_qualifications")
private List<String> requiredQualifications;

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "positions_competencies", joinColumns = @JoinColumn(name = "position_id"), inverseJoinColumns = @JoinColumn(name = "Competence_id"))
private List<Competence> competencies;

@ManyToOne
@JoinColumn(name = "department_id")
private Department department;

我正在使用带有弹簧数据的弹簧靴。 我想忽略getAllPositions方法中的某些字段,而不是getPositionById方法中的某些字段,因此@JsonIgnore对我不起作用。在Spring Boot 2.0.2中执行此操作的最佳方法是什么?

这是控制器:

@Autowired
private PositionRepository positionRepository;

@GetMapping(path = "/positions")
public Iterable<Position> getAllPositions() {
    return positionRepository.findAll();
}

@GetMapping(path = "/positions/{id}")
public Position getPositionById(@PathVariable Long id) {
    return positionRepository.findById(id).get();
}

0 个答案:

没有答案