1 year ago

#317655

test-img

manix

Using enum as @PathVariable in FeignClient

I don't know how to make FeignClient working with enum serialization as @PathVariable.

I have enum type representing Access Type:

public enum StorageFileAccessType {
    TOKEN(0),
    SMSCODE(1);

    private final int id;

    private StorageFileAccessType(int id) {
        this.id = id;
    }

    @JsonValue
    public int getId() {
        return id;
    }

...
}

Each enum value has corresponding int value, which should be used during serialization.

Then I have FeignClient with one method for uploading file: it uploads file for given user and sets access type (token or smscode).

@FeignClient(value = "uploadClient", url = "${serverUrl}", configuration = { ClientConfig.class })
public interface UploadClient {
    @RequestMapping(method = RequestMethod.POST, value = "files/write/{user}/{accessType}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Result uploadFile(@PathVariable("user") String user,
            @PathVariable("accessType") StorageFileAccessType accessType,
            @RequestPart("file") MultipartFile file);

}

Now when I call method, e.g:

feignClient.uploadFile("user", StorageFileAccessType.TOKEN, file);

I expect request is sent with URI: ${serverUrl}/files/write/user/0
but instead it is sent with URI: ${serverUrl}/files/write/user/TOKEN what means Enum serialization to integer is not performed and enum name is used.

When I use enum inside @RequestPart, serialization to int works correctly.

Is there any way to make this Enum serialization working also for @PathVariable?

Of course I can change method parameter to: @PathVariable("accessType") int accessType and pass int directly - and it works, but If possible I prefer using enum - as more cleaner solution.

spring

enums

spring-cloud-feign

openfeign

0 Answers

Your Answer

Accepted video resources