How can I recieve the id inside a wrapper object on the server side in Spring?

Viewed 12

I wrote a controller-client method, which includes a wrapper-object (custom object) in the @PathVariable. This wrapper object is mandatory to do type-based validations inside the @CustomAnnotation.

@CustomAnnotation
@PostMapping
public void save(@Valid @NotNull @PathVariable("id") Id id) {
        // service calls etc.
}

The wrapper object only contains an Id

public class Id {

private String id;

public Id(String id) {
    this.id = id;
}

public Long getId() {
    // ...
}

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

This client method is in an interface package and can distributed to clients. The clients call the server via OpenFeign. The controller serving the interface implements these methods.

Problem:
I can easily invoke the method type-based, but the server retrieves the object as .toString() - making it impossible to reconstruct the id.

How can I recieve the original id inside a wrapper object on the server side?

1 Answers

Just overwrite the toString method. This way, the server can understand the object in @PathVariable

    @Override
    public String toString() {
        return id;
    }
Related