Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error creating bean 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'

I'm trying to create a web-app, using Maven and Intellij Idea. Tests work fine as installing in .war-file. But when I try to refer to my rest with jetty, I have lots of error cases:

Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1' defined in ServletContext resource [/WEB-INF/rest-spring.xml]: Initialization of bean failed;
nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException

Here are rest module files: 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>

rest-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>

I saw solutions for similar situations: people offer to change versions of used frameworks and up the search level of component scan, but it doesn't work for me.

like image 619
KOHTPOJIEP Avatar asked Feb 11 '16 19:02

KOHTPOJIEP


3 Answers

Add dependency to your POM:

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

As you use dependency management in parent POM, there is no need to specify version.

like image 80
Karol Lewandowski Avatar answered Oct 26 '22 09:10

Karol Lewandowski


Solved! The problem was I use not only jackson-core dependency, but also jackson databind, datatype and annotations and these dependencies had different versions: 2.7.1 for "core" and 2.4.3 for other ones. Now I use the same version for all of them and thus adding dependency had an affect. Thx for your help!)

like image 34
KOHTPOJIEP Avatar answered Oct 26 '22 09:10

KOHTPOJIEP


I had the same problem. I used spring version 5.0.5.RELEASE and Jackson Core version 2.4.3.

I upgraded Jackson core up to 2.9.5 and it works now : exception "Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter' " dissapeared and my rest service works.

Before in my 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>

After :

    <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>

I hope it will help.

like image 26
Dricom Avatar answered Oct 26 '22 09:10

Dricom