【发布时间】:2017-02-25 04:05:20
【问题描述】:
我正在使用@Restcontroller 和@Vaid 注释。
@RequestMapping(path = "/1050"
method = RequestMethod.POST,
headers = {"Content-Type=application/json"},
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE
)
public UserListResp getUserList(@RequestBody @Valid UserListReq request, BindingResult bindingResult,
Principal principal){
UserListResp response = new UserListResp();
if (bindingResult.hasErrors()){
response.setResultCode(102); // Validation error
response.setErrMsg("Wrong " + bindingResult.getFieldError().getDefaultMessage() + " value.");
} else {
return userService.getUserList(request) ;
}
return response;
}
传入的请求映射到经过验证的对象。
public class UserListReq {
private String userName;
....
}
我没有从传入的 json 请求中获取此值(用户名),而是通过令牌从 oAuth 服务中获取的。 是否可以从 @ControllerAdvice 将 userName 发送到验证约束?
@InitBinder
public void dataBinding(WebDataBinder binder, HttpServletRequest req) {
// userName
req.getUserPrincipal().getName();
}
谢谢。
【问题讨论】:
-
谢谢,但我的问题更难。我有休息控制器,我已经在正文请求中发送了 json(我没有使用 POST 或 GET 表单参数)。
标签: spring-mvc bean-validation spring-validator