springMVC中bindingResult报java.lang.NullPointerException?

彪彪
  • 2
新手上路,请多包涵

问题描述

tomcat下部署项目a和b,tomcat先部署a,后部署b。
两个项目分别使用LoginController中的login完成登录,其中xtuser使用@Validated完成前端传入参数userid的校验,a项目可以校验通过,b项目校验时bindingResult报错:Property 'userId' threw exception; nested exception is java.lang.NullPointerException;

但是单独部署b项目,校验又不出任何问题,求大神指点?

相关代码

// LoginController.java

@RequestMapping(value="/login", method = RequestMethod.GET)
public void login(    @Validated({IXtLoginGroup.class}) XtUser xtUser,BindingResult bindingResult
                    ,@RequestParam(value ="token_index", required = true)String token_index
                    , HttpServletRequest request,HttpServletResponse response) throws Exception {
    if(bindingResult.hasFieldErrors()){
        String messageStr=ExceptionMessager.extractMessagesFromExceptionList(bindingResult);
        try {
            Map map=new HashMap();
            map.put("code", 0);//1为正常,0为错误
            map.put("message", "fault");
            Map childMap=new HashMap();
            childMap.put("title", "服务端校验错误");
            childMap.put("errorMsg",messageStr);
            map.put("data",childMap);
            System.out.println( JsonTool.Obj2Json(map));
            ResponseUtil.write(response, JsonTool.Obj2Json(map));
            return;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//XtUser.java

public class XtUser implements java.io.Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @NotEmpty(message="用户账号{notNull}",groups={IXtUserGroup.class,IXtLoginGroup.class})
    private String userId;

}

//spring相关配置

<!-- 默认的注解映射的支持  --> 
<mvc:annotation-driven validator="validator" conversion-service="conversion-service" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="providerClass"  value="org.hibernate.validator.HibernateValidator"/>
    <!--不设置则默认为classpath下的 ValidationMessages.properties -->
    <property name="validationMessageSource" ref="validatemessageSource"/>
</bean>
<bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
    <property name="basename" value="classpath:validatemessages"/>  
    <!-- <property name="useCodeAsDefaultMessage" value="true" /> -->
    <property name="defaultEncoding" value="utf-8" /> 
    <property name="cacheSeconds" value="120"/>  
</bean>
回复
阅读 1.7k
1 个回答
彪彪
  • 2
新手上路,请多包涵
✓ 已被采纳
public void setUserId(String userId) throws Exception {
    this.userId = userId;
    //加密userId,作为界面返回controller的定位参数
    
    this.userId_encoded=SM2Util.encrypt(userId);
    System.out.println("***********"+userId);
    //this.userId_encoded=userId;
}

由于SM2Util错误使用了静态块生成公私钥,导致b项目中公私钥为空,entity中setUserId下SM2Util.encrypt()方法没有成功执行完成,导致数据绑定报错!

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