REST控制器中的Spring Boot绑定和验证错误处理

前端之家收集整理的这篇文章主要介绍了REST控制器中的Spring Boot绑定和验证错误处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我有以下模型与JSR-303(验证框架)注释:

  1. public enum Gender {
  2. MALE,FEMALE
  3. }
  4. public class Profile {
  5. private Gender gender;
  6. @NotNull
  7. private String name;
  8. ...
  9. }

以及以下JSON数据:

  1. { "gender":"INVALID_INPUT" }

在我的REST控制器中,我想处理绑定错误(性别属性的无效枚举值)和验证错误(name属性不能为null).

以下控制器方法不起作用:

  1. @RequestMapping(method = RequestMethod.POST)
  2. public Profile insert(@Validated @RequestBody Profile profile,BindingResult result) {
  3. ...
  4. }

这会在绑定或验证发生之前给出com.fasterxml.jackson.databind.exc.InvalidFormatException序列化错误.

经过一番摆弄后,我想出了这个自定义代码,它可以满足我的需求:

  1. @RequestMapping(method = RequestMethod.POST)
  2. public Profile insert(@RequestBody Map values) throws BindException {
  3. Profile profile = new Profile();
  4. DataBinder binder = new DataBinder(profile);
  5. binder.bind(new MutablePropertyValues(values));
  6. // validator is instance of LocalValidatorfactorybean class
  7. binder.setValidator(validator);
  8. binder.validate();
  9. // throws BindException if there are binding/validation
  10. // errors,exception is handled using @ControllerAdvice.
  11. binder.close();
  12. // No binding/validation errors,profile is populated
  13. // with request values.
  14. ...
  15. }

基本上这个代码的作用是序列化为通用映射而不是模型,然后使用自定义代码绑定到模型并检查错误.

我有以下问题:

>自定义代码是否可以在这里进行,或者在Spring Boot中有更标准的方法吗?
> @Validated注释如何工作?如何创建自己的自定义注释,像@Validated一样封装我的自定义绑定代码

最佳答案
这是我在我的一个项目中使用的代码,用于在spring boot中验证REST api,这与你要求的不一样,但是相同..检查这是否有帮助

  1. @RequestMapping(value = "/person/{id}",method = RequestMethod.PUT)
  2. @ResponseBody
  3. public Object updatePerson(@PathVariable Long id,@Valid Person p,BindingResult bindingResult){
  4. if (bindingResult.hasErrors()) {
  5. ListFailed");
  6. error.setCause(message.toString());
  7. return error;
  8. }
  9. else
  10. {
  11. Person person = personRepository.findOne(id);
  12. person = p;
  13. personRepository.save(person);
  14. success.setMessage("Updated Successfully");
  15. success.setCode(2);
  16. return success;
  17. }

Success.java

  1. public class Success {
  2. int code;
  3. String message;
  4. public int getCode() {
  5. return code;
  6. }
  7. public void setCode(int code) {
  8. this.code = code;
  9. }
  10. public String getMessage() {
  11. return message;
  12. }
  13. public void setMessage(String message) {
  14. this.message = message;
  15. }
  16. }

Error.java

  1. public class Error {
  2. int code;
  3. String message;
  4. String cause;
  5. public int getCode() {
  6. return code;
  7. }
  8. public void setCode(int code) {
  9. this.code = code;
  10. }
  11. public String getMessage() {
  12. return message;
  13. }
  14. public void setMessage(String message) {
  15. this.message = message;
  16. }
  17. public String getCause() {
  18. return cause;
  19. }
  20. public void setCause(String cause) {
  21. this.cause = cause;
  22. }
  23. }

你也可以看看这里:Spring REST Validation

猜你在找的Spring相关文章