搜尋

首頁  >  問答  >  主體

java - springmvc配置json時報錯Error creating bean

spingmvc 返回json數據時報錯,spingmvc啟動失敗

严重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:306)
at java.lang.Thread.run(Thread.java:745)

spingmvc-servlet.xml配置如下:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.ResourceHttpMessageConverter">
            </bean>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

pom.xml依賴

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

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.5</version>
        </dependency>

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

spring版本是4.x
controller代碼如下

@Controller
@RequestMapping("/json")
public class UserController {
    
    //用户注册
    @RequestMapping(value = "/register.do")
    @ResponseBody
    public Map<String,Object> register() throws IOException {  
        
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("xx", "ok");
        return map;
    }
}

瀏覽器輸入

http://localhost:8080/MybatisDemo/json/register.do

後台就提示啟動springmvc失敗,說是找不json對應的類,可是相關的依賴我都添加了啊。。
前台提示Servlet.init() for servlet springmvc threw exception,忙活半天了。。網上的方案都不能解決.

PHP中文网PHP中文网2804 天前989

全部回覆(2)我來回復

  • 高洛峰

    高洛峰2017-04-18 10:56:51

    有沒有多個版本的jackson-core???

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:56:51

    一般加json-all一個就行了。你試著把其他的包包去掉。加上這個試下。

    回覆
    0
  • 取消回覆