开发者社区> 问答> 正文

MVC中bindingResult报java.lang.NullPointer报错

springMVC中bindingResult报java.lang.NullPointerException?

"

问题描述

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>
"

展开
收起
因为相信,所以看见。 2020-05-27 10:02:27 1182 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    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()方法没有成功执行完成,导致数据绑定报错!

    "
    2020-05-27 17:36:34
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
如何通过 Serverless 提高 Java 微服务治理效 立即下载
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
Java Spring Boot开发实战系列课程【第7讲】:Spring Boot 2.0安全机制与MVC身份验证实战(Java面试题) 立即下载