2016-02-11 67 views
2

我正嘗試使用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> 

我看到的解決方案類似的情況:人們提供了改變使用框架的版本和高達分量的搜索水平掃描,但它不適合我。

回答

3

添加依賴你的POM:

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

當您使用在父POM依賴管理,沒有必要指定版本。

+0

沒有工作。我看着我的普通.pom,並且在那裏只有版本2.4.3有這種依賴關係(我使用)。我在第一次嘗試將版本更改爲2.7.1,並在休息.pom中添加依賴項,然後在普通.pom中添加註釋代碼,然後在休息.pom中添加依賴項。在每次操作之後,我通過終端在項目根目錄中進行「mvn clean install」,並嘗試使用jetty:在rest模塊中重新運行。可能需要提供其他文件的代碼? – KOHTPOJIEP

+0

我並不是指特定的版本,而是加入依賴關係。如果你在父POM中使用依賴管理,它應該可以工作。 –

+0

但不幸的是,它不。現在,我有這樣的休息.pom: com.fasterxml.jackson.core 傑克遜核心 ,這在一般.pom: com.fasterxml.jackson.core jackson-核心 2.7。1 之前我在這裏問過問題,它有提供由我以前讀過的一些解決方案。 – KOHTPOJIEP

3

解決!問題是我不僅使用jackson核心依賴項,還使用jackson數據綁定,數據類型和註釋,這些依賴項具有不同的版本:2.7.1代表「核心」,2.4.3代表其他版本。現在我爲它們全部使用相同的版本,因此添加依賴性會產生影響。 Thx爲您的幫助!)