第六章 跟着老师写工具类,但是基本都是报HTTP Status 500 – Internal Server Error,实在无法查出原因

来源:6-6 获取Spring上下文工具-ApplicationContextHelper开发

慕村1342571

2018-06-02

代码都一样滴,但就是报下面的错误,得不要想要的结果

HTTP Status 500 – Internal Server Error

Type Exception Report

Message Request processing failed; nested exception is java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:986)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)


@RequestMapping("/validate.json")

@ResponseBody
public JsonData validate(TestVo vo) throws ParamException {
   log.info("validate");
   SysAclModuleMapper moduleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
   SysAclModule module = moduleMapper.selectByPrimaryKey(1);
   log.info(JsonMapper.obj2String(module));
   BeanValidator.check(vo);

   return JsonData.success("test validate");
}

写回答

10回答

慕村1342571

提问者

2018-07-04

解决了

0
0

慕村1342571

提问者

2018-07-04

如果是正常的返回就没有问题,但是自定义的异常类没有起作用

0
0

慕村1342571

提问者

2018-07-04

HTTP Status 500 – Internal Server Error

Type Exception Report

Message Request processing failed; nested exception is com.mmall.exception.PermissionException: test exception

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.mmall.exception.PermissionException: test exception
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Root Cause

com.mmall.exception.PermissionException: test exception
com.mmall.controller.TestController.hellot3(TestController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/8.5.24


0
0

慕村1342571

提问者

2018-07-04

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

   <!--占位参数 -->
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
       <property name="ignoreUnresolvablePlaceholders" value="true" />
       <property name="locations">
           <list>
               <value>classpath:settings.properties</value>
           </list>
       </property>
   </bean>

   <!-- 数据源 -->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
       <property name="driverClassName" value="${db.driverClassName}" />
       <property name="url" value="${db.url}" />
       <property name="username" value="${db.username}" />
       <property name="password" value="${db.password}" />
       <property name="initialSize" value="3" />
       <property name="minIdle" value="3" />
       <property name="maxActive" value="20" />
       <property name="maxWait" value="60000" />
       <property name="filters" value="stat,wall" />
   </bean>

   <!-- sql会话工厂 、 mybatis的mapper位置 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="configLocation" value="classpath:mybatis-config.xml" />
       <property name="dataSource" ref="dataSource" />
       <property name="mapperLocations" value="classpath:mapper/*.xml" />
   </bean>
   <!-- mybatis的dao操作 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.mmall.dao" />
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
   </bean>

   <!-- tx 事务管理-->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
       <property name="dataSource" ref="dataSource" />
   </bean>

   <tx:annotation-driven transaction-manager="transactionManager" />

   <!-- druid的stat和wall过滤器 -->
   <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
       <property name="slowSqlMillis" value="3000" />
       <property name="logSlowSql" value="true" />
       <property name="mergeSql" value="true" />
   </bean>

   <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
       <property name="dbType" value="mysql" />
   </bean>
</beans>

0
0

慕村1342571

提问者

2018-07-04

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.cdunicom</groupId>
   <artifactId>permission</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>permission Maven Webapp</name>
   <!-- FIXME change it to the project's website -->
   <url>http://www.10010.com</url>

   <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       <maven.compiler.source>1.7</maven.compiler.source>
       <maven.compiler.target>1.7</maven.compiler.target>
       <spring.version>4.3.17.RELEASE</spring.version>
       <logback.versiion>1.1.11</logback.versiion>
       <slf4j.version>1.7.22</slf4j.version>
       <mybatis.version>3.4.1</mybatis.version>
       <mybatispring.version>1.3.1</mybatispring.version>
       <jackson.version>2.9.5</jackson.version>
       <druid.version>1.1.10</druid.version>
       <mysql.version>5.1.46</mysql.version>
       <lombok.versioin>1.16.18</lombok.versioin>
   </properties>

   <dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.11</version>
           <scope>test</scope>
       </dependency>

       <!-- Spring Beans -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <!-- Spring Context -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <!-- Spring MVC + Spring WEB-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-web</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-webmvc</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <!-- Spring JDBC-->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-jdbc</artifactId>
           <version>${spring.version}</version>
       </dependency>

       <!-- mybatis -->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>${mybatis.version}</version>
       </dependency>
       <!-- mybatis -->
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>${mybatispring.version}</version>
       </dependency>

       <!-- druid -->
       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid</artifactId>
           <version>${druid.version}</version>
       </dependency>

       <!-- mysql -->
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>${mysql.version}</version>
       </dependency>

       <!-- lombok -->
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>${lombok.versioin}</version>
       </dependency>

       <!-- Jackson -->
       <dependency>
           <groupId>com.fasterxml.jackson.datatype</groupId>
           <artifactId>jackson-datatype-guava</artifactId>
           <version>${jackson.version}</version>
       </dependency>

       <!-- logback -->
       <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-core</artifactId>
           <version>${logback.versiion}</version>
       </dependency>
       <dependency>
           <groupId>ch.qos.logback</groupId>
           <artifactId>logback-classic</artifactId>
           <version>${logback.versiion}</version>
       </dependency>
       <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-api</artifactId>
           <version>${slf4j.version}</version>
       </dependency>

       <!-- jsp api -->
       <dependency>
           <groupId>org.apache.tomcat</groupId>
           <artifactId>jsp-api</artifactId>
           <version>6.0.36</version>
       </dependency>

   </dependencies>

   <build>
       <finalName>permission</finalName>
       <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
           <plugins>
               <plugin>
                   <artifactId>maven-clean-plugin</artifactId>
                   <version>3.0.0</version>
               </plugin>
               <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
               <plugin>
                   <artifactId>maven-resources-plugin</artifactId>
                   <version>3.0.2</version>
               </plugin>
               <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <version>3.7.0</version>
               </plugin>
               <plugin>
                   <artifactId>maven-surefire-plugin</artifactId>
                   <version>2.20.1</version>
               </plugin>
               <plugin>
                   <artifactId>maven-war-plugin</artifactId>
                   <version>3.2.0</version>
               </plugin>
               <plugin>
                   <artifactId>maven-install-plugin</artifactId>
                   <version>2.5.2</version>
               </plugin>
               <plugin>
                   <artifactId>maven-deploy-plugin</artifactId>
                   <version>2.8.2</version>
               </plugin>
           </plugins>
       </pluginManagement>
   </build>
</project>

0
0

慕村1342571

提问者

2018-07-04

web.xml:

<!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
   <display-name>Archetype Created Web Application</display-name>

   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <!-- Spring beans 配置文件所在目录 -->
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <!-- 指定classpath则从resources目录中查找,若没有指定则从WEB-INF中搜索 -->
       <param-value>classpath:applicationContext.xml</param-value>
   </context-param>

   <!-- Spring mvc 配置 -->
   <servlet>
       <servlet-name>spring</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>spring</servlet-name>
       <!-- 拦截所有请求 -->
       <!-- / 和 /*的区别:/可以匹配到路径型如/login、/hello等但是匹配不到带后缀型的/*.jsp,
           路径型在经过视图解析器后就不会返回到DispatcherServlet;
           /*可以匹配到路径型和后缀型所有的地址(/*.html、/*.jsp、/*.js、/*.css);
           在处理请求时处理/就可以了不需要处理.jsp的请求,但是在处理编码的时候需要全部过滤
       -->
       <url-pattern>/</url-pattern>
   </servlet-mapping>

   <!-- Encoding 编码过滤器-->
   <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
           <param-name>encoding</param-name>
           <param-value>UTF-8</param-value>
       </init-param>
       <init-param>
           <param-name>forceEncoding</param-name>
           <param-value>true</param-value>
       </init-param>
   </filter>

   <filter-mapping>
       <filter-name>encodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

   <!-- druid servlet -->
   <servlet>
       <servlet-name>DruidStatServlet</servlet-name>
       <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
       <!-- 根据StatViewServlet继承的ResourceServlet中的静态成员变量PARAM_NAME_USERNAME和PARAM_NAME_PASSWORD进行设置 -->
       <init-param>
           <param-name>loginUsername</param-name>
           <param-value>druid</param-value>
       </init-param>
       <init-param>
           <param-name>loginPassword</param-name>
           <param-value>druid</param-value>
       </init-param>
   </servlet>
   <servlet-mapping>
       <servlet-name>DruidStatServlet</servlet-name>
       <url-pattern>/sys/druid/*</url-pattern> <!-- 增加前缀可以方便对所有sys下的功能连接进行权限校验拦截(比如进行一次登录),不限于druid的请求 -->
   </servlet-mapping>

   <!-- druid Web filter -->
   <filter>
       <filter-name>DruidWebStatFilter</filter-name>
       <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
       <init-param>
           <param-name>exclusions</param-name>
           <param-value>*.js,*.css,*.jpg,*.png,*.ico,*.gif,/sys/druid/*</param-value>
       </init-param>
   </filter>
   <filter-mapping>
       <filter-name>DruidWebStatFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>

   <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

</web-app>

0
0

慕村1342571

提问者

2018-07-04

SpringExceptionResolver:

package com.mmall.common;


import com.mmall.exception.PermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {

   // 异常处理
   @Override
   public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
       String url = request.getRequestURL().toString();
       ModelAndView mv;
       String defaultMsg = "System error";

       // .json, .page 确定是json请求还是页面请求
       // 要求项目中所有请求json数据,都使用.json结尾
       if(url.endsWith(".json")){
           if (ex instanceof PermissionException){
               JsonData result = JsonData.fail(ex.getMessage());
               // jsonView和spring-servlet.xml中的 <bean id="jsonView" ...定义对应
               mv = new ModelAndView("jsonView", result.toMap());
           }else {
               log.error("unknow json exception, url:" + url, ex);
               JsonData result = JsonData.fail(defaultMsg);
               mv = new ModelAndView("jsonView", result.toMap());
           }
       }else if(url.endsWith(".page")){ // 要求项目中所有请求page页面,都使用.page结尾
           log.error("unknow page exception, url:" + url, ex);
           JsonData result = JsonData.fail(defaultMsg);
           mv = new ModelAndView("exception", result.toMap());
       }else{
           log.error("unknow exception, url:" + url, ex);
           JsonData result = JsonData.fail(defaultMsg);
           mv = new ModelAndView("jsonView", result.toMap());
       }
       return null;
   }
}

0
1
慕村1342571
又比对了下代码,在视频8:09分的时候返回mv,忽略了还是用滴null,把自己坑了
2018-07-04
共1条回复

慕村1342571

提问者

2018-07-04

TestController.java:


package com.mmall.controller;

import com.mmall.common.JsonData;
import com.mmall.exception.PermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {

   @RequestMapping("/hellot1")
   @ResponseBody
   public String hellot1() {
       log.info("hellot1 Normal String Test");
       return "hello, permission";
   }

   @RequestMapping("/hellot2.json")
   @ResponseBody
   public JsonData hellot2() {
       log.info("hellot2 JsonData Success Test");
       return JsonData.success("hellot2, permission");
   }

   @RequestMapping("/hellot3.json")
   @ResponseBody
   public JsonData hellot3() {
       log.info("hellot3 JsonData PermissionException Test");
       throw new PermissionException("test exception");
   }
}

0
0

慕村1342571

提问者

2018-07-04

重新写了一次,问题依旧 :spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:annotation-config />

    <!-- 启动注解驱动的spring mvc 功能 -->

    <mvc:annotation-driven />


    <!-- 启动包扫描功能 -->

    <context:component-scan base-package="com.mmall.controller" />

    <context:component-scan base-package="com.mmall.service" />

    <!-- RequestMappingHandlerMapping可以实现通过设置RequestMapping找到处理接口,告诉请求可以通过什么代码来处理 -->

    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />-->

    <!-- 全局异常处理类 -->

    <bean class="com.mmall.common.SpringExceptionResolver" />

    <!-- 返回视图渲染,如json数据、jsp页面、PDF文件 -->

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

    <!-- json返回的处理 -->

    <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/views/" />

        <property name="suffix" value=".jsp" />

    </bean>

</beans>


0
0

Jimin

2018-06-02

你好,看你发出来的这个错,是底层框架出现空指针了,怀疑是配置哪里不对,比如spring管理的bean没有被扫描到,mybatis配置错了等等都有可能,你按照这个方向查一下看看,感觉是配置哪里不一致。
如果依旧查不出来,那么把xml相关的配置发出来看一下

0
0

Java开发企业级权限管理系统

源于企业真实Java项目,涉及大量高级技巧,覆盖权限管理开发技术

2261 学习 · 1347 问题

查看课程