[英]initBinder method is not being called for conversion from String to Enum
I'm testing out the @InitBinder
annotation so I can have String
objects converted into appropriate Enum
objects during web requests. 我正在测试
@InitBinder
批注,以便可以在Web请求期间将String
对象转换为适当的Enum
对象。
I created the following simple Enum
: 我创建了以下简单的
Enum
:
SampleEnum.java SampleEnum.java
public enum SampleEnum {
ONE,
TWO,
THREE,
FOUR,
FIVE;
}
Then, I created an editor extending PropertyEditorSupport
to be called from the @InitBinder
code: 然后,我创建了一个扩展
PropertyEditorSupport
的编辑器,可以从@InitBinder
代码中调用@InitBinder
:
EnumPropertyEditor.java EnumPropertyEditor.java
@SuppressWarnings("rawtypes")
public class EnumPropertyEditor extends PropertyEditorSupport {
private Class clazz;
public EnumPropertyEditor(Class clazz) {
this.clazz = clazz;
}
@Override
public String getAsText() {
return (getValue() == null ? "" : ((Enum) getValue()).name());
}
@SuppressWarnings("unchecked")
@Override
public void setAsText(String text) {
Enum e = Enum.valueOf(clazz, text);
setValue(e);
}
}
Then, in my controller I added the @InitBinder
and a simple request mapping: 然后,在我的控制器中,添加了
@InitBinder
和一个简单的请求映射:
Controller 控制者
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(SampleEnum.class, new EnumPropertyEditor(SampleEnum.class));
}
@RequestMapping(method = POST, value = "/postSampleEnum")
@ResponseBody
public SampleEnum postSampleEnum(@RequestBody SampleEnum sampleEnum) {
return sampleEnum;
}
From my understanding, a request for this controller method should attempt to convert a string value into the SampleEnum
object. 据我了解,对此控制器方法的请求应尝试将字符串值转换为
SampleEnum
对象。 However, no breakpoints are hit in either initBinder
, request mapping method, nor any of the methods in the EnumPropertyEditor
. 但是,
initBinder
,请求映射方法或EnumPropertyEditor
中的任何方法都不会命中断点。
I'm testing with RESTClient in FireFox, and have tried sending in the request body "THREE", which I would expect to work. 我正在FireFox中使用RESTClient进行测试,并尝试发送请求主体“ THREE”(我希望它可以正常工作)。 Instead, I get a 415 error regardless of the what's in the request body.
相反,无论请求主体中有什么内容,我都会收到415错误。 (The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().)
(服务器拒绝此请求,因为请求实体的格式不受请求的方法()所请求的资源支持。)
If I change the request mapping to take in a string instead of a SampleEnum
, the postSampleEnum
gets called and doesn't use the custom editor (as expected). 如果我将请求映射更改为采用字符串而不是
SampleEnum
, postSampleEnum
调用SampleEnum
,并且不使用自定义编辑器(按预期方式)。
Am I missing anything that allows the custom editor code to be called? 我是否缺少任何可以调用自定义编辑器代码的内容? What is the best way to continue debugging this?
继续调试的最佳方法是什么?
First of all, I forgot to add the application/json content-type to the request header in RESTClient. 首先,我忘了将application / json内容类型添加到RESTClient中的请求标头中。 >_<
> _ <
However, I noticed that the code execution still doesn't go through the custom property editor. 但是,我注意到代码执行仍然没有通过自定义属性编辑器进行。 As GriffeyDog said, it looks like the code only executes if I switch to a
RequestParam
or ModelAttribute
. 就像GriffeyDog所说的,似乎代码仅在我切换到
RequestParam
或ModelAttribute
时才执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.