在 FeignClient 中使用 enum 作为 @PathVariable
我不知道如何使 FeignClient 使用枚举序列化作为 @PathVariable 。
我有代表访问类型的枚举类型:
public enum StorageFileAccessType {
TOKEN(0),
SMSCODE(1);
private final int id;
private StorageFileAccessType(int id) {
this.id = id;
}
@JsonValue
public int getId() {
return id;
}
...
}
每个枚举值都有相应的 int 值,应该在序列化期间使用。
然后我有 FeignClient 和一种上传文件的方法:它为给定用户上传文件并设置访问类型(令牌或短信代码)。
@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);
}
现在,当我调用方法时,例如:
feignClient.uploadFile("user", StorageFileAccessType.TOKEN, file);
我希望使用 URI 发送请求:${serverUrl}/files/write/user/0
但它是通过 URI 发送的: ${serverUrl}/files/write/user/TOKEN 这意味着不执行枚举序列化为整数,而是使用枚举名称。
当我在 @RequestPart 中使用枚举时,序列化为 int 可以正常工作。
有什么方法可以使这个 Enum 序列化也适用于 @PathVariable 吗?
当然我可以将方法参数更改为: @PathVariable("accessType") int accessType 并直接传递 int - 它可以工作,但如果可能的话我更喜欢使用 enum - 作为更干净的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论