spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
913 stars 561 forks source link

Transient are ignored in response #2316

Closed lowcasz closed 11 months ago

lowcasz commented 11 months ago

I have some fields which should be displayed in response, but not stored in db. I'm trying to add property to response, but I can't. Using additional getters, it doesn't work. Using @Transient fields with @PolstLoad calculate() method, it doesn't work too?

IMO Response should be controlled by @JsonIgnore/@JsonInclude instead of @Transient Is it purposeful behaviour? Can I do this on Entity level without overrides/create controller?

mp911de commented 11 months ago

We do not filter transient properties from the Jackson representation.

"_embedded": {
"persons": [
{
  "name": "John",
  "greeting": "Hello John",
  "_links": {
    "self": {
      "href": "http://localhost:8080/persons/1"
    },
    "person": {
      "href": "http://localhost:8080/persons/1"
    }
  }
},…
@Entity
public class Person {

    @Id @GeneratedValue Long id;
    String name;
    @Transient String greeting;

    public Person() {
    }

    public Person(Long id, String name) {
        this.id = id;
        this.name = name;
        this.greeting = "Hello " + name;
    }

    @PostLoad
    public void postLoad() {
        this.greeting = "Hello " + name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}

Please note that there is also an association for certain operations (such as PATCH) that require a property mapping between its persistent state and its REST representation.