java - Jersey JAX -RS 中的 @InitBinder 是否有等效项?
问题描述
我正在构建一个新的休息 API 资源,我有一个 @queryParam,它可以是“CSV”或“XLS”。我使用枚举类完成了这个,然后我实现了这个“@DefaultValue("xls") @QueryParam(FORMAT_FILTER) FormatExport 格式”谷歌搜索我意识到实现@InitBinder 是必要的,尽管InitBinder 是Spring 的一部分,而不是Jersey。有没有等价物。
//1. Enum class.
public enum FormatExport
{
json("xls"),
scv("csv");
private String label;
private FormatExport(String value)
{
this.label = value;
}
public static FormatExport fromValue(String value)
{
for(FormatExport format : values())
{
if(format.label.compareToIgnoreCase(value) == 0)
{
return format;
}
}
throw new IllegalArgumentException("Unknown value type: " + value );
}
}
//2. resource
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/elements")
public Response findElements(
@DefaultValue("json") @QueryParam(FORMAT_FILTER) FormatExport format)
{
switch (format)
{
case json:
/* Code */
case scv:
/* Code */
default:
break;
}
}
//I found this
public class QuestionCategoryConverter extends PropertyEditorSupport{
public void setAsText(final String text) throws IllegalArgumentException
{
setValue(QuestionCategory.fromValue(text));
}
}
@InitBinder
public void initBinder(final WebDataBinder webdataBinder) {
webdataBinder.registerCustomEditor(QuestionCategory.class, new
QuestionCategoryConverter());
}
//This used a spring annotation