创建bean时出错'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'

时间:2016-02-11 19:18:17

标签: spring maven spring-mvc

我正在尝试使用Maven和Intellij Idea创建一个Web应用程序。测试在.war文件中安装正常。但是当我尝试用码头引用我的休息时,我有很多错误情况:

  

创建名为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'的bean在ServletContext资源[/WEB-INF/rest-spring.xml]中定义时出错:bean的初始化失败;   
嵌套异常是java.lang.NoClassDefFoundError:com / fasterxml / jackson / core / JsonProcessingException

这是休息模块文件: 的 Web.xml中

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>restDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--the location of the spring context configuration file-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/rest-spring.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>restDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

其余-spring.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:jdbc="http://www.springframework.org/schema/jdbc"
       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/jdbc
       http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">


    <mvc:annotation-driven/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:model.properties</value>
                <value>classpath:database.properties</value>
                <value>classpath:automobile.properties</value>
            </list>
        </property>
    </bean>


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath*:create-tables-model.sql"/>
        <jdbc:script location="classpath*:create-tables-automobile.sql"/>
        <jdbc:script location="classpath*:data-script.sql"/>
    </jdbc:initialize-database>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

    <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json" />
        <property name="prettyPrint" value="true" />
    </bean>

    <bean id="modelDao" class="com.dao.ModelDaoImpl">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="automobileDao" class="com.dao.AutomobileDaoImpl">
        <constructor-arg ref="dataSource" />
    </bean>

    <bean id="modelService" class="com.service.ModelServiceImpl">
        <property name = "modelDao" ref = "modelDao"/>
    </bean>
    <bean id="automobileService" class="com.service.AutomobileServiceImpl">
        <property name = "automobileDao" ref = "automobileDao"/>
    </bean>
    <context:component-scan base-package="com.rest"/>
</beans>

ModelRestController

package com.rest;

import com.dto.ModelDto;
import com.general.Model;
import com.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
/**
 * Created by kohtpojiep on 23.01.16.
 */
@RestController
public class ModelRestController
{
    private static final Logger LOGGER = LogManager.getLogger();

    @Autowired
    private ModelService modelService;

    @RequestMapping(value="/models", method = RequestMethod.GET)
    public @ResponseBody List<Model> getAllModels()
    {
        LOGGER.debug("Getting all models");
        return modelService.getAllModels();
    }

    @RequestMapping(value="/model", method = RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.CREATED)
    public @ResponseBody Integer addModel(@RequestBody Model model)
    {
        LOGGER.debug("Adding model modelName = {}", model.getModelName());
        return modelService.addModel(model);
    }

