spring mvc 常见错误

 

1 返回400,

   —— 请求参数不正确

 

2 返回406, 

HTTP ERROR: 406

NOT_ACCEPTABLE
 ————
@RequestMapping(value = "/user/byAcc",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_XML_VALUE + ";charset=UTF-8")

   —— 请求结果无法正确渲染,请求返回的是xml 就导致springmvc 无法解析了。。。 那就加上jackson-xml:

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>

 

3 直接返回viewName,而不是viewName对应的 view(如jsp,velocity)

  这个通常是因为方法前有@ResponseBody注解导致的,或者方法对应类前有 @RestController,

 

4  jetty 启动的时候出错:

java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.SerializationConfig.withDefaultPrettyPrinter(Lcom/fasterxml/jackson/core/PrettyPrinter;)Lcom/fasterxml/jackson/databind/SerializationConfig;

 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter]: Factory method 'mappingJackson2XmlHttpMessageConverter' threw exception; nested exception is java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.SerializationConfig.withDefaultPrettyPrinter(Lcom/fasterxml/jackson/core/PrettyPrinter;)Lcom/fasterxml/jackson/databind/SerializationConfig;
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 145 more
    Caused by: java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.SerializationConfig.withDefaultPrettyPrinter(Lcom/fasterxml/jackson/core/PrettyPrinter;)Lcom/fasterxml/jackson/databind/SerializationConfig;
        at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:82)
        at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:63)
        at com.fasterxml.jackson.dataformat.xml.XmlMapper.<init>(XmlMapper.java:49)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78)
        at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:514)
        at org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter.<init>(MappingJackson2XmlHttpMessageConverter.java:49)
        at org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2XmlHttpMessageConverterConfiguration.mappingJackson2XmlHttpMessageConverter(JacksonHttpMessageConvertersConfiguration.java:84)
        at org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2XmlHttpMessageConverterConfiguration$$EnhancerBySpringCGLIB$$f2b9ded.CGLIB$mappingJackson2XmlHttpMessageConverter$0(<generated>)
        at org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2XmlHttpMessageConverterConfiguration$$EnhancerBySpringCGLIB$$f2b9ded$$FastClassBySpringCGLIB$$911122bf.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
        at org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2XmlHttpMessageConverterConfiguration$$EnhancerBySpringCGLIB$$f2b9ded.mappingJackson2XmlHttpMessageConverter(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        ... 146 more

 

原因是版本不匹配, spring MVC 4.3.6.RELEASE 和 com.fasterxml.jackson.core 的2.8.8 不兼容,或者jackson-annotations 和 jackson-core 的版本没有一致。 (2.8.8 是 我手动添加的com.fasterxml.jackson.core 的最新版本),把 com.fasterxml.jackson.core  版本改为 2.5.0 就ok了!! 

 

5 ModelAndView 的view设置没有生效,

@RequestMapping(value="/index_html", method=RequestMethod.GET)
public ModelAndView indexView(){
System.out.println("++++++++++++++");
ModelAndView mv = new ModelAndView("index");
mv.setViewName("abc"); // 期望解析到abc.jsp , 结果是index_html.jsp ,然后就出现了 404 ..

return mv;
}

 应该是import org.springframework.web.servlet.ModelAndView; 

我居然不小心引入了import org.springframework.web.portlet.ModelAndView;

参考http://bbs.csdn.net/topics/390543354

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值