org.springframework.web.bind.ServletRequestBindingException Java Examples

The following examples show how to use org.springframework.web.bind.ServletRequestBindingException. 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: SmsCodeFilter.java    From FEBS-Security with Apache License 2.0 6 votes vote down vote up
private void validateSmsCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobile = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "mobile");
    ValidateCode codeInSession = (ValidateCode) sessionStrategy.getAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在,请重新发送!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);
        throw new ValidateCodeException("验证码已过期,请重新发送!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_SMS_CODE + mobile);

}
 
Example #2
Source File: SmsCodeFilter.java    From imooc-security with Apache License 2.0 6 votes vote down vote up
private void validate(ServletWebRequest request) throws ServletRequestBindingException {
    ValidateCode codeInSession = (ValidateCode) sessionStrategy.getAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(), "smsCode");
    if(StringUtils.isBlank(codeInRequest)){
        throw new ValidateCodeException("验证码不能为空");
    }
    if(codeInSession==null){
        throw new ValidateCodeException("验证码不存在");
    }
    if(codeInSession.isExpired()){
        sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
        throw new ValidateCodeException("验证码过期");
    }
    if(!StringUtils.equals(codeInSession.getCode(),codeInRequest)){
        throw new ValidateCodeException("验证码不匹配");
    }
    sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_CODE);
}
 
Example #3
Source File: SmsCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #4
Source File: ValidateCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #5
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #6
Source File: ValidateCodeFilter.java    From imooc-security with Apache License 2.0 6 votes vote down vote up
private void validate(ServletWebRequest request) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
    String codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(), "imageCode");
    if(StringUtils.isBlank(codeInRequest)){
        throw new ValidateCodeException("验证码不能为空");
    }
    if(codeInSession==null){
        throw new ValidateCodeException("验证码不存在");
    }
    if(codeInSession.isExpired()){
        sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
        throw new ValidateCodeException("验证码过期");
    }
    if(!StringUtils.equals(codeInSession.getCode(),codeInRequest)){
        throw new ValidateCodeException("验证码不匹配");
    }
    sessionStrategy.removeAttribute(request,ValidateCodeController.SESSION_KEY_IMAGE);
}
 
Example #7
Source File: RESTFilter.java    From airsonic-advanced with GNU General Public License v3.0 6 votes vote down vote up
private void handleException(Throwable x, HttpServletRequest request, HttpServletResponse response) {
    if (x instanceof NestedServletException && x.getCause() != null) {
        x = x.getCause();
    }

    SubsonicRESTController.ErrorCode code = (x instanceof ServletRequestBindingException) ? SubsonicRESTController.ErrorCode.MISSING_PARAMETER : SubsonicRESTController.ErrorCode.GENERIC;
    String msg = getErrorMessage(x);

    // This happens often and outside of the control of the server, so
    // we catch Tomcat/Jetty "connection aborted by client" exceptions
    // and display a short error message.
    boolean shouldCatch = Util.isInstanceOfClassName(x, "org.apache.catalina.connector.ClientAbortException");
    if (shouldCatch) {
        LOG.info("{}: Client unexpectedly closed connection while loading {} ({})", request.getRemoteAddr(),
                Util.getAnonymizedURLForRequest(request), x.getMessage());
    } else {
        LOG.warn("Error in REST API", x);

        try {
            jaxbWriter.writeErrorResponse(request, response, code, msg);
        } catch (Exception e) {
            LOG.error("Failed to write error response.", e);
        }
    }

}
 
Example #8
Source File: StreamController.java    From airsonic-advanced with GNU General Public License v3.0 6 votes vote down vote up
private VideoTranscodingSettings createVideoTranscodingSettings(MediaFile file, HttpServletRequest request)
        throws ServletRequestBindingException {
    Integer existingWidth = file.getWidth();
    Integer existingHeight = file.getHeight();
    Integer maxBitRate = ServletRequestUtils.getIntParameter(request, "maxBitRate");
    int timeOffset = ServletRequestUtils.getIntParameter(request, "timeOffset", 0);
    double defaultDuration = file.getDuration() == null ? Double.MAX_VALUE : file.getDuration() - timeOffset;
    double duration = ServletRequestUtils.getDoubleParameter(request, "duration", defaultDuration);
    boolean hls = ServletRequestUtils.getBooleanParameter(request, "hls", false);

    Dimension dim = getRequestedVideoSize(request.getParameter("size"));
    if (dim == null) {
        dim = getSuitableVideoSize(existingWidth, existingHeight, maxBitRate);
    }

    return new VideoTranscodingSettings(dim.width, dim.height, timeOffset, duration, hls);
}
 
Example #9
Source File: SmsCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #10
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example #11
Source File: ValidateCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #12
Source File: DefaultAnnotationHandlerMapping.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Validate the given type-level mapping metadata against the current request,
 * checking HTTP request method and parameter conditions.
 * @param mapping the mapping metadata to validate
 * @param request current HTTP request
 * @throws Exception if validation failed
 */
