web项目常见错误和解决方案

01、 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

2016-10-10
今天遇到一个奇怪的错误,关于Springmvc的,我明明在Controller方法中写了@ResponseBody,返回一个Map,结果报了406错误。

Paste_Image.png

结果发现,少了一个jar包:

Paste_Image.png

加上去就没事了。

pom.xml

<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-core</artifactId>  
    <version>2.1.0</version>  
</dependency>  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-databind</artifactId>  
    <version>2.1.0</version>  
</dependency>  
<dependency>  
    <groupId>com.fasterxml.jackson.core</groupId>  
    <artifactId>jackson-annotations</artifactId>  
    <version>2.1.0</version>  
</dependency>  

如果还不能解决,就检查在web.xml中配置的spring拦截后缀,不要使用.html后缀!

02、 The method getServletContext() is undefined for the type HttpServletRequest

2016-10-11
这个问题也挺奇怪的,我记得以前都是这么写:

//获取服务器的路径
String dirPath = request.getServletContext().getRealPath("/");

今天,这段代码竟然报错了,说这个方法未定义,我TM也是无语了。

原来,高版本的servlet已经不支持这个方法了。代替方案为:

String dirPath = request.getSession().getServletContext().getRealPath("/");

OK,完美解决。

02、 SpringMVC项目,什么都配好了,可就是进不了Controller,控制台也不报错,页面显示400 ?

原因:
多半是因为数据绑定错误了,比如你用了@PathVariable,@RequestParam等,而前台没有传进来对应的值。那么就会出现这种情况,而且控制台不报错。

也可能是绑定JavaBean的时候,前台传过来一个String类型的日期,而JavaBean中的日期是Date格式的,它转不了,那么也可能会出现这个情况。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容