Java Code Examples for org.springframework.validation.BindingResult#getFieldError()

The following examples show how to use org.springframework.validation.BindingResult#getFieldError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindingResultAspect.java    From mall-swarm with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 2
Source File: SmsController.java    From parker with MIT License 6 votes vote down vote up
@ApiOperation("发送")
@PostMapping("send")
public BaseResult send(@RequestBody @Validated SmsMessage message, BindingResult bindingResult){
    BaseResult result = new BaseResult();

    // 校验参数
    if (bindingResult.hasErrors()) {
        FieldError fieldError = bindingResult.getFieldError();
        log.error("参数错误:{}", fieldError.getDefaultMessage());
        result.setCode(HttpStatus.BAD_REQUEST.value());
        result.setMessage(fieldError.getDefaultMessage());
        return result;
    }

    try {
        service.send(message);
    } catch (Exception e) {
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
        log.error("发送错误{}", e.getMessage());
        e.printStackTrace();
    }
    return result;
}
 
Example 3
Source File: BindingResultAspect.java    From spring-admin-vue with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if (fieldError != null) {
                    return JsonResult.fail(fieldError.getDefaultMessage());
                } else {
                    return JsonResult.fail("请求参数错误!");
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 4
Source File: ModelResultMatchers.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Assert a field error code for a model attribute using a {@link org.hamcrest.Matcher}.
 * @since 4.1
 */
public <T> ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName,
		final Matcher<? super String> matcher) {

	return mvcResult -> {
		ModelAndView mav = getModelAndView(mvcResult);
		BindingResult result = getBindingResult(mav, name);
		assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
		FieldError fieldError = result.getFieldError(fieldName);
		if (fieldError == null) {
			fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
		}
		String code = fieldError.getCode();
		assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher);
	};
}
 
Example 5
Source File: BindingResultAspect.java    From macrozheng with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 6
Source File: BindingResultAspect.java    From HIS with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 7
Source File: BindingResultAspect.java    From HIS with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 8
Source File: BindingResultAspect.java    From HIS with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 9
Source File: BindingResultAspect.java    From HIS with Apache License 2.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if(fieldError!=null){
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                }else{
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 10
Source File: ModelResultMatchers.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Assert a field error code for a model attribute using a {@link org.hamcrest.Matcher}.
 * @since 4.1
 */
public <T> ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName,
		final Matcher<? super String> matcher) {

	return mvcResult -> {
		ModelAndView mav = getModelAndView(mvcResult);
		BindingResult result = getBindingResult(mav, name);
		assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
		FieldError fieldError = result.getFieldError(fieldName);
		if (fieldError == null) {
			fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
		}
		String code = fieldError.getCode();
		assertThat("Field name '" + fieldName + "' of attribute '" + name + "'", code, matcher);
	};
}
 
Example 11
Source File: BindingResultAspect.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
@Around("BindingResult()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    for (Object arg : args) {
        if (arg instanceof BindingResult) {
            BindingResult result = (BindingResult) arg;
            if (result.hasErrors()) {
                FieldError fieldError = result.getFieldError();
                if (fieldError != null) {
                    return CommonResult.validateFailed(fieldError.getDefaultMessage());
                } else {
                    return CommonResult.validateFailed();
                }
            }
        }
    }
    return joinPoint.proceed();
}
 
Example 12
Source File: GlobalExceptionTranslator.java    From staffjoy with MIT License 5 votes vote down vote up
@ExceptionHandler(MethodArgumentNotValidException.class)
public BaseResponse handleError(MethodArgumentNotValidException e) {
    logger.warn("Method Argument Not Valid", e);
    BindingResult result = e.getBindingResult();
    FieldError error = result.getFieldError();
    String message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
    return BaseResponse
            .builder()
            .code(ResultCode.PARAM_VALID_ERROR)
            .message(message)
            .build();
}
 
Example 13
Source File: ModelResultMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Assert a field error code for a model attribute using exact String match.
 * @since 4.1
 */
