Skip to content

Ignore field only for data rest #2475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ZeeD opened this issue Apr 29, 2025 · 0 comments
Open

Ignore field only for data rest #2475

ZeeD opened this issue Apr 29, 2025 · 0 comments
Assignees
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@ZeeD
Copy link

ZeeD commented Apr 29, 2025

According to https://docs.spring.io/spring-data/rest/reference/customizing/configuring-the-rest-url-path.html#customizing-sdr.hiding-repositories

You may not want a certain repository, a query method on a repository, or a field of your entity to be exported at all. Examples include hiding fields like password on a User object and similar sensitive data. To tell the exporter to not export these items, annotate them with @RestResource and set exported = false.

[...]

to skip exporting a field, you can annotate the field with @RestResource(exported = false), as follows:

@Entity
public class Person {

  @Id @GeneratedValue private Long id;

  @OneToMany
  @RestResource(exported = false)
  private Map<String, Profile> profiles;
}

but this doesn't seems to work, not with association nor with "normal" attributes:

if I have defined an entity like

package vito.prove.spring.classic;

import java.util.List;
import org.springframework.data.rest.core.annotation.RestResource;
import jakarta.persistence.*;
import lombok.*;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor(staticName = "of")
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    @OneToMany(fetch = FetchType.EAGER)
    @RestResource(exported = false)
    private List<Profile> profiles;

    @RestResource(exported = false)
    private String label;
}

where I want to "hide" both profiles and label fields from a rest repository; yet in the rest interface I see that the endpoints still return all the fields

{
  "profiles": [
    {
      "label": "profile 1"
    }
  ],
  "label": "person 1",
  "_links": {
    "self": {
      "href": "http://localhost:8080/persons/1"
    },
    "person": {
      "href": "http://localhost:8080/persons/1"
    }
  }
}

keep in mind that I also use the same entity in a "normal" controller+service-based rest interface in the same application, so I cannot really use the jackson annotations, because they will hide the fields also there

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 29, 2025
@mp911de mp911de self-assigned this May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

3 participants