    @RequestMapping (value="/model/{modelId}/{modelName}", method=RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public @ResponseBody void updateModel(@PathVariable(value="modelId") Integer modelId,
                                          @PathVariable(value="modelName") String modelName)
    {
        LOGGER.debug("Updating model modelId = {}", modelId);
        modelService.updateModel(new Model(modelId,modelName));
    }

    @RequestMapping (value="/model/{modelName}", method = RequestMethod.DELETE)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public @ResponseBody void deleteModelByName(@PathVariable(value = "modelName") String modelName)
    {
        LOGGER.debug("Deleting model modelName= {}",modelName);
        modelService.deleteModelByName(modelName);
    }

    @RequestMapping (value="/modelsdto", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody
    ModelDto getModelsDto()
    {
        LOGGER.debug("Getting models Dto");
        return modelService.getModelDto();
    }

}

AutomobileRestController

package com.rest;

import com.dto.AutomobileDto;
import com.general.Automobile;
import com.service.AutomobileService;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
/**
 * Created by kohtpojiep on 02.02.16.
 */
@RestController
public class AutomobileRestController
{
    private static final Logger LOGGER = LogManager.getLogger();

    @Autowired
    AutomobileService automobileService;

    private static LocalDate convertToLocalDate(String date)
    {
        DateTimeFormatter formattedDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        return formattedDate.parseLocalDate(date);
    }

    @RequestMapping(value = "/automobiles", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public @ResponseBody List<Automobile> getAllAutomobiles()
    {
        LOGGER.debug("Getting all automobiles");
        return automobileService.getAllAutomobiles();
    }

    @RequestMapping(value = "/automobile", method = RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.CREATED)
    public @ResponseBody Integer addAutomobile (@RequestBody Automobile automobile)
    {
        LOGGER.debug("Adding automobile modelName = {}",automobile.getModelName());
        return automobileService.addAutomobile(automobile);
    }

    @RequestMapping(value = "/automobile/update", method = RequestMethod.PUT)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public @ResponseBody void updateAutomobile (@RequestBody Automobile automobile)
    {
        LOGGER.debug("Updating automobile automobileId = {}", automobile.getAutomobileId());
        automobileService.updateAutomobile(automobile);
    }

    @RequestMapping(value = "/automobile/{automobileId}", method = RequestMethod.DELETE)
    @ResponseStatus(value = HttpStatus.NO_CONTENT)
    public @ResponseBody void depeteAutomobile (@PathVariable (value="automobileId")
                                                Integer automobileId)
    {
        LOGGER.debug("Deleting automobile automobileId = {}",automobileId);
        automobileService.deleteAutomobileById(automobileId);
    }

    @RequestMapping(value = "/automobiles/date-sort", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public @ResponseBody List<Automobile> getAutomobilesSortedByDate (@RequestParam(value="firstDate")
    String firstDate, @RequestParam (value="lastDate") String lastDate)
    {
        LOGGER.debug("Getting automobiles sorted by date:\n");
        return automobileService.getAutomobilesSortedByDate(
                convertToLocalDate(firstDate),convertToLocalDate(lastDate));
    }

    @RequestMapping(value = "/automobilesdto", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.OK)
    public @ResponseBody
    AutomobileDto getAutomobileDto()
    {
        LOGGER.debug("Getting automobile DTO");
        return automobileService.getAutomobileDto();
    }
}

的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">
    <parent>
        <artifactId>usermanagement</artifactId>
        <groupId>com.epam.brest.course2015</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>app-rest</artifactId>
    <name>${project.artifactId}</name>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.epam.brest.course2015</groupId>
            <artifactId>app-service</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
                <configuration>
                    <stopPort>9091</stopPort>
                    <stopKey>STOP</stopKey>
                    <webAppConfig>
                        <contextPath>/rest</contextPath>
                        <allowDuplicateFragmentNames>true</allowDuplicateFragmentNames>
                    </webAppConfig>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>8081</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

我看到类似情况的解决方案:人们提供更改已使用框架的版本和更高的组件扫描搜索级别,但它对我不起作用。

4 个答案:

答案 0 :(得分:3)

为您的POM添加依赖关系:

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

在父POM中使用依赖关系管理时,无需指定版本。

答案 1 :(得分:3)

解决!问题是我不仅使用jackson-core依赖,而且还使用jackson数据绑定,数据类型和注释,这些依赖关系有不同的版本:2.7.1用于“核心”,2.4.3用于其他版本。现在我对所有这些版本使用相同的版本,因此添加依赖性会产生影响。谢谢你的帮助!)

答案 2 :(得分:1)

我有同样的问题。我使用的是春季版本5.0.5.RELEASE和杰克逊核心版本2.4.3。

我将Jackson核心升级到2.9.5,现在可以正常工作:异常“创建名称为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter的bean出错”,我的其余服务正常工作。 / p>

在我的pom.xml中之前:

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

之后:

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

我希望它会有所帮助。

答案 3 :(得分:0)

是的。使用2.9.5版可以:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.5</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>       
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>        
        </exclusion>
    </exclusions>
</dependency>           

我正在使用spring-webmvc:5.0.12.RELEASE,如果有帮助的话。