自定义DTO内的映射请求参数和字段? [英] Customize mapping request parameters and fields inside the DTO ?
问题描述
我有以下课程:
public class MyDTO {
private String kiosk;
...
}
以及以下网址:
http://localhost:1234/mvc/controllerUrl?kiosk=false
以及以下控制器方法:
@RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)
@ResponseBody
public ResponseEntity<List<?>> getRequestSupportKludge(final MyDTO myDTO, BindingResult bindingResult) {
...
}
现在它可以正常工作,并且布尔值字段可以正确解析.
Now it is working nice and boolean field resolves properly.
现在url参数已更改如下:
Now url parameter has changed like this:
http://localhost:1234/mvc/controllerUrl?new_kiosk=false
我不想在DTO内更改参数名称.有没有办法说弹簧来理解new_kiosk
请求参数值应该放在kiosk
字段中?
I don't want to change parameter name inside the DTO. Is there way to say spring to understand that new_kiosk
request parameter value should be put into kiosk
field ?
推荐答案
除了设置其他设置器之外,您还可以通过创建自定义参数解析器来处理该问题.有几种方法可以解决,但是已经有充分讨论的
Apart from setting an additional setter you can hande the case by making a custom argument resolver. There's a few ways you can go about it, but there's already a well discussed post. If I were you I would focus on the jkee's answer. Follow it step by step, and than all you should do is annotate your DTO with something like,
public class MyDTO {
@ParamName("new_kiosk")
private String kiosk;
...
}
请注意,即使您无法更改MyDTO
类,也仍然可以遵循自定义解析程序路线.在此帖子中,我已经回答了如何编写参数类型注释.结合这两个帖子,您可以轻松地提出一个注释,例如@ParamMapper
,它将定义从请求到属性的映射.想像
Note that even if you can't change MyDTO
class, you can still follow a custom resolver route. In this post I've answered how you can write a parameter type annotation. Combining the two post you can easily come up with an annotation e.g. @ParamMapper
that would define the mapping from request to properties. Think of something like
getRequestSupportKludge(@ParamMapper("new_kiosk;kiosk") MyDTO myDTO, BindingResult bindingResult)
这篇关于自定义DTO内的映射请求参数和字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!