public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, final String error) {
	return mvcResult -> {
		ModelAndView mav = getModelAndView(mvcResult);
		BindingResult result = getBindingResult(mav, name);
		assertTrue("No errors for attribute '" + name + "'", result.hasErrors());
		FieldError fieldError = result.getFieldError(fieldName);
		if (fieldError == null) {
			fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
		}
		String code = fieldError.getCode();
		assertTrue("Expected error code '" + error + "' but got '" + code + "'", error.equals(code));
	};
}
 
Example 14
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute("myCommand") TestBean tb, BindingResult errors, ModelMap model) {
	FieldError error = errors.getFieldError("age");
	assertNotNull("Must have field error for age property", error);
	assertEquals("value2", error.getRejectedValue());
	if (!model.containsKey("myKey")) {
		model.addAttribute("myKey", "myValue");
	}
	return "myView";
}
 
Example 15
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute(name="myCommand", binding=true) TestBean tb,
		BindingResult errors, ModelMap model) {

	FieldError error = errors.getFieldError("age");
	assertNotNull("Must have field error for age property", error);
	assertEquals("value2", error.getRejectedValue());
	if (!model.containsKey("myKey")) {
		model.addAttribute("myKey", "myValue");
	}
	return "myView";
}
 
Example 16
Source File: ModelResultMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert a field error code for a model attribute using exact String match.
 * @since 4.1
 */
public ResultMatcher attributeHasFieldErrorCode(final String name, final String fieldName, final String error) {
	return mvcResult -> {
		ModelAndView mav = getModelAndView(mvcResult);
		BindingResult result = getBindingResult(mav, name);
		assertTrue("No errors for attribute '" + name + "'", result.hasErrors());
		FieldError fieldError = result.getFieldError(fieldName);
		if (fieldError == null) {
			fail("No errors for field '" + fieldName + "' of attribute '" + name + "'");
		}
		String code = fieldError.getCode();
		assertTrue("Expected error code '" + error + "' but got '" + code + "'", error.equals(code));
	};
}
 
Example 17
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute("myCommand") TestBean tb, BindingResult errors, ModelMap model) {
	FieldError error = errors.getFieldError("age");
	assertNotNull("Must have field error for age property", error);
	assertEquals("value2", error.getRejectedValue());
	if (!model.containsKey("myKey")) {
		model.addAttribute("myKey", "myValue");
	}
	return "myView";
}
 
Example 18
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/myPath.do")
public String myHandle(@ModelAttribute("myCommand") TestBean tb, BindingResult errors, ModelMap model) {
	FieldError error = errors.getFieldError("age");
	assertNotNull("Must have field error for age property", error);
	assertEquals("value2", error.getRejectedValue());
	if (!model.containsKey("myKey")) {
		model.addAttribute("myKey", "myValue");
	}
	return "myView";
}
 
Example 19
Source File: UserController.java    From parker with MIT License 5 votes vote down vote up
@ApiOperation("新增")
@PostMapping("create")
public BaseResult create(@RequestBody @Validated CreateUserDto dto, BindingResult bindingResult){
    BaseResult result = new BaseResult();

    // 校验参数
    if (bindingResult.hasErrors()) {
        FieldError fieldError = bindingResult.getFieldError();
        log.error("新增用户参数错误:{}", fieldError.getDefaultMessage());
        result.setCode(HttpStatus.BAD_REQUEST.value());
        result.setMessage(fieldError.getDefaultMessage());
        return result;
    }

    try {
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        log.info("接收到用户{},{}", user.getUsername(), user.getPassword());
        result.setData(user);
    } catch (Exception e) {
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
        log.error("新增用户错误{}", e.getMessage());
        e.printStackTrace();
    }
    return result;
}
 
Example 20
Source File: BladeRestExceptionTranslator.java    From blade-tool with GNU Lesser General Public License v3.0 4 votes vote down vote up
private R handleError(BindingResult result) {
	FieldError error = result.getFieldError();
	String message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
	return R.fail(ResultCode.PARAM_BIND_ERROR, message);
}