创建bean'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'时出错'
问题描述:
我正尝试使用Maven和Intellij Idea创建一个Web应用程序。测试工作正常,在.war文件中安装。但是当我尝试用jetty引用我的其余部分时,我有很多这种错误情况: 在ServletContext资源中定义名称为'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'的bean时出错[ /WEB-INF/rest-spring.xml]:bean初始化失败;嵌套的例外是java.lang.NoClassDefFoundError:COM/fasterxml /杰克逊/核心/ JsonProcessingException创建bean'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#1'时出错'
这里是休息模块文件: 在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>
我看到的解决方案类似的情况:人们提供了改变使用框架的版本和高达分量的搜索水平扫描,但它不适合我。
答
添加依赖你的POM:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
当您使用在父POM依赖管理,没有必要指定版本。
答
解决!问题是我不仅使用jackson核心依赖项,还使用jackson数据绑定,数据类型和注释,这些依赖项具有不同的版本:2.7.1代表“核心”,2.4.3代表其他版本。现在我为它们全部使用相同的版本,因此添加依赖性会产生影响。 Thx为您的帮助!)
没有工作。我看着我的普通.pom,并且在那里只有版本2.4.3有这种依赖关系(我使用)。我在第一次尝试将版本更改为2.7.1,并在休息.pom中添加依赖项,然后在普通.pom中添加注释代码,然后在休息.pom中添加依赖项。在每次操作之后,我通过终端在项目根目录中进行“mvn clean install”,并尝试使用jetty:在rest模块中重新运行。可能需要提供其他文件的代码? –
KOHTPOJIEP
我并不是指特定的版本,而是加入依赖关系。如果你在父POM中使用依赖管理,它应该可以工作。 –
但不幸的是,它不。现在,我有这样的休息.pom: com.fasterxml.jackson.core 杰克逊核心 ,这在一般.pom: com.fasterxml.jackson.core jackson-核心 2.7。1 之前我在这里问过问题,它有提供 由我以前读过的一些解决方案。 –
KOHTPOJIEP