protected void validateMapping(RequestMapping mapping, HttpServletRequest request) throws Exception {
	RequestMethod[] mappedMethods = mapping.method();
	if (!ServletAnnotationMappingUtils.checkRequestMethod(mappedMethods, request)) {
		String[] supportedMethods = new String[mappedMethods.length];
		for (int i = 0; i < mappedMethods.length; i++) {
			supportedMethods[i] = mappedMethods[i].name();
		}
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), supportedMethods);
	}

	String[] mappedParams = mapping.params();
	if (!ServletAnnotationMappingUtils.checkParameters(mappedParams, request)) {
		throw new UnsatisfiedServletRequestParameterException(mappedParams, request.getParameterMap());
	}

	String[] mappedHeaders = mapping.headers();
	if (!ServletAnnotationMappingUtils.checkHeaders(mappedHeaders, request)) {
		throw new ServletRequestBindingException("Header conditions \"" +
				StringUtils.arrayToDelimitedString(mappedHeaders, ", ") +
				"\" not met for actual request");
	}
}
 
Example #13
Source File: ValidateCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #14
Source File: SmsCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    String smsCodeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");
    String mobileInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "smsCode");

    SmsCode codeInSession = (SmsCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_SMS_CODE + mobileInRequest);

    if (StringUtils.isBlank(smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), smsCodeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #15
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void controllerAdvice() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
	resolver.setApplicationContext(ctx);
	resolver.afterPropertiesSet();

	ServletRequestBindingException ex = new ServletRequestBindingException("message");
	assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex));

	assertEquals(400, this.servletResponse.getStatus());
	assertEquals("error content", this.servletResponse.getContentAsString());
	assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader"));
}
 
Example #16
Source File: ResponseEntityExceptionHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
	StaticWebApplicationContext ctx = new StaticWebApplicationContext();
	ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
	ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
	ctx.refresh();

	DispatcherServlet servlet = new DispatcherServlet(ctx);
	servlet.init(new MockServletConfig());
	try {
		servlet.service(this.servletRequest, this.servletResponse);
	}
	catch (ServletException ex) {
		assertTrue(ex.getCause() instanceof IllegalStateException);
		assertTrue(ex.getCause().getCause() instanceof ServletRequestBindingException);
	}
}
 
Example #17
Source File: ValidateCodeFilter.java    From SpringAll with MIT License 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, ValidateController.SESSION_KEY_IMAGE_CODE);

}
 
Example #18
Source File: ImageCodeFilter.java    From FEBS-Security with Apache License 2.0 6 votes vote down vote up
private void validateCode(ServletWebRequest servletWebRequest) throws ServletRequestBindingException {
    ImageCode codeInSession = (ImageCode) sessionStrategy.getAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);
    String codeInRequest = ServletRequestUtils.getStringParameter(servletWebRequest.getRequest(), "imageCode");

    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("验证码不能为空!");
    }
    if (codeInSession == null) {
        throw new ValidateCodeException("验证码不存在,请重新发送!");
    }
    if (codeInSession.isExpire()) {
        sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);
        throw new ValidateCodeException("验证码已过期,请重新发送!");
    }
    if (!StringUtils.equalsIgnoreCase(codeInSession.getCode(), codeInRequest)) {
        throw new ValidateCodeException("验证码不正确!");
    }
    sessionStrategy.removeAttribute(servletWebRequest, FebsConstant.SESSION_KEY_IMAGE_CODE);

}
 
Example #19
Source File: UserSettingsController.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
private User getUser(HttpServletRequest request) throws ServletRequestBindingException {
    Integer userIndex = ServletRequestUtils.getIntParameter(request, "userIndex");
    if (userIndex != null) {
        List<User> allUsers = securityService.getAllUsers();
        if (userIndex >= 0 && userIndex < allUsers.size()) {
            return allUsers.get(userIndex);
        }
    }
    return null;
}
 
Example #20
Source File: ValidateCodeServiceImpl.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(ServletWebRequest request) {
	String imageRes = redisTemplate.opsForValue().get(SESSION_KEY);
	ImageCode codeInSession = JSONUtil.toBean(imageRes, ImageCode.class);
	String codeInRequest;
	try {
		codeInRequest = ServletRequestUtils.getStringParameter(request.getRequest(),
				"imageCode");
	} catch (ServletRequestBindingException e) {
		throw new ValidateCodeException("获取验证码的值失败");
	}

	if (StrUtil.isBlank(codeInRequest)) {
		throw new ValidateCodeException("验证码的值不能为空");
	}

	if (codeInSession == null) {
		throw new ValidateCodeException("验证码不存在");
	}

	if (codeInSession.isExpried()) {
		redisTemplate.delete(SESSION_KEY);
		throw new ValidateCodeException("验证码已过期");
	}

	if (!StrUtil.equals(codeInSession.getCode(), codeInRequest)) {
		throw new ValidateCodeException("验证码不匹配");
	}

	redisTemplate.delete(SESSION_KEY);
}
 
Example #21
Source File: DefaultHandlerExceptionResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void handleServletRequestBindingException() {
	String message = "Missing required value - header, cookie, or pathvar";
	ServletRequestBindingException ex = new ServletRequestBindingException(message);
	ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
	assertNotNull("No ModelAndView returned", mav);
	assertTrue("No Empty ModelAndView returned", mav.isEmpty());
	assertEquals("Invalid status code", 400, response.getStatus());
}
 
