0

I have the following class:

@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;

I'm using spring boot with spring data. I want to ignore some of the fields in the getAllPositions method, but not in the getPositionById method, so @JsonIgnore won't work for me. What is the best way to do this in Spring Boot 2.0.2?

Here is the controller:

@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();
}
Ammar Samater
  • 529
  • 2
  • 7
  • 24
  • 1
    There's such Jackson feature (https://stackoverflow.com/questions/38279782/what-is-the-json-view-class-in-jackson-and-how-does-it-work) `@JsonView`, where the `Public.class` \ `Internal.class` are marker interfaces, optionally empty ones – WildDev Oct 13 '18 at 21:35
  • 1
    so the answer is to use `@JsonView` – Ammar Samater Oct 14 '18 at 08:16

0 Answers0