Example #22
Source File: ResponseEntityExceptionHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ResponseEntity<Object> handleServletRequestBindingException(
		ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

	headers.set("someHeader", "someHeaderValue");
	return handleExceptionInternal(ex, "error content", headers, status, request);
}
 
Example #23
Source File: MatrixVariablesMethodArgumentResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test(expected = ServletRequestBindingException.class)
public void resolveArgumentMultipleMatches() throws Exception {
	getVariablesFor("var1").add("colors", "red");
	getVariablesFor("var2").add("colors", "green");
	MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class);

	this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null);
}
 
Example #24
Source File: ExceptionHandlers.java    From spring-boot-aop with MIT License 5 votes vote down vote up
@ExceptionHandler(ServletRequestBindingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorResponse handle(final ServletRequestBindingException ex) {
    log.error("Missing parameter", ex);
    return new ErrorResponse("MISSING_PARAMETER", ex.getMessage());
}
 
Example #25
Source File: ExceptionControllerAdvice.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 拦截绑定参数异常
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public MessageResult myErrorHandler(ServletRequestBindingException e) {
    e.printStackTrace();
    log.info(">>>拦截绑定参数异常>>",e);
    MessageResult result = MessageResult.error(3000, "参数绑定错误(如:必须参数没传递)!");
    return result;
}
 
Example #26
Source File: RandomPlayQueueController.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
private List<MusicFolder> getMusicFolders(HttpServletRequest request) throws ServletRequestBindingException {
    String username = securityService.getCurrentUsername(request);
    Integer selectedMusicFolderId = ServletRequestUtils.getRequiredIntParameter(request, "musicFolderId");
    if (selectedMusicFolderId == -1) {
        selectedMusicFolderId = null;
    }
    return settingsService.getMusicFoldersForUser(username, selectedMusicFolderId);
}
 
Example #27
Source File: ValidateCodeServiceImpl.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 验证验证码
 */
@Override
public void validate(HttpServletRequest request) {
    String deviceId = request.getParameter("deviceId");
    if (StringUtils.isBlank(deviceId)) {
        throw new ValidateCodeException("请在请求参数中携带deviceId参数");
    }
    String code = this.getCode(deviceId);
    String codeInRequest;
    try {
        codeInRequest = ServletRequestUtils.getStringParameter(request, "validCode");
    } catch (ServletRequestBindingException e) {
        throw new ValidateCodeException("获取验证码的值失败");
    }
    if (StringUtils.isBlank(codeInRequest)) {
        throw new ValidateCodeException("请填写验证码");
    }

    if (code == null) {
        throw new ValidateCodeException("验证码不存在或已过期");
    }

    if (!StringUtils.equals(code, codeInRequest.toLowerCase())) {
        throw new ValidateCodeException("验证码不正确");
    }

    this.remove(deviceId);
}
 
Example #28
Source File: IValidateCodeServiceImpl.java    From open-capacity-platform with Apache License 2.0 5 votes vote down vote up
/**
 * 验证验证码
 */
@Override
public void validate(HttpServletRequest request) {
	String deviceId = request.getParameter("deviceId");
	if (StringUtils.isBlank(deviceId)) {
		throw new RuntimeException("请在请求参数中携带deviceId参数");
	}
	String code = this.getCode(deviceId);
	String codeInRequest;
	try {
		codeInRequest = ServletRequestUtils.getStringParameter(request, "validCode");
	} catch (ServletRequestBindingException e) {
		throw new AuthenticationException ("获取验证码的值失败"){};
	}
	if (StringUtils.isBlank(codeInRequest)) {
		throw new AuthenticationException ("请填写验证码"){};
	}

	if (code == null) {
		throw new AuthenticationException ("验证码不存在或已过期"){};
	}

	if (!StringUtils.equals(code, codeInRequest)) {
		throw new AuthenticationException ("验证码不正确"){};
	}

	this.remove(deviceId);
}
 
Example #29
Source File: ExceptionsHandler.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public ResponseEntity bindExceptionHandler(ServletRequestBindingException ex) {
    log.warn("catch bindExceptionHandler", ex);

    Map<String, Object> map = new HashMap<>();
    //  map.put("exception", frontException);
    map.put("errorMessage", ex.getMessage());
    map.put("code", 400);
    log.warn("bindExceptionHandler return:{}", JsonUtils.toJSONString(map));
    return ResponseEntity.status(400).body(map);
}
 
Example #30
Source File: ExceptionControllerAdvice.java    From ZTuoExchange_framework with MIT License 5 votes vote down vote up
/**
 * 拦截绑定参数异常
 *
 * @param e
 * @return
 */
@ResponseBody
@ExceptionHandler(value = ServletRequestBindingException.class)
public MessageResult myErrorHandler(ServletRequestBindingException e) {
    e.printStackTrace();
    log.info(">>>拦截绑定参数异常>>",e);
    MessageResult result = MessageResult.error(3000, "参数绑定错误(如:必须参数没传递)!");
    return result;
}